ansible常用基础模块示例

 

查看ansible支持的模块  =======================

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

查看模块支持的参数

# ansible-doc <模块名称>

[root@master ~]# ansible-doc ping

查看该模块有哪些参数可以使用

[root@master ~]# ansible-doc  -s  <模块名称>

如:

ansible-doc  -s user  查看user模块有哪些参数可以用

推荐常用模块博客:http://blog.csdn.net/pushiqiang/article/details/78249665

1、ping模块

2、command模块

3、shell 模块

4、copy模块

5、cron模块

6、fetch模块

7、file模块

8、hostname模块

9、yum模块

10、service模块

11、 uri模块

12、group模块

13、user模块

14、script模块

15、setup模块

16、authorized_key模块

17、synchronize模块(使用rsync同步文件。使用rsync 模块,系统必须安装rsync 包,否则无法使用这个模块)

ansible模块的说明:

# ansible <pattern> -m <module_name> [-a <arguments>]

1、ping模块=====================

作用:检测被管理端是否在线

[root@Allen ~]# ansible test -m ping

172.16.20.114 | SUCCESS => {

    "changed": false,

    "ping": "pong"

}

ping的方法有三种:

1、all   代表全部的管理节点,即我们/etc/ansible/hosts中添加的全部被管理的主机

2、指定分组   如:自定义分组test  下面截图会列出来

3、指定单个主机ip或者域名    即我们/etc/ansible/hosts中添加的主机名或ip

2、command模块 =====================

作用:在被管理端执行命令,不支持重定向,管道 ,默认模块

指定test组(这个组是配置文件中指定的)

[root@Allen ~]# ansible test -m command -a 'uptime'    //-a指定要传递给模块的参数

172.16.20.114 | SUCCESS | rc=0 >>

 18:19:18 up  1:40,  2 users,  load average: 0.00, 0.00, 0.00

 

all代表所有主机:

[root@Allen ~]# ansible all -m command -a 'uptime'

172.16.20.115 | SUCCESS | rc=0 >>

 18:20:30 up 22 min,  3 users,  load average: 0.00, 0.00, 0.00

用ip具体指向某一台服务器

[root@Allen ~]# ansible 172.16.20.114 -m command -a 'uptime'

172.16.20.114 | SUCCESS | rc=0 >>

 18:20:31 up  1:41,  2 users,  load average: 0.00, 0.00, 0.00

[root@Allen ~]# ansible test -m command -a 'date'

172.16.20.114 | SUCCESS | rc=0 >>

Mon Jul 31 18:21:16 CST 2017

 

[root@Allen ~]# ansible test -m command -a 'ls /tmp'

172.16.20.114 | SUCCESS | rc=0 >>

ansible_0TxRxj

yum.log

 

[root@Allen ~]# ansible test -m command -a 'df -Th'

172.16.20.115 | SUCCESS | rc=0 >>

Filesystem           Type   Size  Used Avail Use% Mounted on

/dev/mapper/VolGroup-lv_root

                     ext4    50G  1.1G   46G   3% /

tmpfs                tmpfs  932M     0  932M   0% /dev/shm

/dev/sda1            ext4   477M   38M  414M   9% /boot

/dev/mapper/VolGroup-lv_home

                     ext4    45G   52M   43G   1% /home

 

[root@Allen ~]# ansible test -m command -a 'touch /tmp/a.txt'

 [WARNING]: Consider using file module with state=touch rather than running touch

//警告意思:建议用file模块创建一个文件 不建议用command运行touch命令。类似salt的cmd.run命令。是一个万能的命令,就连删除根都可以

172.16.20.114 | SUCCESS | rc=0 >>

[root@Allen ~]# ansible all -a '/bin/echo hello'

172.16.20.114 | SUCCESS | rc=0 >>

hello

 

参数:========================

       chdir=<Directory>

[root@Allen ~]# ansible test -m command -a 'chdir=/tmp ls ./'

172.16.20.114 | SUCCESS | rc=0 >>

ansible_OQrj0Q

b.txt

 

3、shell 模块==========================

作用:在被管理端执行命令 支持重定向,管道

先看一下command的结果:

[root@Allen ~]# ansible test -m command -a 'echo "hello ansible" > /tmp/b.txt'

172.16.20.115 | SUCCESS | rc=0 >>

hello ansible > /tmp/b.txt

 

172.16.20.114 | SUCCESS | rc=0 >>

hello ansible > /tmp/b.txt

再看一下shell的结果:

[root@Allen ~]# ansible test -m shell -a "echo 'hello ansible2' > /tmp/ddd.txt"

172.16.20.114 | SUCCESS | rc=0 >>

172.16.20.115 | SUCCESS | rc=0 >>

结果显示:如果是command她就在屏幕上打印出来,如果是shell,他就重定向到文件,不在屏幕上打印

[root@Allen ~]# ansible test -m command -a "cat /tmp/b.txt"

172.16.20.114 | SUCCESS | rc=0 >>

hello ansible

 

172.16.20.115 | SUCCESS | rc=0 >>

hello ansible

 

[root@Allen ~]# ansible test -m shell -a 'ls /tmp'

172.16.20.114 | SUCCESS | rc=0 >>

ansible_HAgoqq

b.txt

 

172.16.20.115 | SUCCESS | rc=0 >>

ansible_so6OnF

a.txt

b.txt

yum.log

 

参数:

       chdir=<Directory>

 

[root@Allen ~]# ansible test -m shell -a 'chdir=/tmp ls ./'

172.16.20.114 | SUCCESS | rc=0 >>

ansible_PsJuUc

bb.txt

 

4.copy模块===========================

作用:拷贝ansible管理端的文件到远程主机的指定位置

常见参数有:

     dest=    指明拷贝文件的目标目录位置,使用绝对路径,如果源是目录,则目标也要是目录,如果目标文件已存在,会覆盖原有内容

     src=     指明本地路径下的某个文件,可以使用相对路径和绝对路径,支持直接指定目录,如果源是目录,则目标也要是目录。如果路径用/结尾了,那么只复制目录里面的内容。如果没有用/来结尾,则包含目录在内的整个内容都复制,类似rsync

     mode=    指明复制时,目标文件的权限

     owner=   指明复制时,目标文件的属主

     group=   指明复制时,目标文件的属组

     content= 指明复制到目标主机上的内容,不能与src一起使用,相当于复制content指明的数据到目标文件中

 

[root@Allen ~]# ansible test -m copy -a "src=/etc/hosts dest=/tmp mode=755 owner=root group=root"

 

172.16.20.114 | SUCCESS => {

    "changed": true,

    "checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa",

    "dest": "/tmp/hosts",

    "gid": 0,

    "group": "root",

    "mode": "0755",

    "owner": "root",

    "path": "/tmp/hosts",

    "size": 158,

    "state": "file",

    "uid": 0

}

 

[root@Allen ~]# ansible test -m copy -a 'content="hello linux" dest=/tmp/c.txt mode=600'

172.16.20.114 | SUCCESS => {

    "changed": true,

    "checksum": "223ce1d650508823f9dd51d8cb4b527ad3d03ca7",

    "dest": "/tmp/c.txt",

    "gid": 0,

    "group": "root",

    "md5sum": "c5fe55563f6ea61e2b28be7c8a5835c2",

    "mode": "0600",

    "owner": "root",

    "size": 11,

    "src": "/root/.ansible/tmp/ansible-tmp-1501510008.59-35638455893367/source",

    "state": "file",

    "uid": 0

}

[root@Allen ansible]#ansible all -m copy -a 'content="hello ansible\nHi lifu" dest=/tmp/test.ansible'

到远程主机上查看:

[root@localhost tmp]# cat test.ansible 

hello ansible

Hi lifu[root@localhost tmp]#

 

5.cron模块============================

作用:管理计划任务的模块

  常见参数有:

    minute=  指明计划任务的分钟,支持格式:0-59,*,*/2等,与正常cron任务定义的一样的语法,省略时,默认为*,也就是每分钟都执行

    hour=    指明计划任务的小时,支持的语法:0-23,*,*/2等,省略时,默认为*,也就是每小时都执行

    day=     指明计划任务的天,支持的语法:1-31,*,*/2等,省略时,默认为*,也就是每天都执行

    month=   指明计划任务的月,支持的语法为:1-12,*,*/2等,省略时,默认为*,也就是每月都执行

    weekday= 指明计划任务的星期几,支持的语法为:0-6,*等,省略时,默认为*,也就是每星期几都执行

    reboot   指明计划任务执行的时间为每次重启之后

    name=    给该计划任务取个名称,必须要给明。每个任务的名称不能一样。

    job=     执行的任务是什么,当state=present时才有意义

    state=present|absent   表示这个任务是创建还是删除,present表示创建,absent表示删除,默认是present

创建计划任务

[root@Allen ~]# ansible test -m cron -a 'minute=*/5 name=Ajob job="/usr/sbin/ntpdate 172.16.8.100 &> /dev/null" state=present'

172.16.20.115 | SUCCESS => {

    "changed": true,

    "envs": [],

    "jobs": [

        "Ajob"

    ]

}

 

删除计划任务

[root@Allen ~]# ansible test -m cron -a 'minute=*/5 name=Ajob job="/usr/sbin/ntpdate 172.16.8.100 &> /dev/null state=absent"'

172.16.20.115 | SUCCESS => {

    "changed": true,

    "envs": [],

    "jobs": [

        "Ajob"

    ]

}

查看计划任务

[root@Allen ~]# ansible test -m shell -a "crontab -l"

172.16.20.115 | SUCCESS | rc=0 >>

#Ansible: Ajob

*/5 * * * * /usr/sbin/ntpdate 172.16.8.100 &> /dev/null

 

删除任务计划

[root@Allen ~]# ansible test -m shell -a "crontab -r"

172.16.20.115 | SUCCESS | rc=0 >>

 

6.fetch模块   ===========================

作用:从远程主机拉取文件到本地。一般情况下,只会从一个远程节点拉取数据

 常见参数有:

    dest=  从远程主机上拉取的文件存放在本地的位置,一般只能是目录

    src=   指明远程主机上要拉取的文件,只能是文件,不能是目录

[root@Allen ~]# ansible test -m fetch -a 'src=/etc/passwd dest=/tmp'

172.16.20.115 | SUCCESS => {

    "changed": true,

    "checksum": "41b88ce7a15c857acd8d1d71d9b0e9f985643ba6",

    "dest": "/tmp/172.16.20.115/etc/passwd",

    "md5sum": "76945c2dd3a7b0b83f64ef91e6604bcf",

    "remote_checksum": "41b88ce7a15c857acd8d1d71d9b0e9f985643ba6",

    "remote_md5sum": null

}

 

作用:用于设定远程主机上的文件属性

   常见参数有:

        path=   指明对哪个文件修改其属性

        src=   指明path=指明的文件是软链接文件,其对应的源文件是谁,必须要在state=link时才有用

        state=directory|link|absent   表示创建的文件是目录还是软链接

        owner=   指明文件的属主

        group=   指明文件的属组

        mode=   指明文件的权限

创建软链接的用法:

            src=  path=  state=link

 修改文件属性的用法:

            path=  owner=  mode=  group=

 创建目录的用法:

            path=  state=directory

 删除文件:

            path=  state=absent

7、file模块======================== 

创建软连接:

[root@Allen ~]# ansible test -m file -a 'src=/etc/passwd path=/tmp/passwd.link state=link'

172.16.20.115 | SUCCESS => {

    "changed": true,

    "dest": "/tmp/passwd.link",

    "gid": 0,

    "group": "root",

    "mode": "0777",

    "owner": "root",

    "size": 11,

    "src": "/etc/passwd",

    "state": "link",

    "uid": 0

}

查看刚创建的/tmp下的软连接

[root@Allen ~]# ansible all -m shell -a 'ls -l /tmp/passwd.link'

172.16.20.114 | SUCCESS | rc=0 >>

lrwxrwxrwx 1 root root 11 Aug  1 18:45 /tmp/passwd.link -> /etc/passwd

删除文件

[root@Allen ~]# ansible test -m file -a 'path=/tmp/cc.txt state=absent'

172.16.20.114 | SUCCESS => {

    "changed": True,

    "path": "/tmp/cc.txt",

    "state": "absent"

}

 

修改文件属性

[root@Allen ~]# ansible test -m file -a 'path=/tmp/bb.txt mode=700 owner=root group=root'

172.16.20.114 | SUCCESS => {

    "changed": true,

    "gid": 0,

    "group": "root",

    "mode": "0700",

    "owner": "root",

    "path": "/tmp/bb.txt",

    "size": 14,

    "state": "file",

    "uid": 0

}

创建目录(可以递归创建,直接加上文件名即可)

如果state=directory,那么如果目录不存在,那么所有的子目录将被创建(而且提供权限的创建)

如果state=file,文件将不会被创建

[root@Allen ~]# ansible test -m file -a 'path=/tmp/bj state=directory'

172.16.20.114 | SUCCESS => {

    "changed": true,

    "gid": 0,

    "group": "root",

    "mode": "0755",

    "owner": "root",

    "path": "/tmp/bj",

    "size": 4096,

    "state": "directory",

    "uid": 0

}

 

创建文件

[root@Allen ~]# ansible all -m file -a 'name=d.txt state=touch'   

172.16.20.114 | SUCCESS => {

    "changed": true,

    "dest": "d.txt",

    "gid": 0,

    "group": "root",

    "mode": "0644",

    "owner": "root",

    "size": 0,

    "state": "file",

    "uid": 0

}

删除目录(可以递归删除,无需任何参数,直接加上)

[root@Allen ~]# ansible test -m file -a 'path=/tmp/bj state=absent'

172.16.20.114 | SUCCESS => {

    "changed": true,

    "path": "/tmp/bj",

    "state": "absent"

}

 

[root@Allen ~]# ansible test -m shell -a 'ls -l /tmp'

172.16.20.114 | SUCCESS | rc=0 >>

total 20

drwx------ 2 root root 4096 Jul 31 22:40 ansible_KVKNun

-rwx------ 1 root root   14 Jul 31 21:42 bb.txt

-rw------- 1 root root   11 Jul 31 22:07 c.txt

-rw-r--r-- 1 root root   15 Jul 31 21:58 ddd.txt

-rwxr-xr-x 1 root root  158 Jul 31 21:51 hosts

lrwxrwxrwx 1 root root   11 Jul 31 22:27 passwd.link -> /etc/passwd

 

 

8.hostname模块============================

作用:管理远程主机上的主机名

常用参数有

       name=  指明主机名

[root@Allen ~]# ansible test -m shell -a 'hostname'  //查看主机名

172.16.20.114 | SUCCESS | rc=0 >>

Allen.ansible.com

 

[root@Allen ~]# ansible test -m hostname -a 'name=Allen'   //更改主机名

172.16.20.114 | SUCCESS => {

    "ansible_facts": {

        "ansible_domain": "",

        "ansible_fqdn": "Allen",

        "ansible_hostname": "Allen",

        "ansible_nodename": "Allen"

    },

    "changed": true,

    "name": "Allen"

}

 

9.yum模块==============================

作用:基于yum机制,对远程主机管理程序包

   常用参数有:

       name=     指明程序包的名称,可以带上版本号,不指明版本,就是默认最新版本

       name=httpd

       name=httpd-2.2.15