Ansible  可以使用命令行方式进行自动化管理,命令管理工具都是有一系列模块、参数所支持的。基本语法如下:

ansible  <host-pattern>  [ –m  module_name ]  [ –a  args]

<host-pattern> : 对哪些主机有效

[ –m  module_name ]  :指定所使用的模块

[-a args ]  :模块特有参数

Ansible软件执行结果

     l  输出内容显示绿色:表示执行成功,没有任何改变

     l  输出内容显示×××:表示执行成功,但对被管理主机进行了改变

     l  输出内容显示红色:表示执行失败!!!

使用环境如下:


   角色        主机名          IP地址            组名
控制主机     ansible      192.168.66.138
被管理主机     node1      192.168.66.141       webserver
被管理主机     node2      192.168.66.142       mysql

        使用 ansible-doc 查看模块忙助信息的工具,最主要的选项 –l 用来列出可使用的模块,-s  用来累出某个模块的描述信息和使用事例。如列出 command 模块的描述信息和操作动作:

[root@promote .ssh]# ansible-doc -s command
- name: Executes a command on a remote node
   command:
       argv:                  # Allows the user to provide the command as a list
                                vs. a string.  Only
                                the string or the
                                list form can be
                                provided, not both.
                                One or the other
                                must be provided.
       chdir:                 # Change into this directory before running the
                                command.
       creates:               # A filename or (since 2.0) glob pattern. If it
                                already exists, this
                                step *won't* be run.
                                      ………

Ansible 自带了很多模块,能够下发执行 Ansible 的各种任务。首先了解一下常用的这些核心模块

1.  command 模块         

        Ansible 管理工具使用 –m 选项来指定使用模块,默认使用 command 模块,即 –m 选项省略是可以运行此模块,用于被管理主机上运行命令。

例如在被管理主机上执行 date 命令,显示被管理主机的时间。有三种执行命令的方式去管理写入主机清单中的主机。

(1)使用 IP 地址指定运行主机 

[root@promote .ssh]# ansible 192.168.66.141 -m command -a 'date'           //-m 指定模块, –a  模块特有参数
192.168.66.141 | CHANGED | rc=0 >>
2018年 10月 20日 星期六 21:58:18 CST

(2)使用被管理主机中的分类运行

[root@promote .ssh]# ansible mysql -m command -a 'date'
192.168.66.142 | CHANGED | rc=0 >>
2018年 10月 20日 星期六 21:58:43 CST

(3)在所有主机清单中的主机运行,使用 all

[root@promote .ssh]# ansible all -m command -a 'date'
192.168.66.141 | CHANGED | rc=0 >>
2018年 10月 20日 星期六 21:59:20 CST

192.168.66.142 | CHANGED | rc=0 >>
2018年 10月 20日 星期六 21:59:22 CST

2.  cron 模块

Ansible 中的 cron 模块用于定义任务计划。其中有两种状态(state): present 表示添加(省略时默认使用), absent  表示移除

(1)添加计划性任务。每隔一分钟向所有被管理主机输出名为“test  haha” 的文件在 /bin/目录下

[root@ansible .ssh]# ansible all -m cron -a 'minute="*/1" job="/bin/echo haha" name="test haha"'
192.168.66.141 | CHANGED => {             //表示执行成功
     "changed": true,
     "envs": [],
     "jobs": [
         "test haha"
     ]
}
192.168.66.142 | CHANGED => {
     "changed": true,
     "envs": [],
     "jobs": [
         "test haha"
     ]
}

查看计划性任务

[root@ansible .ssh]# ansible all -a 'crontab -l'
192.168.66.141 | CHANGED | rc=0 >>
#Ansible: test haha
*/1 * * * * /bin/echo haha

192.168.66.142 | CHANGED | rc=0 >>
#Ansible: test haha
*/1 * * * * /bin/echo haha

在被管理的主机上查看

[root@node1 bin]# which echo
/usr/bin/echo
您在 /var/spool/mail/root 中有新邮件
[root@node1 bin]# crontab -l
#Ansible: test haha
*/1 * * * * /bin/echo haha

[root@node2 .ssh]# which echo
/usr/bin/echo
您在 /var/spool/mail/root 中有新邮件
[root@node2 .ssh]# crontab -l
#Ansible: test haha
*/1 * * * * /bin/echo haha

(2)移除计划性任务

[root@ansible ~]# ansible all -m cron -a 'name="test haha" state=absent'
192.168.66.141 | CHANGED => {
     "changed": true,
     "envs": [],
     "jobs": []
}
192.168.66.142 | CHANGED => {
     "changed": true,
     "envs": [],
     "jobs": []
}

3 .  user模块

Ansible 中的 user 模块用于创建新用户和更改、删除已存在的用户。其中 name 选项用来指明创建用户名称

(1)创建用户。在所有被管理主机上创建名为 “test01”  用户

[root@ansible ~]# ansible all -m user -a 'name="test01"'           //name 指明创建用户名称
192.168.66.141 | CHANGED => {
     "changed": true,
     "comment": "",
     "create_home": true,
     "group": 1002,
     "home": "/home/test01",
     "name": "test01",
     "shell": "/bin/bash",
     "state": "present",
     "system": false,
     "uid": 1002
}
192.168.66.142 | CHANGED => {
     "changed": true,
     "comment": "",
     "create_home": true,
     "group": 1001,
     "home": "/home/test01",
     "name": "test01",
     "shell": "/bin/bash",
     "state": "present",
     "system": false,
     "uid": 1001
}

在被管理主机上查看是否创建用户

[root@node1 ~]# id test01
uid=1001(test01) gid=1001(test01) 组=1001(test01)

[root@node2 ~]# id test01
uid=1002(test01) gid=1002(test01) 组=1002(test01)

(2)删除用户

[root@ansible ~]# ansible mysql -m user -a 'name=test01 state=absent'       //删除被管理主机1 mysql 的用户,状态(state)absent 表示移除
192.168.66.142 | CHANGED => {
     "changed": true,
     "force": false,
     "name": "test01",
     "remove": false,
     "state": "absent"
}

在被管理主机上查询 “test01” 用户是否删除

[root@node1 ~]# id test01
id: test01: no such user

4 .  group  模块

Ansible 中的 group模块用于对用户组进行管理。

(1)创建 test02 组,将 test02 用户添加到 test02 组

[root@ansible ~]# ansible mysql -m group -a 'name=test02 gid=306 system=yes'
192.168.66.142 | CHANGED => {
     "changed": true,
     "gid": 306,
     "name": "test02",
     "state": "present",
     "system": true
}

在被管理主机查看添加的组

[root@ansible ~]# vim /etc/ansible/hosts
[root@ansible ~]# ansible mysql -a 'tail /etc/group'
192.168.66.142 | CHANGED | rc=0 >>
avahi:x:70:
slocate:x:21:
postdrop:x:90:
postfix:x:89:
stapusr:x:156:
stapsys:x:157:
stapdev:x:158:
tcpdump:x:72:
liu:x:1000:
test02:x:306:

(2)也可用 user 模块创建用户并添加到组

[root@ansible ~]# ansible webserver -m user -a 'name=mysql uid=306 group=mysql system=yes'
192.168.66.141 | CHANGED => {
     "append": false,
     "changed": true,
     "comment": "",
     "group": 1001,
     "home": "/home/mysql",
     "move_home": false,
     "name": "mysql",
     "shell": "/sbin/nologin",
     "state": "present",
     "uid": 306
}

查看创建的用户所属组

[root@ansible ~]# ansible webserver -a 'id mysql'
192.168.66.141 | CHANGED | rc=0 >>
uid=306(mysql) gid=1001(mysql) 组=1001(mysql)

[root@node2 ~]# id mysql
uid=306(mysql) gid=1001(mysql) 组=1001(mysql)

5 .  copy  模块

        Ansible 中的 copy模块---用于实现文件复制和批量下发文件。其中使用src来定义本地源文件路径,使用dest定义被管理主机文件路径,使用content则是指定信息内容来生成目标文件。

(1)将本地文件/etc/fstab 复制到被管理主机上的/opt/fstab.bk,将所有者设置为root,权限为644

[root@ansible ~]# ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.bk owner=root mode=644'
192.168.66.142 | CHANGED => {                      //src来定义本地源文件路径,使用dest定义被管理主机文件路径
     "changed": true,
     "checksum": "cb9821536bd491d110542bdf799c0828715ab6bd",
     "dest": "/opt/fstab.bk",
     "gid": 0,
     "group": "root",
     "md5sum": "643872130cf520bc6762e1c5d803a890",
     "mode": "0644",
     "owner": "root",
     "secontext": "system_u:object_r:usr_t:s0",
     "size": 689,
     "src": "/root/.ansible/tmp/ansible-tmp-1540103669.94-50253022890998/source",
     "state": "file",
     "uid": 0
}

查看复制文件

[root@ansible ~]# ansible mysql -a 'ls -l /opt'
192.168.66.142 | CHANGED | rc=0 >>
总用量 4
-rw-r--r--. 1 root root 689 10月 21 14:34 fstab.bk
drwxr-xr-x. 2 root root   6 3月  26 2015 rh

在被管理主机上查询

[root@node2 ~]# cd /opt/
[root@node2 opt]# ls -l

总用量 4
-rw-r--r--. 1 root root 689 10月 21 14:34 fstab.bk
drwxr-xr-x. 2 root root   6 3月  26 2015 rh

(2)将 “ this is test ” 写入 /opt/test.txt 文件中

[root@ansible ~]# ansible mysql -m copy -a 'content="this is test" dest=/opt/test.txt'  
192.168.66.142 | CHANGED => {                        //content则是指定信息内容来生成目标文件
     "changed": true,
     "checksum": "b6794b2000d94d348203d0279c2e7322b922cb16",
     "dest": "/opt/test.txt",
     "gid": 0,
     "group": "root",
     "md5sum": "8c6d115258631625b625486f81b09532",
     "mode": "0644",
     "owner": "root",
     "secontext": "system_u:object_r:usr_t:s0",
     "size": 12,
     "src": "/root/.ansible/tmp/ansible-tmp-1540104362.42-107935433980182/source",
     "state": "file",
     "uid": 0
}

查看生成文件

[root@ansible ~]# ansible mysql -a 'cat /opt/test.txt'
192.168.66.142 | CHANGED | rc=0 >>
this is test

在被管理主机上查询

[root@node2 opt]# ls
fstab.bk  rh  test.txt
[root@node2 opt]# cat test.txt
this is test[root@node2 opt]#

6 .  file  模块

       file 模块--用来设置文件属性。其中使用path指定文件路径,使用src定义源文件路径,使用name或dest 来替换创建文件的符号链接

(1)设置文件 /opt/fstab.bk 的所属主为 mysql ,所属组为 mysql , 权限为 666

[root@ansible ~]# ansible mysql -m file -a 'path=/opt/fstab.bk owner=mysql group=mysql mode=666'
192.168.66.142 | CHANGED => {                    //path指定文件路径
     "changed": true,
     "gid": 1001,
     "group": "mysql",
     "mode": "0666",
     "owner": "mysql",
     "path": "/opt/fstab.bk",
     "secontext": "system_u:object_r:usr_t:s0",
     "size": 689,
     "state": "file",
     "uid": 306
}

在被管理主机上查看

[root@node2 opt]# ls -la
总用量 8
drwxr-xr-x.  3 root  root   48 10月 21 14:46 .
dr-xr-xr-x. 18 root  root  235 8月  22 18:33 ..
-rw-rw-rw-.  1 mysql mysql 689 10月 21 14:34 fstab.bk      //属主,属组为 mysql ,权限为666
drwxr-xr-x.  2 root  root    6 3月  26 2015 rh
-rw-r--r--.  1 root  root   12 10月 21 14:46 test.txt

(2)设置文件 /opt/fstab.txt.link  为文件 /opt/fstab.bk 的连接文件

[root@ansible ~]# ansible mysql -m file -a 'src=/opt/fstab.bk path=/opt/fstab.txt.link state=link'
192.168.66.142 | CHANGED => {                   //src定义源文件路径                                     //状态为 link
     "changed": true,
     "dest": "/opt/fstab.txt.link",
     "gid": 0,
     "group": "root",
     "mode": "0777",
     "owner": "root",
     "secontext": "unconfined_u:object_r:usr_t:s0",
     "size": 13,
     "src": "/opt/fstab.bk",
     "state": "link",
     "uid": 0
}

[root@node2 opt]# ls
fstab.bk  fstab.txt.link  rh  test.txt

(3)创建空文件。状态(state)为 touch

[root@ansible ~]# ansible mysql -m file -a 'path=/opt/abc.txt state=touch'
192.168.66.142 | CHANGED => {
     "changed": true,
     "dest": "/opt/abc.txt",
     "gid": 0,
     "group": "root",
     "mode": "0644",
     "owner": "root",
     "secontext": "unconfined_u:object_r:usr_t:s0",
     "size": 0,
     "state": "file",
     "uid": 0
}

[root@node2 opt]# ls
abc.txt  fstab.bk  fstab.txt.link  rh  test.txt
[root@node2 opt]# cat abc.txt                 //空文件
[root@node2 opt]#

(4)创建目录 。状态为 directory

[root@ansible ~]# ansible mysql -m file -a 'path=/opt/abc state=directory'
192.168.66.142 | CHANGED => {
     "changed": true,
     "gid": 0,
     "group": "root",
     "mode": "0755",
     "owner": "root",
     "path": "/opt/abc",
     "secontext": "unconfined_u:object_r:usr_t:s0",
     "size": 6,
     "state": "directory",
     "uid": 0
}

(5)删除文件 。

[root@ansible ~]# ansible mysql -m file -a 'path=/opt/abc.txt state=absent'
192.168.66.142 | CHANGED => {
     "changed": true,
     "path": "/opt/abc.txt",
     "state": "absent"
}

[root@node2 opt]# ls
abc  fstab.bk  fstab.txt.link  rh  test.txt

7 .  ping 模块

在Ansible 中使用 ping 模块 检查指定主机的连通性。

[root@ansible ~]# ansible all -m ping
192.168.66.142 | SUCCESS => {
     "changed": false,
     "ping": "pong"
}
192.168.66.141 | SUCCESS => {
     "changed": false,
     "ping": "pong"
}

8 .  yum 模块

          Ansible 中的 yum 模块负责在被管理主及上安装与卸载软件包。其中 name 指定要安装的软件包,使用 state 指定安装软件包的状态,present 、latest 用来表示安装, absent 表示卸载。

(1)在被管理主机上安装 httpd 的软件包

[root@node1 ~]# rpm -q httpd               
未安装软件包 httpd                                              //可以看到被管理主机上都为安装 httpd 服务

[root@node2 opt]# rpm -q httpd
未安装软件包 httpd

在安装的过程中不会出现安装提示信息,只有在安装完成后,会显示较长的安装信息

[root@ansible ~]# ansible all -m yum -a 'name=httpd'
192.168.66.141 | CHANGED => {             //第一台主机安装完成后
     "ansible_facts": {
         "pkg_mgr": "yum"
     },
     "changed": true,
     "msg": "warning: /var/cache/yum/x86_64/7/base/packages/mailcap-2.1.41-2.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY\nImporting GPG key 0xF4A80EB5:\n Userid     : \"CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>\"\n Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5\n Package    : centos-release-7-4.1708.el7.centos.x86_64 (@anaconda)\n From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7\n",
     "rc": 0,
     "results": [
         "Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\n * base: centos.ustc.edu.cn\n * extras: centos.ustc.edu.cn\n * updates: centos.ustc.edu.cn\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-80.el7.centos.1 will be installed\n--> Processing Dependency: httpd-tools = 2.4.6-80.el7.centos.1 for package: httpd-2.4.6-80.el7.centos.1.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-80.el7.centos.1.x86_64\n--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-80.el7.centos.1.x86_64\n--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-80.el7.centos.1.x86_64\n--> Running transaction check\n---> Package apr.x86_64 0:1.4.8-3.el7_4.1 will be installed\n---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed\n---> Package httpd-tools.x86_64 0:2.4.6-80.el7.centos.1 will be installed\n---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package           Arch         Version                     Repository     Size\n================================================================================\nInstalling:\n httpd             x86_64       2.4.6-80.el7.centos.1       updates       2.7 M\nInstalling for dependencies:\n apr               x86_64       1.4.8-3.el7_4.1             base          103 k\n apr-util          x86_64       1.5.2-6.el7                 base           92 k\n httpd-tools       x86_64       2.4.6-80.el7.centos.1       updates        90 k\n mailcap           noarch       2.1.41-2.el7                base           31 k\n\nTransaction Summary\n================================================================================\nInstall  1 Package (+4 Dependent packages)\n\nTotal download size: 3.0 M\nInstalled size: 10 M\nDownloading packages:\nPublic key for mailcap-2.1.41-2.el7.noarch.rpm is not installed\nPublic key for httpd-tools-2.4.6-80.el7.centos.1.x86_64.rpm is not installed\n--------------------------------------------------------------------------------\nTotal                                              1.6 MB/s | 3.0 MB  00:01     \nRetrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : apr-1.4.8-3.el7_4.1.x86_64                                   1/5 \n  Installing : apr-util-1.5.2-6.el7.x86_64                                  2/5 \n  Installing : httpd-tools-2.4.6-80.el7.centos.1.x86_64                     3/5 \n  Installing : mailcap-2.1.41-2.el7.noarch                                  4/5 \n  Installing : httpd-2.4.6-80.el7.centos.1.x86_64                           5/5 \n  Verifying  : mailcap-2.1.41-2.el7.noarch                                  1/5 \n  Verifying  : httpd-tools-2.4.6-80.el7.centos.1.x86_64                     2/5 \n  Verifying  : apr-util-1.5.2-6.el7.x86_64                                  3/5 \n  Verifying  : apr-1.4.8-3.el7_4.1.x86_64                                   4/5 \n  Verifying  : httpd-2.4.6-80.el7.centos.1.x86_64                           5/5 \n\nInstalled:\n  httpd.x86_64 0:2.4.6-80.el7.centos.1                                          \n\nDependency Installed:\n  apr.x86_64 0:1.4.8-3.el7_4.1                  apr-util.x86_64 0:1.5.2-6.el7   \n  httpd-tools.x86_64 0:2.4.6-80.el7.centos.1    mailcap.noarch 0:2.1.41-2.el7   \n\nComplete!\n"
     ]
}
192.168.66.142 | CHANGED => {             //第二台主机安装完成后
     "ansible_facts": {
         "pkg_mgr": "yum"
     },
     "changed": true,
     "msg": "http://ftp.sjtu.edu.cn/centos/7.5.1804/os/x86_64/Packages/mailcap-2.1.41-2.el7.noarch.rpm: [Errno 14] curl#7 - \"Failed to connect to 2001:da8:8000:6023::230: Network is unreachable\"\nTrying other mirror.\n",
     "rc": 0,
     "results": [
         "Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\n * base: mirrors.nju.edu.cn\n * extras: mirrors.163.com\n * updates: mirrors.nju.edu.cn\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-80.el7.centos.1 will be installed\n--> Processing Dependency: httpd-tools = 2.4.6-80.el7.centos.1 for package: httpd-2.4.6-80.el7.centos.1.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-80.el7.centos.1.x86_64\n--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-80.el7.centos.1.x86_64\n--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-80.el7.centos.1.x86_64\n--> Running transaction check\n---> Package apr.x86_64 0:1.4.8-3.el7_4.1 will be installed\n---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed\n---> Package httpd-tools.x86_64 0:2.4.6-80.el7.centos.1 will be installed\n---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package           Arch         Version                     Repository     Size\n================================================================================\nInstalling:\n httpd             x86_64       2.4.6-80.el7.centos.1       updates       2.7 M\nInstalling for dependencies:\n apr               x86_64       1.4.8-3.el7_4.1             base          103 k\n apr-util          x86_64       1.5.2-6.el7                 base           92 k\n httpd-tools       x86_64       2.4.6-80.el7.centos.1       updates        90 k\n mailcap           noarch       2.1.41-2.el7                base           31 k\n\nTransaction Summary\n================================================================================\nInstall  1 Package (+4 Dependent packages)\n\nTotal download size: 3.0 M\nInstalled size: 10 M\nDownloading packages:\n--------------------------------------------------------------------------------\nTotal                                              198 kB/s | 3.0 MB  00:15     \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : apr-1.4.8-3.el7_4.1.x86_64                                   1/5 \n  Installing : apr-util-1.5.2-6.el7.x86_64                                  2/5 \n  Installing : httpd-tools-2.4.6-80.el7.centos.1.x86_64                     3/5 \n  Installing : mailcap-2.1.41-2.el7.noarch                                  4/5 \n  Installing : httpd-2.4.6-80.el7.centos.1.x86_64                           5/5 \n  Verifying  : mailcap-2.1.41-2.el7.noarch                                  1/5 \n  Verifying  : httpd-tools-2.4.6-80.el7.centos.1.x86_64                     2/5 \n  Verifying  : apr-util-1.5.2-6.el7.x86_64                                  3/5 \n  Verifying  : apr-1.4.8-3.el7_4.1.x86_64                                   4/5 \n  Verifying  : httpd-2.4.6-80.el7.centos.1.x86_64                           5/5 \n\nInstalled:\n  httpd.x86_64 0:2.4.6-80.el7.centos.1                                          \n\nDependency Installed:\n  apr.x86_64 0:1.4.8-3.el7_4.1                  apr-util.x86_64 0:1.5.2-6.el7   \n  httpd-tools.x86_64 0:2.4.6-80.el7.centos.1    mailcap.noarch 0:2.1.41-2.el7   \n\nComplete!\n"
     ]
}

在被管理主机上查看 httpd 服务是否安装

[root@node1 ~]# rpm -q httpd
httpd-2.4.6-80.el7.centos.1.x86_64

[root@node2 opt]# rpm -q httpd
httpd-2.4.6-80.el7.centos.1.x86_64

(2)卸载 http 软件包

[root@ansible ~]# ansible webserver -m yum -a 'name=httpd state=absent'   //卸载其中一台主机上的 httpd 服务。
192.168.66.141 | CHANGED => {
     "ansible_facts": {
         "pkg_mgr": "yum"
     },
     "changed": true,
     "msg": "",
     "rc": 0,
     "results": [
         "已加载插件:fastestmirror, langpacks\n正在解决依赖关系\n--> 正在检查事务\n---> 软件包 httpd.x86_64.0.2.4.6-80.el7.centos.1 将被 删除\n--> 解决依赖关系完成\n\n依赖关系解决\n\n================================================================================\n Package      架构          版本                          源               大小\n================================================================================\n正在删除:\n httpd        x86_64        2.4.6-80.el7.centos.1         @updates        9.4 M\n\n事务概要\n================================================================================\n移除  1 软件包\n\n安装大小:9.4 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  正在删除    : httpd-2.4.6-80.el7.centos.1.x86_64                          1/1 \n  验证中      : httpd-2.4.6-80.el7.centos.1.x86_64                          1/1 \n\n删除:\n  httpd.x86_64 0:2.4.6-80.el7.centos.1                                          \n\n完毕!\n"
     ]
}

9 .  service 模块

       在 Ansible 中使用 service 模块来控制管理服务的运行状态。其中,使用enabled 表示是否开机自启动,取值为ture 或flase;使用name定义服务名称;使用state 指定服务状态,取值分别为started 、stopped 、restarted.

启动 httpd 服务,并设置开机自启动。

[root@ansible ~]# ansible mysql -m service -a 'name=httpd enabled=true state=started'
192.168.66.142 | CHANGED => {                                                 //设置开机自启动,状态为 started
     "changed": true,
     "enabled": true,
     "name": "httpd",
     "state": "started",
     "status": {
         "ActiveEnterTimestampMonotonic": "0",
         "ActiveExitTimestampMonotonic": "0",
         "ActiveState": "inactive",

               …….

查看 httpd 服务的状态

[root@node2 opt]# systemctl status httpd
httpd.service - The Apache HTTP Server
    Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
    Active: active (running) since 日 2018-10-21 15:22:41 CST; 46s ago
      Docs: man:httpd(8)
            man:apachectl(8)
  Main PID: 6036 (httpd)
    Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
    CGroup: /system.slice/httpd.service
            ├─6036 /usr/sbin/httpd -DFOREGROUND
            ├─6050 /usr/sbin/httpd -DFOREGROUND
            ├─6051 /usr/sbin/httpd -DFOREGROUND
            ├─6052 /usr/sbin/httpd -DFOREGROUND
            ├─6053 /usr/sbin/httpd -DFOREGROUND
            └─6056 /usr/sbin/httpd -DFOREGROUND

10月 21 15:22:16 node2 systemd[1]: Starting The Apache HTTP Server...
10月 21 15:22:31 node2 httpd[6036]: AH00558: httpd: Could not reliably determine the serv...age
10月 21 15:22:41 node2 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

10 .  shell 模块

        Ansible 中的 shell 模块可以在被管理了主机上运行命令,并支持向管道符号等功能的复杂命令

创建用户后使用无交互模式给用户设置密码

[root@ansible ~]# ansible mysql -m user -a 'name=tom'
192.168.66.142 | CHANGED => {
     "changed": true,
     "comment": "",
     "create_home": true,
     "group": 1003,
     "home": "/home/tom",
     "name": "tom",
     "shell": "/bin/bash",
     "state": "present",
     "system": false,
     "uid": 1003
}
[root@ansible ~]# ansible mysql -m shell -a 'echo acb123 | passwd --stdin tom'
192.168.66.142 | CHANGED | rc=0 >>
更改用户 tom 的密码 。
passwd:所有的身份验证令牌已经成功更新。

11 .  script 模块

        Ansible 中的 script可以将本地脚本复制到被管理主机上进行运行。需要注意的是,使用相对路径来指定脚本。

编辑一个本地脚本 test.sh,复制到被管理主机上运行。

[root@ansible ~]# cd /opt/
[root@ansible opt]# vim test.sh

#!/bin/bash
echo "this is test script" > /opt/script.txt            //被管理主机脚本路径
chmod 666 /opt/script.txt

[root@ansible opt]# chmod +x test.sh   //给脚本执行权限


[root@ansible opt]# ansible all -m script -a 'test.sh'         //复制到被管理主机     
192.168.66.142 | CHANGED => {
     "changed": true,
     "rc": 0,
     "stderr": "Shared connection to 192.168.66.142 closed.\r\n",
     "stderr_lines": [
         "Shared connection to 192.168.66.142 closed."
     ],
     "stdout": "",
     "stdout_lines": []
}
192.168.66.141 | CHANGED => {
     "changed": true,
     "rc": 0,
     "stderr": "Shared connection to 192.168.66.141 closed.\r\n",
     "stderr_lines": [
         "Shared connection to 192.168.66.141 closed."
     ],
     "stdout": "",
     "stdout_lines": []
}

在被管理主机上查看脚本

[root@node1 opt]# ls -l
总用量 4
drwxr-xr-x. 2 root root  6 3月  26 2015 rh
-rw-rw-rw-. 1 root root 20 10月 21 15:31 script.txt                 //脚本文件
[root@node1 opt]# cat script.txt                                               //查看脚本内容
this is test script

[root@node2 opt]# ls -l
总用量 12
drwxr-xr-x. 2 root  root    6 10月 21 15:07 abc
-rw-rw-rw-. 1 mysql mysql 689 10月 21 14:34 fstab.bk
lrwxrwxrwx. 1 root  root   13 10月 21 15:00 fstab.txt.link -> /opt/fstab.bk
drwxr-xr-x. 2 root  root    6 3月  26 2015 rh
-rw-rw-rw-. 1 root  root   20 10月 21 15:31 script.txt
-rw-r--r--. 1 root  root   12 10月 21 14:46 test.txt
[root@node2 opt]# cat script.txt
this is test script

12 .  setup 模块

         在Ansible 中使用 setup 模块收集、查看被管理主机的 facts (facts 是Ansible 采集被管理主机设备信息的一个功能)。每个被管理主机在接收并运行命令之前,都将自己的相关信息(操作系统版本、IP地址等)发送给控制主机。

[root@ansible opt]# ansible mysql -m setup
192.168.66.142 | SUCCESS => {
     "ansible_facts": {
         "ansible_all_ipv4_addresses": [
             "192.168.122.1",
             "192.168.66.142"
         ],
         "ansible_all_ipv6_addresses": [
             "fe80::7d01:50f5:b8bc:52cd"
         ],
         "ansible_apparmor": {
             "status": "disabled"
         },
         "ansible_architecture": "x86_64",
         "ansible_bios_date": "05/19/2017",
         "ansible_bios_version": "6.00",
         "ansible_cmdline": {
             "BOOT_IMAGE": "/vmlinuz-3.10.0-693.el7.x86_64",
             "LANG": "zh_CN.UTF-8",
             "quiet": true,
             "rhgb": true,
             "ro": true,
             "root": "UUID=5e874d95-9d77-4a71-8f97-454363885543"

              ……