Ansible批量管理自动化工具-学习笔记

Ansible批量管理自动化工具

1 Ansible简介

1.1 ansible简介

  • 批量管理服务器的工具
  • 无需部署agent,通过ssh进行管理
  • 流行的自动化运维工具:https://github.com/ansible/ansible

1.2 检查安装环境

[root@ansible ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)

[root@ansible ~]# uname -r
3.10.0-1062.4.3.el7.x86_64

#python版本需要2.6以上,不过通过centos7都会默认安装上python2.7.5
[root@ansible ~]# python -V
Python 2.7.5

2 配置公私钥实现ssh无密码登陆

  • ansible是无agent的,无agent是怎么批量管理服务器的?主要是借用ssh来批量管理服务器。
  • ssh默认登陆是需要密码的,所以管理起来比较麻烦,这节课主要是介绍ssh的无密码登陆。
  • ssh无密码登陆实现以后,使用ansible批量管理服务器就变得简单了
主机名 IP
ansible 192.168.200.27
web01 192.168.200.28
web02 192.168.200.29

2.1生成密钥对

[root@ansible ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ""
Generating public/private rsa key pair.
Created directory '/root/.ssh'.
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:g2QJQiBD4PRiGYf1ARed89NGH4CnceXzRP/VCJEJq9w root@ansible
The key's randomart image is:
+---[RSA 2048]----+
|B*=+o+o . oo== . |
|+o=.o..= o =+o..o|
| = . .+ o O .oo.+|
|. .  o o * o .+ o|
|      . S E    ..|
|         .       |
|                 |
|                 |
|                 |
+----[SHA256]-----+

2.2 分别分发密钥到Web01与Web02上

[root@ansible ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 192.168.200.28
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password: 123.123

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh -o ' StrictHostKeyChecking=no' '192.168.200.28'"
and check to make sure that only the key(s) you wanted were added.

2.3 进行免密码登陆测试

[root@ansible ~]# hostname -I
192.168.200.27 
[root@ansible ~]# ssh 192.168.200.28
Last login: Sun Dec 22 18:28:34 2019 from 192.168.200.1
[root@web01 ~]# hostname -I
192.168.200.28 

3. 安装Ansible

3.1 yum安装ansible软件

[root@ansible ~]# yum -y install ansible

#查看ansible的版本
[root@ansible ~]# ansible --version
ansible 2.4.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]

3.2 修改ansible的配置文件(单节点)

[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# vim hosts
[root@ansible ansible]# cat hosts
[Web01]
192.168.200.28
[Web02]
192.168.200.29

3.3 进行ansible远程执行命令测试

[root@ansible ansible]# ansible Web01 -m ping
192.168.200.28 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

[root@ansible ansible]# ansible Web02 -m ping
192.168.200.29 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
[root@ansible ansible]# ansible Web01 -a "uname -r"
192.168.200.28 | SUCCESS | rc=0 >>
3.10.0-1062.el7.x86_64

3.4 修改ansible的配置文件(多节点)

[root@ansible ansible]# vim hosts
[root@ansible ansible]# tail -3 hosts
[all]
192.168.200.28
192.168.200.29

3.5 进行ansible远程执行命令测试

[root@ansible ansible]# ansible all -a "uname -r"
192.168.200.28 | SUCCESS | rc=0 >>
3.10.0-1062.el7.x86_64

192.168.200.29 | SUCCESS | rc=0 >>
3.10.0-1062.4.3.el7.x86_64

4. Ansible的命令模块

4.1 ansible模块command(不支持管道,不支持重定向,不建议使用)

参数-m:模块名
参数-a:直接跟命令(不支持命令缩写)

#command支持直接回显命令的执行结果
[root@ansible ~]# ansible all -m command -a "pwd"
192.168.200.28 | SUCCESS | rc=0 >>
/root

192.168.200.29 | SUCCESS | rc=0 >>
/root
#command模块不支持重定向操作
[root@ansible ~]# ansible all -m command -a "echo ywb >> /tmp/hyx"
192.168.200.28 | SUCCESS | rc=0 >>
ywb >> /tmp/hyx

192.168.200.29 | SUCCESS | rc=0 >>
ywb >> /tmp/hyx
#command模块不支持管道符操作
[root@ansible ~]# ansible all -m command -a "echo ywb | grep w"
192.168.200.28 | SUCCESS | rc=0 >>
ywb | grep w

192.168.200.29 | SUCCESS | rc=0 >>
ywb | grep w
[root@ansible ~]# ansible all -a 'tail -1 /etc/passwd'
192.168.200.29 | SUCCESS | rc=0 >>
chrony:x:998:996::/var/lib/chrony:/sbin/nologin

192.168.200.28 | SUCCESS | rc=0 >>
chrony:x:998:996::/var/lib/chrony:/sbin/nologin

4.2 ansible模块shell(支持管道,支持重定向)

#shell支持重定向
[root@ansible ~]# ansible all -m shell -a "echo ywb >> /tmp/hyx"
192.168.200.29 | SUCCESS | rc=0 >>

192.168.200.28 | SUCCESS | rc=0 >>
#shell模块支持管道符
[root@ansible ~]# ansible all -m shell -a "echo ywb | grep w"
192.168.200.28 | SUCCESS | rc=0 >>
ywb

192.168.200.29 | SUCCESS | rc=0 >>
ywb

4.3 ping模块

#ping,模块是用来检测指定主机的连通性。
[root@ansible ~]# ansible all -m ping
192.168.200.28 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.200.29 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

4.4 cron模块

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

#添加任务计划
[root@ansible ~]# ansible Web01 -m cron -a 'minute="*/1" job="/bin/echo heihei" name="ceshi"'
192.168.200.28 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "ceshi"
    ]
}

#检查是否添加成功
[root@ansible ~]# ansible Web01 -a "crontab -l"
192.168.200.28 | SUCCESS | rc=0 >>
#Ansible: ceshi
*/1 * * * * /bin/echo heihei

#查看Wbe01组的主机查看
[root@ansible ~]# ansible Web01 -a "tail -2 /var/spool/mail/root "
192.168.200.28 | SUCCESS | rc=0 >>
heihei
#移除任务计划
[root@ansible ~]# ansible Web01 -m cron -a 'name="ceshi" state=absent'
192.168.200.28 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}

#检查定时任务是否移除成功
[root@ansible ~]# ansible Web01 -a "crontab -l"
192.168.200.28 | SUCCESS | rc=0 >>

4.5 user模块

user模块用于创建新用户和更改、删除已存在的用户。其中name选项用来指明创建的用户名称。请求的是useradd, userdel, usermod三个指令

#创建用户
[root@ansible ~]# ansible Web01 -m user -a 'name="ywb"'
192.168.200.28 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 1000, 
    "home": "/home/ywb", 
    "name": "ywb", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1000
}

#检查用户是否创建成功
[root@ansible ~]# ansible Web01 -a 'id ywb'
192.168.200.28 | SUCCESS | rc=0 >>
uid=1000(ywb) gid=1000(ywb) 组=1000(ywb)
#删除用户
[root@ansible ~]# ansible Web01 -m user -a 'name="ywb" state=absent'
192.168.200.28 | SUCCESS => {
    "changed": true, 
    "force": false, 
    "name": "ywb", 
    "remove": false, 
    "state": "absent"
}

#检查用户是否删除成功
[root@ansible ~]# ansible Web01 -a 'id ywb'
192.168.200.28 | FAILED | rc=1 >>
id: ywb: no such usernon-zero return code

4.6 group模块

group模块用于对用户组进行管理。请求的是groupadd, groupdel, groupmod 三个指令。

#创建用户组
[root@ansible ~]# ansible Web01 -m group -a 'name=hyx gid=306 system=yes'
192.168.200.28 | SUCCESS => {
    "changed": true, 
    "gid": 306, 
    "name": "hyx", 
    "state": "present", 
    "system": true
}

#检查是否创建成功
[root@ansible ~]# ansible Web01 -a 'tail -1 /etc/group'
192.168.200.28 | SUCCESS | rc=0 >>
hyx:x:306:
#将用户ywb添加到hyx组中
[root@ansible ~]# ansible Web01 -m user -a 'name=ywb uid=306 system=yes group=hyx'
192.168.200.28 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 306, 
    "home": "/home/ywb", 
    "name": "ywb", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": true, 
    "uid": 306
}

#查看用户ywb是否添加到hyx组中
[root@ansible ~]# ansible Web01 -a 'tail -1 /etc/passwd'
192.168.200.28 | SUCCESS | rc=0 >>
ywb:x:306:306::/home/ywb:/bin/bash

4.7 copy模块

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

#将本地文件/etc/fstab复制到被管理主机上的/tmp/fstab.bak,将所有者设置为root,权限设置为644.
[root@ansible ~]# ansible Web01 -m copy -a 'src=/etc/fstab dest=/tmp/fstab.bak owner=root mode=644'
192.168.200.28 | SUCCESS => {
    "changed": true, 
    "checksum": "aa95f6ca1f678dcfadb671a29a3cfcc5610ac5b9", 
    "dest": "/tmp/fstab.bak", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "6bd26227e6a9b79520d2e4c55e232780", 
    "mode": "0644", 
    "owner": "root", 
    "size": 473, 
    "src": "/root/.ansible/tmp/ansible-tmp-1578033751.3-3069697382672/source", 
    "state": "file", 
    "uid": 0
}

#查看是否存在复制的文件
[root@ansible ~]# ansible Web01 -a "ls -d /tmp/fstab.bak"
192.168.200.28 | SUCCESS | rc=0 >>
/tmp/fstab.bak
#将hello写入/tmp/fstab.back
[root@ansible ~]# ansible Web01 -m copy -a 'content="hello" dest=/tmp/fstab.bak'
192.168.200.28 | SUCCESS => {
    "changed": true, 
    "checksum": "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d", 
    "dest": "/tmp/fstab.bak", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "5d41402abc4b2a76b9719d911017c592", 
    "mode": "0644", 
    "owner": "root", 
    "size": 5, 
    "src": "/root/.ansible/tmp/ansible-tmp-1578033973.42-101266898343089/source", 
    "state": "file", 
    "uid": 0
}

#查看内容是否写入
[root@ansible ~]# ansible Web01 -a 'cat /tmp/fstab.bak'
192.168.200.28 | SUCCESS | rc=0 >>
hello

4.8 file模块

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

#设置文件/tmp/fstab.back的所属主为ywb,所属组为hyx,权限为755.
[root@ansible ~]# ansible Web01 -m file -a 'owner=ywb group=hyx mode=755 path=/tmp/fstab.bak'
192.168.200.28 | SUCCESS => {
    "changed": true, 
    "gid": 306, 
    "group": "hyx", 
    "mode": "0755", 
    "owner": "ywb", 
    "path": "/tmp/fstab.bak", 
    "size": 5, 
    "state": "file", 
    "uid": 306
}

#查看文件的属性
[root@ansible ~]# ansible Web01 -a 'ls -l /tmp/fstab.bak'
192.168.200.28 | SUCCESS | rc=0 >>
-rwxr-xr-x 1 ywb hyx 5 1月   3 14:46 /tmp/fstab.bak
#设置/tmp/fstab.link为/tmp/fstab.bak的链接文件
[root@ansible ~]# ansible Web01 -m file -a 'path=/tmp/fstab.bak.link src=/tmp/fstab.bak state=link'
192.168.200.28 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/fstab.bak.link", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "size": 14, 
    "src": "/tmp/fstab.bak", 
    "state": "link", 
    "uid": 0
}

#检查软连接是否创建成功
[root@ansible ~]# ansible Web01 -a 'ls -l /tmp/fstab.bak /tmp/fstab.bak.link'
192.168.200.28 | SUCCESS | rc=0 >>
-rwxr-xr-x 1 ywb  hyx   5 1月   3 14:46 /tmp/fstab.bak
lrwxrwxrwx 1 root root 14 1月   4 17:51 /tmp/fstab.bak.link -> /tmp/fstab.bak
#删除文件/tmp/fstab.bak
[root@ansible ~]# ansible Web01 -m file -a "path=/tmp/fstab.bak state=absent"
192.168.200.28 | SUCCESS => {
    "changed": true, 
    "path": "/tmp/fstab.bak", 
    "state": "absent"
}
#创建文件/tmp/test
[root@ansible ~]# ansible Web01 -m file -a "path=/tmp/test state=touch"
192.168.200.28 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/test", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}

4.9 script模块

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

#例如,编辑一个本地脚本test.sh,复制到被管理主机上运行。
[root@ansible ~]# vim /tmp/test.sh
[root@ansible ~]# cat /tmp/test.sh 
#!/bin/bash
echo "hello" > /tmp/script.txt
[root@ansible ~]# chmod +x /tmp/test.sh
[root@ansible ~]# ll -d /tmp/test.sh 
-rwxr-xr-x 1 root root 43 1月   4 18:04 /tmp/test.sh

[root@ansible ~]# ansible Web01 -m script -a '/tmp/test.sh'
192.168.200.28 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.200.28 closed.\r\n", 
    "stdout": "", 
    "stdout_lines": []
}

#查看脚本实现
[root@ansible ~]# ansible Web01 -a "cat /tmp/script.txt"
192.168.200.28 | SUCCESS | rc=0 >>
hello

4.10 ansible模块raw,最原始的方式运行命令(不依赖python,仅通过ssh实现)

#清除yum缓存
[root@ansible ~]# ansible all -m raw -a "yum -y clean all"
192.168.200.29 | SUCCESS | rc=0 >>
已加载插件:fastestmirror
正在清理软件源: base extras updates
Shared connection to 192.168.200.29 closed.


192.168.200.28 | SUCCESS | rc=0 >>
已加载插件:fastestmirror
正在清理软件源: base extras updates
Shared connection to 192.168.200.28 closed.
#建立yum缓存
[root@ansible ~]# ansible all -m raw -a "yum makecache"
192.168.200.29 | SUCCESS | rc=0 >>
已加载插件:fastestmirror
Determining fastest mirrors
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirror.bit.edu.cn
base                                                     | 3.6 kB     00:00     
extras                                                   | 2.9 kB     00:00     
updates                                                  | 2.9 kB     00:00     
(1/10): base/7/x86_64/group_gz                             | 165 kB   00:00     
(2/10): extras/7/x86_64/filelists_db                       | 207 kB   00:01     
(3/10): extras/7/x86_64/other_db                           | 100 kB   00:00     
(4/10): extras/7/x86_64/primary_db                         | 153 kB   00:01     
(5/10): base/7/x86_64/other_db                             | 2.6 MB   00:03     
(6/10): base/7/x86_64/primary_db                           | 6.0 MB   00:05     
(7/10): updates/7/x86_64/filelists_db                      | 3.3 MB   00:03     
(8/10): updates/7/x86_64/primary_db                        | 5.8 MB   00:07     
(9/10): base/7/x86_64/filelists_db                         | 7.3 MB   00:21     
(10/10): updates/7/x86_64/other_db                         | 363 kB   00:24     
元数据缓存已建立
Shared connection to 192.168.200.29 closed.


192.168.200.28 | SUCCESS | rc=0 >>
已加载插件:fastestmirror
Determining fastest mirrors
 * base: mirrors.neusoft.edu.cn
 * extras: mirrors.neusoft.edu.cn
 * updates: mirrors.njupt.edu.cn
base                                                     | 3.6 kB     00:00     
extras                                                   | 2.9 kB     00:00     
updates                                                  | 2.9 kB     00:00     
(1/10): extras/7/x86_64/filelists_db                       | 207 kB   00:01     
(2/10): base/7/x86_64/other_db                             | 2.6 MB   00:01     
(3/10): extras/7/x86_64/other_db                           | 100 kB   00:00     
(4/10): extras/7/x86_64/primary_db                         | 153 kB   00:00     
(5/10): base/7/x86_64/group_gz                             | 165 kB   00:04     
(6/10): updates/7/x86_64/other_db                          | 363 kB   00:00     
(7/10): base/7/x86_64/primary_db                           | 6.0 MB   00:04     
(8/10): updates/7/x86_64/filelists_db                      | 3.3 MB   00:07     
(9/10): updates/7/x86_64/primary_db                        | 5.8 MB   00:17     
(10/10): base/7/x86_64/filelists_db                        | 7.3 MB   00:39     
元数据缓存已建立
Shared connection to 192.168.200.28 closed.

4.11 yum模块

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

#安装zsh软件包(zsh命令参考链接https://zhuanlan.zhihu.com/p/63585679)
[root@ansible ~]# ansible Web01 -m yum -a 'name=zsh'
192.168.200.28 | SUCCESS => {
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.aliyun.com\n * extras: mirrors.huaweicloud.com\n * updates: mirrors.aliyun.com\nResolving Dependencies\n--> Running transaction check\n---> Package zsh.x86_64 0:5.0.2-33.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package        Arch              Version                 Repository       Size\n================================================================================\nInstalling:\n zsh            x86_64            5.0.2-33.el7            base            2.4 M\n\nTransaction Summary\n================================================================================\nInstall  1 Package\n\nTotal download size: 2.4 M\nInstalled size: 5.6 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : zsh-5.0.2-33.el7.x86_64                                      1/1 \n  Verifying  : zsh-5.0.2-33.el7.x86_64                                      1/1 \n\nInstalled:\n  zsh.x86_64 0:5.0.2-33.el7                                                     \n\nComplete!\n"
    ]
}

#查看是否安装成功
[root@ansible ~]# ansible Web01 -a 'rpm -q zsh'
 [WARNING]: Consider using yum, dnf or zypper module rather than running rpm

192.168.200.28 | SUCCESS | rc=0 >>
zsh-5.0.2-33.el7.x86_64
#卸载zsh软件包
[root@ansible ~]# ansible Web01 -m yum -a 'name=zsh state=absent'
192.168.200.28 | SUCCESS => {
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "已加载插件:fastestmirror\n正在解决依赖关系\n--> 正在检查事务\n---> 软件包 zsh.x86_64.0.5.0.2-33.el7 将被 删除\n--> 解决依赖关系完成\n\n依赖关系解决\n\n================================================================================\n Package       架构             版本                      源               大小\n================================================================================\n正在删除:\n zsh           x86_64           5.0.2-33.el7              @base           5.6 M\n\n事务概要\n================================================================================\n移除  1 软件包\n\n安装大小:5.6 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  正在删除    : zsh-5.0.2-33.el7.x86_64                                     1/1 \n  验证中      : zsh-5.0.2-33.el7.x86_64                                     1/1 \n\n删除:\n  zsh.x86_64 0:5.0.2-33.el7                                                     \n\n完毕!\n"
    ]
}

4.12 service模块

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

#启动httpd服务并设置开机自启动。
[root@ansible ~]# ansible Web01 -m service -a 'enabled=true name=httpd state=started'
192.168.200.28 | SUCCESS => {
    "changed": true, 
    "enabled": true, 
    "name": "httpd", 
    "state": "started", 
    "status": {
        "ActiveEnterTimestampMonotonic": "0", 
        "ActiveExitTimestampMonotonic": "0", 
        "ActiveState": "inactive", 
        "After": "tmp.mount nss-lookup.target -.mount system.slice network.target remote-fs.target systemd-journald.socket basic.target", 
        "AllowIsolate": "no", 
        "AmbientCapabilities": "0", 
#以下省略若干。。。
   

#查看httpd的状态
[root@ansible ~]# ansible Web01 -a 'systemctl status httpd'
192.168.200.28 | SUCCESS | rc=0 >>
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2020-01-05 16:28:58 CST; 1min 9s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 1673 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─1673 /usr/sbin/httpd -DFOREGROUND
           ├─1674 /usr/sbin/httpd -DFOREGROUND
           ├─1675 /usr/sbin/httpd -DFOREGROUND
           ├─1676 /usr/sbin/httpd -DFOREGROUND
           ├─1677 /usr/sbin/httpd -DFOREGROUND
           └─1678 /usr/sbin/httpd -DFOREGROUND

1月 05 16:28:52 web01 systemd[1]: Starting The Apache HTTP Server...
1月 05 16:28:56 web01 httpd[1673]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::20c:29ff:feb5:5280. Set the 'ServerName' directive globally to suppress this message
1月 05 16:28:58 web01 systemd[1]: Started The Apache HTTP Server.

4.13 setup模块

setup模块收集、查看被管理主机的facts。每个被管理主机在接并运行管理命令之前,都会将自己的相关信息(操作系统、IP地址)发送给控制主机

#例如,查看Web01组的facts信息
root@ansible ~]# ansible Web01 -m setup
192.168.200.28 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.200.28"
        ], 
        "ansible_all_ipv6_addresses": [
            "fe80::20c:29ff:feb5:5280"
        ], 
        "ansible_apparmor": {
            "status": "disabled"
        }, 
        "ansible_architecture": "x86_64", 
        "ansible_bios_date": "07/02/2015", 
        "ansible_bios_version": "6.00", 
        "ansible_cmdline": {
            "BOOT_IMAGE": "/vmlinuz-3.10.0-1062.el7.x86_64", 
            "LANG": "zh_CN.UTF-8", 
            "crashkernel": "auto", 
            "quiet": true, 
            "rd.lvm.lv": "centos_ywb/swap", 
            "rhgb": true, 
            "ro": true, 
            "root": "/dev/mapper/centos_ywb-root"
        }, 
#以下省略若干。。。

部分文献分享来自于:https://blog.51cto.com/11134648/2153774

猜你喜欢

转载自www.cnblogs.com/ywb123/p/12152817.html