1. command模块:默认模块,执行日常的一些命令,但是不支持通配符,管道,重定向

chdir    #切换目录
creates   #条件判断,如果该文件没有则执行
removes  #如果文件有则执行
[root@master01 ~]# ansible all -a 'chdir=/mnt ls -al'
192.168.3.100 | CHANGED | rc=0 >>
总用量 0
drwxr-xr-x.  4 root   root    29 8月  11 11:57 .
dr-xr-xr-x. 20 root   root   281 8月  19 01:39 ..
drwxr-xr-x.  2 root   root     6 8月  10 18:57 hgfs
drwxrwxrwx   6 nobody nobody  61 8月  18 22:37 nfs
192.168.3.101 | CHANGED | rc=0 >>
总用量 0
drwxr-xr-x.  4 root   root    29 8月  11 20:04 .
dr-xr-xr-x. 18 root   root   258 8月  10 21:46 ..
drwxr-xr-x.  2 root   root     6 8月  11 03:36 hgfs
drwxrwxrwx   6 nobody nobody  61 8月  18 22:37 nfs
192.168.3.102 | CHANGED | rc=0 >>
总用量 0
drwxr-xr-x.  5 root   root    58 8月  20 15:11 .
dr-xr-xr-x. 18 root   root   258 8月  19 01:46 ..
drwxr-xr-x.  2 root   root     6 8月  11 01:50 hgfs
drwxrwxrwx   6 nobody nobody  61 8月  18 22:37 nfs
drwxr-xr-x   3 root   root    17 8月  20 15:18 volumes-back-20210820
[root@master01 ~]# ansible all -a 'chdir=/mnt creates=/mnt/volumes-back-20210820 ls -al'
192.168.3.101 | CHANGED | rc=0 >>
总用量 0
drwxr-xr-x.  4 root   root    29 8月  11 20:04 .
dr-xr-xr-x. 18 root   root   258 8月  10 21:46 ..
drwxr-xr-x.  2 root   root     6 8月  11 03:36 hgfs
drwxrwxrwx   6 nobody nobody  61 8月  18 22:37 nfs
192.168.3.100 | CHANGED | rc=0 >>
总用量 0
drwxr-xr-x.  4 root   root    29 8月  11 11:57 .
dr-xr-xr-x. 20 root   root   281 8月  19 01:39 ..
drwxr-xr-x.  2 root   root     6 8月  10 18:57 hgfs
drwxrwxrwx   6 nobody nobody  61 8月  18 22:37 nfs
192.168.3.102 | SUCCESS | rc=0 >>
skipped, since /mnt/volumes-back-20210820 exists

2. shell模块,由于shell模块支持通配符,重定向,管道等,可以设置为默认模块,编辑:/etc/ansible/ansible.cfg

114 module_name = shell    #这样我们的默认模块就是shell了,使用时就不用指定-m了

shell的用法基本与command一致:

[root@master01 ~]# ansible all -a 'useradd test01'
192.168.3.100 | FAILED | rc=9 >>
useradd:用户“test01”已存在non-zero return code
192.168.3.101 | CHANGED | rc=0 >>

192.168.3.102 | CHANGED | rc=0 >>

[root@master01 ~]# ansible all -a 'ls -l /etc/shadow'
192.168.3.100 | CHANGED | rc=0 >>
---------- 1 root root 928 8月  14 15:51 /etc/shadow
192.168.3.101 | CHANGED | rc=0 >>
---------- 1 root root 899 8月  22 23:45 /etc/shadow
192.168.3.102 | CHANGED | rc=0 >>
---------- 1 root root 899 8月  22 23:45 /etc/shadow
[root@master01 ~]# ansible all -a 'echo centos|passwd --stdin test01'     #支持管道
192.168.3.100 | CHANGED | rc=0 >>
更改用户 test01 的密码 。
passwd:所有的身份验证令牌已经成功更新。
192.168.3.101 | CHANGED | rc=0 >>
更改用户 test01 的密码 。
passwd:所有的身份验证令牌已经成功更新。
192.168.3.102 | CHANGED | rc=0 >>
更改用户 test01 的密码 。
passwd:所有的身份验证令牌已经成功更新。

shell模块可以创建文件夹,文件,也可以同时修改文件属性,但是命令需要一条一条执行,多条命令放在一起,只会执行第1条命令:

[root@master01 ~]# ansible all -a 'chdir=/data touch f2.txt chown test01 f2.txt chmon 700 f2.txt'
[WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If you need to use command because file is insufficient you can
add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
192.168.3.101 | CHANGED | rc=0 >>

192.168.3.100 | CHANGED | rc=0 >>

192.168.3.102 | CHANGED | rc=0 >>

[root@master01 ~]# 
[root@master01 ~]# 
[root@master01 ~]# ansible all -a 'ls -l /data/f2.txt'
192.168.3.100 | CHANGED | rc=0 >>
-rw-r--r-- 1 root root 0 8月  22 23:54 /data/f2.txt
192.168.3.101 | CHANGED | rc=0 >>
-rw-r--r-- 1 root root 0 8月  22 23:54 /data/f2.txt
192.168.3.102 | CHANGED | rc=0 >>
-rw-r--r-- 1 root root 0 8月  22 23:54 /data/f2.txt
#  文件属性并没有改变
[root@master01 ~]# ansible all -a 'chmod 700 /data/f2.txt'
[WARNING]: Consider using the file module with mode rather than running 'chmod'.  If you need to use command because file is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
192.168.3.100 | CHANGED | rc=0 >>

192.168.3.101 | CHANGED | rc=0 >>

192.168.3.102 | CHANGED | rc=0 >>

[root@master01 ~]# ansible all -a 'ls -l /data/f2.txt'
192.168.3.100 | CHANGED | rc=0 >>
-rwx------ 1 test01 root 0 8月  22 23:54 /data/f2.txt
192.168.3.101 | CHANGED | rc=0 >>
-rwx------ 1 test01 root 0 8月  22 23:54 /data/f2.txt
192.168.3.102 | CHANGED | rc=0 >>
-rwx------ 1 test01 root 0 8月  22 23:54 /data/f2.txt

3. copy模块;用于拷贝ansible主机文件到被管理端;

group  #拷贝后的文件指定组
mode  #拷贝后的文件指定权限
owner  #指定拥有者
src       #源文件,即ansible主机文件
dest     #目标主机路径  如果src是一个目录,则dest必须是一个目录

用法:

[root@master01 ~]# ansible all -m copy -a 'src=/etc/redhat-release dest=/data/ owner=test01 group=test01 mode=770'
192.168.3.101 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "f0ac05b1248bc5fcb29fb5525f60d2a3cef303a7",
    "dest": "/data/redhat-release",
    "gid": 1000,
    "group": "test01",
    "md5sum": "e30ea93f603f64b25a826cb84fdcb3d3",
    "mode": "0770",
    "owner": "test01",
    "size": 30,
    "src": "/root/.ansible/tmp/ansible-tmp-1629648215.7165456-2370658-213108327814096/source",
    "state": "file",
    "uid": 1000
}
192.168.3.100 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "f0ac05b1248bc5fcb29fb5525f60d2a3cef303a7",
    "dest": "/data/redhat-release",
    "gid": 1001,
    "group": "test01",
    "md5sum": "e30ea93f603f64b25a826cb84fdcb3d3",
    "mode": "0770",
    "owner": "test01",
    "size": 30,
    "src": "/root/.ansible/tmp/ansible-tmp-1629648215.6977394-2370656-267621287342767/source",
    "state": "file",
    "uid": 1001
}
192.168.3.102 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "f0ac05b1248bc5fcb29fb5525f60d2a3cef303a7",
    "dest": "/data/redhat-release",
    "gid": 1000,
    "group": "test01",
    "md5sum": "e30ea93f603f64b25a826cb84fdcb3d3",
    "mode": "0770",
    "owner": "test01",
    "size": 30,
    "src": "/root/.ansible/tmp/ansible-tmp-1629648215.8831189-2370660-48157497330459/source",
    "state": "file",
    "uid": 1000
}
[root@master01 ~]# 
[root@master01 ~]# ansible all -a 'ls -l /data/redhat-release'
192.168.3.100 | CHANGED | rc=0 >>
-rwxrwx--- 1 test01 test01 30 8月  23 00:03 /data/redhat-release
192.168.3.101 | CHANGED | rc=0 >>
-rwxrwx--- 1 test01 test01 30 8月  23 00:03 /data/redhat-release
192.168.3.102 | CHANGED | rc=0 >>
-rwxrwx--- 1 test01 test01 30 8月  23 00:03 /data/redhat-release

4. fetch模块,与copy相反,将被管理端主机文件拷贝到ansible管理主机上。

dest    #指定ansible-server的文件路径,为文件夹
src     #指定源文件,即远程主机的文件
[root@master01 ~]# ansible all -m fetch -a 'src=/etc/redhat-release dest=/data/os'     #/data/os文件夹没有则会创建
192.168.3.100 | CHANGED => {
    "changed": true,
    "checksum": "f0ac05b1248bc5fcb29fb5525f60d2a3cef303a7",
    "dest": "/data/os/192.168.3.100/etc/redhat-release",
    "md5sum": "e30ea93f603f64b25a826cb84fdcb3d3",
    "remote_checksum": "f0ac05b1248bc5fcb29fb5525f60d2a3cef303a7",
    "remote_md5sum": null
}
192.168.3.101 | CHANGED => {
    "changed": true,
    "checksum": "f0ac05b1248bc5fcb29fb5525f60d2a3cef303a7",
    "dest": "/data/os/192.168.3.101/etc/redhat-release",
    "md5sum": "e30ea93f603f64b25a826cb84fdcb3d3",
    "remote_checksum": "f0ac05b1248bc5fcb29fb5525f60d2a3cef303a7",
    "remote_md5sum": null
}
192.168.3.102 | CHANGED => {
    "changed": true,
    "checksum": "f0ac05b1248bc5fcb29fb5525f60d2a3cef303a7",
    "dest": "/data/os/192.168.3.102/etc/redhat-release",
    "md5sum": "e30ea93f603f64b25a826cb84fdcb3d3",
    "remote_checksum": "f0ac05b1248bc5fcb29fb5525f60d2a3cef303a7",
    "remote_md5sum": null
}
[root@master01 ~]# tree /data/os/
/data/os/
├── 192.168.3.100
│   └── etc
│       └── redhat-release
├── 192.168.3.101
│   └── etc
│       └── redhat-release
└── 192.168.3.102
    └── etc
        └── redhat-release

6 directories, 3 files

5. unarchive模块,用于将ansible-server主机的打包文件传输到远程主机并完成解压;

copy     #默认为yes,即从server主机拷贝过去,如果为no,则为远程主机的文件进行解压
creates  #如果后面跟的文件已经存在了,就不执行
dest      #远程主机的解压路径
group    #解压后指定组
mode    #解压后指定权限
owner    #解压后指定用户
src        #源文件
[root@master01 ~]# tar -zcvf /data/etc.tar.gz /etc/sysconfig/  #产生压缩文件
[root@master01 ~]# ansible all -m unarchive -a 'src=/data/etc.tar.gz dest=/data/ owner=test01 mode=700 copy=yes'
[root@master01 ~]# ansible all -a 'ls -l /data/etc'
192.168.3.100 | CHANGED | rc=0 >>
总用量 4
drwx------ 5 test01 root 4096 8月  11 11:41 sysconfig
192.168.3.101 | CHANGED | rc=0 >>
总用量 4
drwx------ 5 test01 root 4096 8月  11 11:41 sysconfig
192.168.3.102 | CHANGED | rc=0 >>
总用量 4
drwx------ 5 test01 root 4096 8月  11 11:41 sysconfig

6. archive模块

dest   #目标文件
format  #压缩格式
group
mode
owner
path    #远程主机的绝对地址

实例如下:将 /etc/yum.repos.d/文件全部打包发回server主机:

[root@master01 ~]# ansible all -m archive -a 'path=/etc/yum.repos.d/ dest=/data/yum.tar.gz format=gz'
[root@master01 ~]# ansible all -a 'ls -l /data' |grep yum
-rw-r--r-- 1 root   root    2336 8月  23 00:25 yum.tar.gz
-rw-r--r-- 1 root   root   2337 8月  23 00:25 yum.tar.gz
-rw-r--r-- 1 root   root   1934 8月  23 00:25 yum.tar.gz

然后发回server主机:

[root@master01 ~]# ansible all -m fetch -a 'src=/data/yum.tar.gz dest=/data/yum'
192.168.3.100 | CHANGED => {
    "changed": true,
    "checksum": "c6a133568eb541b62ba1ae17e705d34c9587869d",
    "dest": "/data/yum/192.168.3.100/data/yum.tar.gz",
    "md5sum": "65fd881d8f3072ecd64aa0e8c8b52cb9",
    "remote_checksum": "c6a133568eb541b62ba1ae17e705d34c9587869d",
    "remote_md5sum": null
}
192.168.3.101 | CHANGED => {
    "changed": true,
    "checksum": "9662f9158cf80589282e75cc4ec86da760ecd189",
    "dest": "/data/yum/192.168.3.101/data/yum.tar.gz",
    "md5sum": "1c903d2142b493cd595147f3fa0748cd",
    "remote_checksum": "9662f9158cf80589282e75cc4ec86da760ecd189",
    "remote_md5sum": null
}
192.168.3.102 | CHANGED => {
    "changed": true,
    "checksum": "5bf5d482d23c92ee38c1d3fe048aed664e18feb9",
    "dest": "/data/yum/192.168.3.102/data/yum.tar.gz",
    "md5sum": "4a9c7f3b5e08371267e9eb499d4f92e1",
    "remote_checksum": "5bf5d482d23c92ee38c1d3fe048aed664e18feb9",
    "remote_md5sum": null
}
[root@master01 ~]# tree /data/yum/
/data/yum/
├── 192.168.3.100
│   └── data
│       └── yum.tar.gz
├── 192.168.3.101
│   └── data
│       └── yum.tar.gz
└── 192.168.3.102
    └── data
        └── yum.tar.gz

6 directories, 3 files

7. Script脚本模块:执行脚本

chdir    #在运行脚本之前cd到远程主机的一个目录下
creates  #远程主机上的文件存在时,则脚本不运行

如,copy一个server的脚本到远程主机并执行;

 1 [root@master01 data]# cat ./test.sh
 2 #!/bin/bash
 3 echo "welcome to my $HOSTNAME"
 4 然后拷贝到远程主机下
 5 [root@master01 data]# ansible all -m copy -a 'src=/data/test.sh dest=/data/test-1.sh mode=755'
 6 192.168.3.101 | CHANGED => {
 7     "ansible_facts": {
 8         "discovered_interpreter_python": "/usr/libexec/platform-python"
 9     },
10     "changed": true,
11     "checksum": "5cf8e18b77ffc1800c384568ac01edcf0b331726",
12     "dest": "/data/test-1.sh",
13     "gid": 0,
14     "group": "root",
15     "md5sum": "2502927a3954f0c5b314cf709609d698",
16     "mode": "0755",
17     "owner": "root",
18     "size": 43,
19     "src": "/root/.ansible/tmp/ansible-tmp-1629725696.280507-2399767-147506280008766/source",
20     "state": "file",
21     "uid": 0
22 }
23 192.168.3.100 | CHANGED => {
24     "ansible_facts": {
25         "discovered_interpreter_python": "/usr/libexec/platform-python"
26     },
27     "changed": true,
28     "checksum": "5cf8e18b77ffc1800c384568ac01edcf0b331726",
29     "dest": "/data/test-1.sh",
30     "gid": 0,
31     "group": "root",
32     "md5sum": "2502927a3954f0c5b314cf709609d698",
33     "mode": "0755",
34     "owner": "root",
35     "size": 43,
36     "src": "/root/.ansible/tmp/ansible-tmp-1629725696.273245-2399765-122019915986623/source",
37     "state": "file",
38     "uid": 0
39 }
40 192.168.3.102 | CHANGED => {
41     "ansible_facts": {
42         "discovered_interpreter_python": "/usr/libexec/platform-python"
43     },
44     "changed": true,
45     "checksum": "5cf8e18b77ffc1800c384568ac01edcf0b331726",
46     "dest": "/data/test-1.sh",
47     "gid": 0,
48     "group": "root",
49     "md5sum": "2502927a3954f0c5b314cf709609d698",
50     "mode": "0755",
51     "owner": "root",
52     "size": 43,
53     "src": "/root/.ansible/tmp/ansible-tmp-1629725696.3662632-2399769-53194866181495/source",
54     "state": "file",
55     "uid": 0
56 }
57 然后使用script模块执行:
58 [root@master01 data]# ansible all -m script -a '/data/test-1.sh'
59 192.168.3.100 | CHANGED => {
60     "changed": true,
61     "rc": 0,
62     "stderr": "Shared connection to 192.168.3.100 closed.\r\n",
63     "stderr_lines": [
64         "Shared connection to 192.168.3.100 closed."
65     ],
66     "stdout": "welcome to my master01\r\n",
67     "stdout_lines": [
68         "welcome to my master01"
69     ]
70 }
71 192.168.3.101 | CHANGED => {
72     "changed": true,
73     "rc": 0,
74     "stderr": "Shared connection to 192.168.3.101 closed.\r\n",
75     "stderr_lines": [
76         "Shared connection to 192.168.3.101 closed."
77     ],
78     "stdout": "welcome to my node01\r\n",
79     "stdout_lines": [
80         "welcome to my node01"
81     ]
82 }
83 192.168.3.102 | CHANGED => {
84     "changed": true,
85     "rc": 0,
86     "stderr": "Shared connection to 192.168.3.102 closed.\r\n",
87     "stderr_lines": [
88         "Shared connection to 192.168.3.102 closed."
89     ],
90     "stdout": "welcome to my node02\r\n",
91     "stdout_lines": [
92         "welcome to my node02"
93     ]
94 }

8. file模块:

group    #指定组名
mode    #指定权限
owner    #指定拥有者
path     # (required) 
state    # absent、diff、directory、

删除一个文件:

  1 [root@master01 data]# ansible all -a 'ls /data'
  2 192.168.3.100 | CHANGED | rc=0 >>
  3 700
  4 chmon
  5 chown
  6 database
  7 etc
  8 etc.tar.gz
  9 f1.txt
 10 f2.txt
 11 os
 12 redhat-release
 13 test01
 14 test-1.sh
 15 test.sh
 16 yum
 17 yum.tar.gz
 18 192.168.3.101 | CHANGED | rc=0 >>
 19 700
 20 chmon
 21 chown
 22 etc
 23 f1.txt
 24 f2.txt
 25 redhat-release
 26 test01
 27 test-1.sh
 28 yum.tar.gz
 29 192.168.3.102 | CHANGED | rc=0 >>
 30 700
 31 chmon
 32 chown
 33 etc
 34 f1.txt
 35 f2.txt
 36 redhat-release
 37 test01
 38 test-1.sh
 39 yum.tar.gz
 40 [root@master01 data]# ansible all -m file -a 'path=/data/700 state=absent'    #删除data下700文件
 41 192.168.3.100 | CHANGED => {
 42     "ansible_facts": {
 43         "discovered_interpreter_python": "/usr/libexec/platform-python"
 44     },
 45     "changed": true,
 46     "path": "/data/700",
 47     "state": "absent"
 48 }
 49 192.168.3.101 | CHANGED => {
 50     "ansible_facts": {
 51         "discovered_interpreter_python": "/usr/libexec/platform-python"
 52     },
 53     "changed": true,
 54     "path": "/data/700",
 55     "state": "absent"
 56 }
 57 192.168.3.102 | CHANGED => {
 58     "ansible_facts": {
 59         "discovered_interpreter_python": "/usr/libexec/platform-python"
 60     },
 61     "changed": true,
 62     "path": "/data/700",
 63     "state": "absent"
 64 }
 65 [root@master01 data]# ansible all -a 'ls /data'
 66 192.168.3.100 | CHANGED | rc=0 >>
 67 chmon
 68 chown
 69 database
 70 etc
 71 etc.tar.gz
 72 f1.txt
 73 f2.txt
 74 os
 75 redhat-release
 76 test01
 77 test-1.sh
 78 test.sh
 79 yum
 80 yum.tar.gz
 81 192.168.3.101 | CHANGED | rc=0 >>
 82 chmon
 83 chown
 84 etc
 85 f1.txt
 86 f2.txt
 87 redhat-release
 88 test01
 89 test-1.sh
 90 yum.tar.gz
 91 192.168.3.102 | CHANGED | rc=0 >>
 92 chmon
 93 chown
 94 etc
 95 f1.txt
 96 f2.txt
 97 redhat-release
 98 test01
 99 test-1.sh
100 yum.tar.gz

9. hostname模块:改主机名

name   #指定主机名为什么,不要同时修改所有机器的主机名,这样所有机器的主机名都一致了

10. cron模块:计划任务模块

minute    #分( 0-59, *, */2, etc )
hour       #时( 0-23, *, */2, etc )
day        #日( 1-31, *, */2, etc )
month    #月( 1-12, *, */2, etc )
weekday  #周   0-6 for Sunday-Saturday
name      #命名,可以在state中条用删除该任务
state       #present or absent 创建删除
user        #周期任务的用户
job #执行一个命令

实例:创建一个计划任务,周一至周五每天凌晨4:30分执行一个脚本

 1 [root@master01 data]# ansible all -m cron -a 'minute=30 hour=4 weekday=1-5 name=echo job=./data/test-1.sh'
 2 192.168.3.100 | CHANGED => {
 3     "ansible_facts": {
 4         "discovered_interpreter_python": "/usr/libexec/platform-python"
 5     },
 6     "changed": true,
 7     "envs": [],
 8     "jobs": [
 9         "echo"
10     ]
11 }
12 192.168.3.101 | CHANGED => {
13     "ansible_facts": {
14         "discovered_interpreter_python": "/usr/libexec/platform-python"
15     },
16     "changed": true,
17     "envs": [],
18     "jobs": [
19         "echo"
20     ]
21 }
22 192.168.3.102 | CHANGED => {
23     "ansible_facts": {
24         "discovered_interpreter_python": "/usr/libexec/platform-python"
25     },
26     "changed": true,
27     "envs": [],
28     "jobs": [
29         "echo"
30     ]
31 }
32 [root@master01 data]# ansible all -a 'crontab -l'
33 192.168.3.100 | CHANGED | rc=0 >>
34 #Ansible: echo
35 30 4 * * 1-5 ./data/test-1.sh
36 192.168.3.101 | CHANGED | rc=0 >>
37 #Ansible: echo
38 30 4 * * 1-5 ./data/test-1.sh
39 192.168.3.102 | CHANGED | rc=0 >>
40 #Ansible: echo
41 30 4 * * 1-5 ./data/test-1.sh

11. yum模块:支持redhat,centos版本

name       #安装包的名称
state        # install (`present' or `installed', `latest'), or remove (`absent' or `removed') 
skip_broken  #跳过依赖或损坏
list      # yum list --show-duplicates <package> 
download_dir
download_only
disablerepo
enablerepo

例如:

[root@master01 data]# ansible all -m yum -a 'list=repos'   #查询主机有哪些源可以用,状态时enable还是什么
192.168.3.100 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "msg": "",
    "results": [
        {
            "repoid": "base",
            "state": "enabled"
        },
        {
            "repoid": "extras",
            "state": "enabled"
        },
        {
            "repoid": "AppStream",
            "state": "enabled"
        },
        {
            "repoid": "docker-ce-stable",
            "state": "enabled"
        },
        {
            "repoid": "epel",
            "state": "enabled"
        },
        {
            "repoid": "kubernetes",
            "state": "enabled"
        }
    ]
}

[root@master01 data]# ansible all -m yum -a 'name=httpd state=installed'   #yum安装http服务
192.168.3.100 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
"Installed: httpd-2.4.37-39.module_el8.4.0+778+c970deab.x86_64",
"Installed: httpd-filesystem-2.4.37-39.module_el8.4.0+778+c970deab.noarch",
"Installed: mod_http2-1.15.7-3.module_el8.4.0+778+c970deab.x86_64",
"Installed: httpd-tools-2.4.37-39.module_el8.4.0+778+c970deab.x86_64",
"Installed: centos-logos-httpd-85.8-1.el8.noarch",
"Installed: apr-1.6.3-11.el8.x86_64",
"Installed: apr-util-1.6.1-6.el8.x86_64",
"Installed: apr-util-bdb-1.6.1-6.el8.x86_64"
]
}

12. replace:替换模块,实现文件替换

path       #文件(required)
regexp    #(required)匹配的正则表达式
replace   #替换成的内容

替换httpd服务的配置文件监听的80端口为1080

 1 [root@master01 data]# ansible all -m replace -a 'path=/etc/httpd/conf/httpd.conf regexp="Listen 80" replace="Listen 1080"'
 2 192.168.3.100 | CHANGED => {
 3     "ansible_facts": {
 4         "discovered_interpreter_python": "/usr/libexec/platform-python"
 5     },
 6     "changed": true,
 7     "msg": "1 replacements made"
 8 }
 9 192.168.3.101 | CHANGED => {
10     "ansible_facts": {
11         "discovered_interpreter_python": "/usr/libexec/platform-python"
12     },
13     "changed": true,
14     "msg": "1 replacements made"
15 }
16 192.168.3.102 | CHANGED => {
17     "ansible_facts": {
18         "discovered_interpreter_python": "/usr/libexec/platform-python"
19     },
20     "changed": true,
21     "msg": "1 replacements made"
22 }
23 [root@master01 data]# ansible all -a 'cat /etc/httpd/conf/httpd.conf |grep ^Listen'
24 192.168.3.101 | CHANGED | rc=0 >>
25 Listen 1080
26 192.168.3.100 | CHANGED | rc=0 >>
27 Listen 1080
28 192.168.3.102 | CHANGED | rc=0 >>
29 Listen 1080

13.service模块:启动服务

启动服务可以使用shell模块:

[root@master01 data]# ansible all -a 'systemctl start httpd'
192.168.3.101 | CHANGED | rc=0 >>

192.168.3.100 | CHANGED | rc=0 >>

192.168.3.102 | CHANGED | rc=0 >>

[root@master01 data]# ansible all -a 'ss -tnl|grep 1080'
192.168.3.100 | CHANGED | rc=0 >>
LISTEN 0      128          0.0.0.0:31080      0.0.0.0:*          
LISTEN 0      128                *:1080             *:*          
192.168.3.101 | CHANGED | rc=0 >>
LISTEN 0      128          0.0.0.0:31080      0.0.0.0:*          
LISTEN 0      128                *:1080             *:*          
192.168.3.102 | CHANGED | rc=0 >>
LISTEN 0      128          0.0.0.0:31080      0.0.0.0:*          
LISTEN 0      128                *:1080             *:*  

可以是使用service模块

name      # (required) 服务名称
enabled   #是否开机启动
state       #`started'/`stopped'
ansible all -m service -a 'name=httpd state=stopped'
[root@master01 data]# ansible all -a 'ss -tnl|grep -w 1080'
192.168.3.100 | FAILED | rc=1 >>
non-zero return code
192.168.3.101 | FAILED | rc=1 >>
non-zero return code
192.168.3.102 | FAILED | rc=1 >>
non-zero return code
再次启动并设置为开机自启
ansible all -m service -a 'name=httpd state=started enabled=yes'
[root@master01 data]# ansible all -a 'ss -tnl|grep -w 1080'
192.168.3.100 | CHANGED | rc=0 >>
LISTEN 0      128                *:1080             *:*          
192.168.3.101 | CHANGED | rc=0 >>
LISTEN 0      128                *:1080             *:*          
192.168.3.102 | CHANGED | rc=0 >>
LISTEN 0      128                *:1080             *:*  
通过查看确实是开机自启动的了
[root@master01 data]# ansible all -a 'systemctl list-unit-files|grep httpd.service'
192.168.3.101 | CHANGED | rc=0 >>
httpd.service                                                          enabled  
192.168.3.100 | CHANGED | rc=0 >>
httpd.service                                                          enabled  
192.168.3.102 | CHANGED | rc=0 >>
httpd.service                                                          enabled  

14.user模块:

15.group模块:

16. lineinfile:替换模块,相当于sed命令,修改文件内容

17. setup模块:收集主机信息,

filter   #指定显示某些特性,支持通配符
[root@master01 data]# ansible all -m setup
过滤下cpu的一些选项
[root@master01 data]# ansible all -m setup |grep cpu
        "ansible_processor_vcpus": 4,
        "ansible_processor_vcpus": 4,
        "ansible_processor_vcpus": 4,
然后使用filter单独查看这些特性
[root@master01 data]# ansible all -m setup -a 'filter=ansible_processor_vcpus'
192.168.3.101 | SUCCESS => {
    "ansible_facts": {
        "ansible_processor_vcpus": 4,
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
192.168.3.100 | SUCCESS => {
    "ansible_facts": {
        "ansible_processor_vcpus": 4,
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
192.168.3.102 | SUCCESS => {
    "ansible_facts": {
        "ansible_processor_vcpus": 4,
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}