扩大
缩小

ansible学习系列2-ansible常用模块使用

1. 查看支持的模块

[root@localhost ~]# ansible-doc -l

这里我们看下ansible的支持的模块个数

[root@localhost ~]# ansible-doc -l |wc -l   #查看支持的模块个数
1039
[root@localhost ~]# ansible --version        #查看我们的ansible版本号
ansible 2.3.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.6.6 (r266:84292, Aug 18 2016, 14:53:48) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]

2.获取模块的帮助

这里我们使用ansible-doc获取下command模块的使用方式。

[root@localhost ~]# ansible-doc command

 

3.1 command模块

command :作为ansible的默认模块,可以允许远程主机范围内的所有shell命令。

注意: 在command的命令中含有像`$ HOME'这样的变量和像``<“',`”>“, `“”“”,“”;“”和“”&“'将无法正常工作(如果需要这些功能,请使用[shell]模块)

[root@localhost ~]# ansible 192.168.168.11* -m command -a 'ip addr show dev eth0'
192.168.168.115 | SUCCESS | rc=0 >>
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:50:56:29:8d:e2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.168.115/24 brd 192.168.168.255 scope global eth0
    inet6 fe80::250:56ff:fe29:8de2/64 scope link 
       valid_lft forever preferred_lft forever

192.168.168.111 | SUCCESS | rc=0 >>
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:77:77:91 brd ff:ff:ff:ff:ff:ff
    inet 192.168.168.111/24 brd 192.168.168.255 scope global eth0
    inet6 fe80::20c:29ff:fe77:7791/64 scope link 
       valid_lft forever preferred_lft forever

3.2 script模块

功能:在远程主机上执行主控端的脚本,相当于scp+shell组合。

[root@localhost ~]# ansible all -m script -a "/home/test.sh 12 34"

3.3 shell模块

功能:执行远程主机的shell脚本文件

[root@localhost ~]# ansible all -m shell -a "/home/test.sh"

shell替代command执行

[root@localhost ~]# ansible 192.168.168.11* -m shell -a 'ip addr show dev eth0'
192.168.168.111 | SUCCESS | rc=0 >>
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:77:77:91 brd ff:ff:ff:ff:ff:ff
    inet 192.168.168.111/24 brd 192.168.168.255 scope global eth0
    inet6 fe80::20c:29ff:fe77:7791/64 scope link 
       valid_lft forever preferred_lft forever

192.168.168.115 | SUCCESS | rc=0 >>
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:50:56:29:8d:e2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.168.115/24 brd 192.168.168.255 scope global eth0
    inet6 fe80::250:56ff:fe29:8de2/64 scope link 
       valid_lft forever preferred_lft forever

3.4 copy模块

功能: 实现主控端向目标主机copy文件。

[root@localhost ~]# ansible all -m copy -a "src=/home/test.sh dest=/tmp/ owner=root group=root mode=0755"    
#src 主控端文件位置
#dest 被控端目标位置
#owner 文件复制过去后的所有者
#group 文件复制过去后的所属组
#mode 文件的权限设定,执行a+x这种方式

3.5 stat模块

功能: 获取远程文件的状态信息,包括atime,ctime,mtime,md5,uid,gid等信息。

[root@localhost ~]# ansible all -m stat -a "path=/etc/sysctl.conf"

3.6 yum模块

功能: 安装软件包。

[root@localhost ~]# ansible all -m yum -a "name=httpd state=latest disable_gpg_check=yes enablerepo=epel"
#name 包名
#state (Choices: present, installed, latest, absent, removed)[Default: present]
#disable_gpg_check:禁止gpg检查
#enablerepo:只启动指定的repo

3.7 cron模块

功能:远程主机crontab配置

[root@localhost ~]# ansible all -m cron -a "name='test' hour='2-5' minute='*/5' day='1' month='3,4' weekday='1' job='ls -l' user=tom"
192.168.168.115 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": [
        "test"
    ]
}
192.168.168.111 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": [
        "test"
    ]
}

我们去被控主机看下生成的crontab作业

[root@localhost ~]# crontab  -l -u tom
#Ansible: test
*/5 2-5 1 3,4 1 ls -l

删除指定crontab

[root@localhost ~]# ansible all -m cron -a "name=test state=absent"

3.8 mount模块

功能: 挂载文件系统

[root@localhost ~]# ansible 192.168.168.111 -m mount -a "path=/mnt/data src=/dev/sd0 fstype=ext3 ots=ro state=present"

注:mount已经使用path代替了原来的name参数,但是name参数还是可以使用的。

3.9 service模块

功能: 服务管理

[root@localhost ~]# ansible all -m service -a "name=httpd state=restarted"    #启动服务
[root@localhost ~]# ansible all -m service -a "name=httpd state=running"      #查看服务状态
[root@localhost ~]# ansible all -m service -a "name=httpd state=stoped"       #停止服务

3.10 user模块

功能: 远程主机的用户管理

[root@localhost ~]# ansible all -m user -a "name=jerry comment=' doubi jerry'"   #添加用户 详细参数参考ansible-doc user
[root@localhost ~]# ansible all -m user -a "name=jerry state=absent remove=yes"  #删除用户

 

posted on 2017-06-28 22:01  LinuxPanda  阅读(3995)  评论(0编辑  收藏  举报

导航