自动化运维工具
Ansible
自动化运维工具有很多
- 1、Puppet:基于 Ruby 开发,采用 C/S 架构,扩展性强,基于 SSL,远程命令执行相对较弱。
- 2、SaltStack:基于 Python 开发,采用 C/S 架构,相对 puppet 更轻量级,配置语法使用 YAML,使配置脚本更简单,需要配置客户端以及服务器端。每台被控制节点需要安装agent。
- 3、Ansible:基于 Python开发,分布式,无需客户端,轻量级,配置语法使用YAML语言,更强的远程命令执行操作。
Ansible简介
ansible是新出现的自动化运维工具,基于Python开发,分布式,无需客户端,轻量级,实现了批量系统配置、批量程序部署、批量运行命令等功能,ansible是基于模块工作的,本身并没有批量部署的能力,真正具有批量部署的是ansible上所运行的模块,ansible只是提供一种框架。
Ansible特性
- 、no agents:不需要在被管控主机上安装任何客户端,更新时只需在操作机上进行一次更新即可(不用安装客户端,分布式的)。
- 、no server:无服务器端,使用时直接运行命令即可。
- 、modules in any languages:基于模块工作,可使用任意语言开发模块。
- 、yaml,not code:使用yaml语言定制剧本playbook。
- 、ssh by default:基于SSH工作。
- 、strong multi-tier solution:可实现多级指挥。
connection plugins:连接插件,负责和被监控端实现通信,默认使用SSH连接。
host inventory:主机清单,是一个配置文件里面定义监控的主机。
modules:模块,核心模块、command模块、自定义模块等。
plugins:modules功能的补充,包括连接插件,邮件插件等。
playbook:编排,定义 Ansible 多任务配置文件,非必需。
安装部署Ansible
在这里我们准备三台机器,分别是server(控制节点),web1(被控节点),web2(被控节点)
#准备三台机器分别关闭它们的防火墙和SELinux
[root@ansible-server] systemctl stop firewalld;setenforce 0
#并在三台机器的/etc/hosts文件中配置解析
[root@ansible-server] vim /etc/hosts
192.168.202.131 ansible-server
192.168.202.132 ansible-web1
192.168.202.133 ansible-web2
#配置秘钥对,采用公钥认证方式:控制节点需要发送ssh公钥给所有被控制节点
[root@ansible-server] ssh-keygen
[root@ansible-server] ssh-copy-id -i 192.168.202.132
#也可以把ip换成解析的ansible-web1(两台机器都要发)
除了ssh公钥认证的方式外,也可以用密码认证
[root@ansible-server] yum -y install ansible
#如果没有的话就先安装epel-release源
[root@ansible-server] ansible --version #查看版本及信息,如果报红则环境有问题安装失败
[root@ansible-server] rpm -qc ansible #查看配置文件
/etc/ansible/ansible.cfg #主配置文件
/etc/ansible/hosts #主机清单文件
/etc/ansible/ansible.cfg 主配置文件,主要设置一些ansible初始化的信息,比如日志存放路径、模块、等配置信息。
/etc/ansible/hosts 默认主机清单文件,定义管理的主机及主机组,默认路径。
[root@ansible-server] vim /etc/ansible/hosts
#单独定义主机的
ansible-web1 #单独定义一个主机
192.168.202.133 #如果未解析也可以写ip
#定义主机组批量管理的
#每个组内可以有一个到多个主机
#定义主机组用[]
[webgrp1] #组1
ansible-web1
[webgrp2] #组2
ansible-web2
#主机组内也可以包含其它组
[weball:children] #后跟children表示子组
webgrp1
webgrp2
#主机组指定变量
[weball:vars] #后跟vars设置变量
ansible_ssh_port=22
ansible_ssh_user=root
ansible_ssh_private_key_file=/root/.ssh/id_rsa
#ansible_ssh_pass=xiaobai123
#如果没有互传秘钥可以使用密码,使用密码就将ansible_ssh_private_key_file=/root/.ssh/id_rsa注释掉
#也可以自定义主机清单文件
#自定义的可以写在任意目录下
[root@ansible-server] vim /tmp/hostlist
[all]
ansible-web1
ansible-web2
[all:vars]
ansible_ssh_port=22
ansible_ssh_user=root
ansible_ssh_private_key_file=/root/.ssh/id_rsa
ansible常见的内置参数
参数 | 用途 |
---|---|
ansible_ssh_host | 定义hosts ssh地址 |
ansible_ssh_port | 定义hosts ssh端口 |
ansible_ssh_user | 定义hosts ssh认证用户 |
ansible_ssh_pass | 定义hosts ssh认证密码 |
ansible_sudo | 定义hosts sudo用户 |
ansible_sudo_pass | 定义hosts sudo密码 |
ansible_sudo_exe | 定义hosts sudo路径 |
ansible_connection | 定义hosts连接方式 |
anshible_ssh_private_key_file | 定义hosts私钥 |
ansible_ssh_shell_type | 定义hosts shell类型 |
ansible_python_interpreter | 定义hosts任务执行python路径 |
ansible_*_interpreter | 定义hosts其它语言解析路径 |
… | … |
查看主机组内的主机
#列出weball组里所有的主机
#如果不加参数,指定是默认主机清单文件
[root@ansible-server] ansible weball --list-hosts
hosts (2):
ansible-web1
ansible-web2
#加上-i参数后跟路径就会读取路径中配置的主机清单文件
[root@ansible-server] ansible -i /tmp/hostlist all --list-hosts
hosts (2):
ansible-web1
ansible-web2
Ansible操作
基本语法
ansible的基本语法
ansible 主机名或主机组或ip或别名等 -m 模块 -a ‘命令’
-m:指定模块
-a:传递给后面的命令
注:最后可以跟-o
-o:友好显示,显示一行
[root@ansible-server] ansible weball -m ping -o #检测能否ping通
ansible-web1 | SUCCESS => {"changed": false, "ping": "pong"}
ansible-web2 | SUCCESS => {"changed": false, "ping": "pong"}
#我们在server端用ansible对ansible-web1执行的操作
[root@ansible-server] ansible ansible-web1 -m shell -a 'uptime'
ansible-web1 | SUCCESS | rc=0 >>
18:37:01 up 9:17, 3 users, load average: 0.00, 0.01, 0.05
#现在我们切换到ansible-web1上执行uptime命令
[root@ansible-web1] uptime
18:39:56 up 9:20, 2 users, load average: 0.00, 0.01, 0.05
#可以看到执行的效果是一样的,由于数据实时更新所以数据会不同
指定多台机器时用逗号分割,或者将之定义到一个主机组内
重定向输入到本地文件中
[root@ansible-server] ansible ansible-web1 -m shell -a 'df -Th' > /opt/a.txt #敲回车后不会有任何输出
#执行后我们换到ansible-web1上查看一下
[root@ansible-web1] cat /opt/a.txt
cat: /opt/a.txt: No such file or directory
#ansible-web1上并没有a.txt
#我们换回到server端
[root@ansible-server] cat /opt/a.txt
ansible-web1 | SUCCESS | rc=0 >>
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 17G 1.2G 16G 7% /
devtmpfs devtmpfs 475M 0 475M 0% /dev
tmpfs tmpfs 487M 0 487M 0% /dev/shm
tmpfs tmpfs 487M 7.7M 479M 2% /run
tmpfs tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 133M 882M 14% /boot
tmpfs tmpfs 98M 0 98M 0% /run/user/0
模块
ansible是基于模块工作的,它上面有很多模块
[root@ansible-server] ansible-doc -l #查看所有模块
[root@ansible-server] ansible-doc -s yum #查看yum模块的使用信息
copy模块
#远程复制备份模块:copy
参数详解
src=:指定源文件路径
dest=:目标地址
owner=:指定属主
group=:指定属组
mode=:指定权限
backup=:如果目标地址已存在这个文件则会查看文件,文件有变化将原文件备份,无变化不做备份,备份文件包含时间信息,选项为yes|no
[root@ansible-server] vim /root/a.txt #在server端创建a.txt
xiaobai #在文件中写入xiaobai
#切换到web1上查看/opt/下有没有a.txt文件
[root@ansible-web1] cat /opt/a.txt
cat: /opt/a.txt: No such file or directory
#可以看到web1的/opt/下是没有a.txt的
[root@ansible-server] ansible webgrp1 -m copy -a 'src=/root/a.txt dest=/opt/ owner=root group=root mode='644' backup=yes' -o
ansible-web1 | SUCCESS => {"changed": true, "checksum": "78926f3d105557525a9eb61e636b60ad9397fcc6", "dest": "/opt/a.txt", "gid": 0, "group": "root", "md5sum": "311ba906b6e142ae9da8d6f725f38811", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:usr_t:s0", "size": 8, "src": "/root/.ansible/tmp/ansible-tmp-1585038219.01-9751441096135/source", "state": "file", "uid": 0}
#此时我们再切换到web1上查看
[root@ansible-web1] ll /opt/
total 4
-rw-r--r--. 1 root root 8 Mar 24 16:23 a.txt
[root@ansible-web1] cat /opt/a.txt
xiaobai
#这时候web1下已经有了a.txt文件,文件内容就是我们输入的xiaobai,属主属组为root,权限为644
#由于之前的/opt/下没有a.txt文件,所以备份开启也不备份
#我们再回到server端执行刚才的命令
[root@ansible-server] ansible webgrp1 -m copy -a 'src=/root/a.txt dest=/opt/ owner=root group=root mode='644' backup=yes' -o
ansible-web1 | SUCCESS => {"changed": false, "checksum": "78926f3d105557525a9eb61e636b60ad9397fcc6", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "path": "/opt/a.txt", "secontext": "system_u:object_r:usr_t:s0", "size": 8, "state": "file", "uid": 0}
#再次回到web1上查看有无备份文件
[root@ansible-web1] ls /opt/
a.txt
#这里我们可以看到,由于文件内容相同不进行备份
#我们先在web2的/opt/下创建一个a.txt文件
[root@ansible-web2] vim /opt/a.txt
little #在这个文件中写入little
#我们换回到server端执行命令
[root@ansible-server] ansible webgrp2 -m copy -a 'src=/root/a.txt dest=/opt/ owner=root group=root mode='644' backup=yes' -o
ansible-web2 | SUCCESS => {"backup_file": "/opt/a.txt.8548.2020-03-24@16:38:46~", "changed": true, "checksum": "78926f3d105557525a9eb61e636b60ad9397fcc6", "dest": "/opt/a.txt", "gid": 0, "group": "root", "md5sum": "311ba906b6e142ae9da8d6f725f38811", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:usr_t:s0", "size": 8, "src": "/root/.ansible/tmp/ansible-tmp-1585039124.97-73653420725579/source", "state": "file", "uid": 0}
#此时我们回到web2查看
[root@ansible-web2] ls /opt/
a.txt a.txt.8548.2020-03-24@16:38:46~
#可以看到这里有两个文件,分别查看
[root@ansible-web2] cat /opt/a.txt.8548.2020-03-24\@16\:38\:46~
little
[root@ansible-web2] cat /opt/a.txt
xiaobai
#长的为原文件,原文件被备份并加上了日期时间
yum模块
#软件包管理模块:yum
参数详解
name=:安装包名
state=absent:用于卸载安装包
state=latest:表示安装最新的
state=removed:表示卸载
#state=absent和state=removed都是卸载,用哪个都可以
#先切换到web1查看有没有下载httpd服务
[root@ansible-web1] rpm -qa |grep httpd
#没有任何反馈就是没下载
#回到server端
[root@ansible-server] ansible webgrp1 -m yum -a 'name=httpd state=latest' #要等一定时间
ansible-web1 | SUCCESS => {"changed": true, "msg": "", "rc": 0, "results": ["Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: mirrors.163.com
* updates: mirrors.163.com
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-90.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-90.el7.centos for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-5.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-90.el7.centos will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
httpd x86_64 2.4.6-90.el7.centos base 2.7 M
Installing for dependencies:
apr x86_64 1.4.8-5.el7 base 103 k
apr-util x86_64 1.5.2-6.el7 base 92 k
httpd-tools x86_64 2.4.6-90.el7.centos base 91 k
mailcap noarch 2.1.41-2.el7 base 31 k
Transaction Summary
================================================================================
Install 1 Package (+4 Dependent packages)
Total download size: 3.0 M\nInstalled size: 10 M\nDownloading packages:
--------------------------------------------------------------------------------
Total 148 kB/s | 3.0 MB 00:20
Running transaction check\nRunning transaction test
Transaction test succeeded\nRunning transaction
Installing : apr-1.4.8-5.el7.x86_64 1/5
Installing : apr-util-1.5.2-6.el7.x86_64 2/5
Installing : httpd-tools-2.4.6-90.el7.centos.x86_64 3/5
Installing : mailcap-2.1.41-2.el7.noarch 4/5
Installing : httpd-2.4.6-90.el7.centos.x86_64 5/5
Verifying : apr-1.4.8-5.el7.x86_64 1/5
Verifying : mailcap-2.1.41-2.el7.noarch 2/5
Verifying : httpd-tools-2.4.6-90.el7.centos.x86_64 3/5
Verifying : apr-util-1.5.2-6.el7.x86_64 4/5
Verifying : httpd-2.4.6-90.el7.centos.x86_64 5/5
Installed:
httpd.x86_64 0:2.4.6-90.el7.centos
Dependency Installed:
apr.x86_64 0:1.4.8-5.el7 apr-util.x86_64 0:1.5.2-6.el7
httpd-tools.x86_64 0:2.4.6-90.el7.centos mailcap.noarch 0:2.1.41-2.el7
Complete!
"]}
#安装成功,我们到web1上查看一下
[root@ansible-web1] rpm -qa |grep httpd
httpd-tools-2.4.6-90.el7.centos.x86_64
httpd-2.4.6-90.el7.centos.x86_64
#此时已经有了httpd,卸载的话用remove和absent都可以
service模块
#服务管理模块:service
参数详解
state=stopped:停止服务
state=restarted:重启服务
state=started:启动服务
enabled=yes:开机自启
enabled=no:开机关闭
[root@ansible-server] ansible webgrp1 -m service -a 'name=httpd state=started enabled=yes' -o
ansible-web1 | SUCCESS => {"changed": false, "enabled": true, "name": "httpd", "state": "started", "status": {"ActiveEnterTimestamp": "Tue 2020-03-24 18:15:11 CST", "ActiveEnterTimestampMonotonic": "32583844873", "ActiveExitTimestampMonotonic": "0", "ActiveState": "active", "After": "remote-fs.target network.target tmp.mount systemd-journald.socket basic.target -.mount nss-lookup.target system.slice", "AllowIsolate": "no", "AmbientCapabilities": "0", "AssertResult": "yes", "AssertTimestamp": "Tue 2020-03-24 18:15:11 CST", "AssertTimestampMonotonic": "32583797872", "Before": "multi-user.target shutdown.target", "BlockIOAccounting": "no", "BlockIOWeight": "18446744073709551615", "CPUAccounting": "no", "CPUQuotaPerSecUSec": "infinity", "CPUSchedulingPolicy": "0", "CPUSchedulingPriority": "0", "CPUSchedulingResetOnFork": "no", "CPUShares": "18446744073709551615", "CanIsolate": "no", "CanReload": "yes", "CanStart": "yes", "CanStop": "yes", "CapabilityBoundingSet": "18446744073709551615", "ConditionResult": "yes", "ConditionTimestamp": "Tue 2020-03-24 18:15:11 CST", "ConditionTimestampMonotonic": "32583797872", "Conflicts": "shutdown.target", "ControlGroup": "/system.slice/httpd.service", "ControlPID": "0", "DefaultDependencies": "yes", "Delegate": "no", "Description": "The Apache HTTP Server", "DevicePolicy": "auto", "Documentation": "man:httpd(8) man:apachectl(8)", "EnvironmentFile": "/etc/sysconfig/httpd (ignore_errors=no)", "ExecMainCode": "0", "ExecMainExitTimestampMonotonic": "0", "ExecMainPID": "9328", "ExecMainStartTimestamp": "Tue 2020-03-24 18:15:11 CST", "ExecMainStartTimestampMonotonic": "32583799031", "ExecMainStatus": "0", "ExecReload": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -k graceful ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }", "ExecStart": "{ path=/usr/sbin/httpd ; argv[]=/usr/sbin/httpd $OPTIONS -DFOREGROUND ; ignore_errors=no ; start_time=[Tue 2020-03-24 18:15:11 CST] ; stop_time=[n/a] ; pid=9328 ; code=(null) ; status=0/0 }", "ExecStop": "{ path=/bin/kill ; argv[]=/bin/kill -WINCH ${MAINPID} ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }", "FailureAction": "none", "FileDescriptorStoreMax": "0", "FragmentPath": "/usr/lib/systemd/system/httpd.service", "GuessMainPID": "yes", "IOScheduling": "0", "Id": "httpd.service", "IgnoreOnIsolate": "no", "IgnoreOnSnapshot": "no", "IgnoreSIGPIPE": "yes", "InactiveEnterTimestampMonotonic": "0", "InactiveExitTimestamp": "Tue 2020-03-24 18:15:11 CST", "InactiveExitTimestampMonotonic": "32583799088", "JobTimeoutAction": "none", "JobTimeoutUSec": "0", "KillMode": "control-group", "KillSignal": "18", "LimitAS": "18446744073709551615", "LimitCORE": "18446744073709551615", "LimitCPU": "18446744073709551615", "LimitDATA": "18446744073709551615", "LimitFSIZE": "18446744073709551615", "LimitLOCKS": "18446744073709551615", "LimitMEMLOCK": "65536", "LimitMSGQUEUE": "819200", "LimitNICE": "0", "LimitNOFILE": "4096", "LimitNPROC": "3795", "LimitRSS": "18446744073709551615", "LimitRTPRIO": "0", "LimitRTTIME": "18446744073709551615", "LimitSIGPENDING": "3795", "LimitSTACK": "18446744073709551615", "LoadState": "loaded", "MainPID": "9328", "MemoryAccounting": "no", "MemoryCurrent": "18446744073709551615", "MemoryLimit": "18446744073709551615", "MountFlags": "0", "Names": "httpd.service", "NeedDaemonReload": "no", "Nice": "0", "NoNewPrivileges": "no", "NonBlocking": "no", "NotifyAccess": "main", "OOMScoreAdjust": "0", "OnFailureJobMode": "replace", "PermissionsStartOnly": "no", "PrivateDevices": "no", "PrivateNetwork": "no", "PrivateTmp": "yes", "ProtectHome": "no", "ProtectSystem": "no", "RefuseManualStart": "no", "RefuseManualStop": "no", "RemainAfterExit": "no", "Requires": "basic.target -.mount", "RequiresMountsFor": "/var/tmp", "Restart": "no", "RestartUSec": "100ms", "Result": "success", "RootDirectoryStartOnly": "no", "RuntimeDirectoryMode": "0755", "SameProcessGroup": "no", "SecureBits": "0", "SendSIGHUP": "no", "SendSIGKILL": "yes", "Slice": "system.slice", "StandardError": "inherit", "StandardInput": "null", "StandardOutput": "journal", "StartLimitAction": "none", "StartLimitBurst": "5", "StartLimitInterval": "10000000", "StartupBlockIOWeight": "18446744073709551615", "StartupCPUShares": "18446744073709551615", "StatusErrno": "0", "StatusText": "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec", "StopWhenUnneeded": "no", "SubState": "running", "SyslogLevelPrefix": "yes", "SyslogPriority": "30", "SystemCallErrorNumber": "0", "TTYReset": "no", "TTYVHangup": "no", "TTYVTDisallocate": "no", "TasksAccounting": "no", "TasksCurrent": "18446744073709551615", "TasksMax": "18446744073709551615", "TimeoutStartUSec": "1min 30s", "TimeoutStopUSec": "1min 30s", "TimerSlackNSec": "50000", "Transient": "no", "Type": "notify", "UMask": "0022", "UnitFilePreset": "disabled", "UnitFileState": "enabled", "WantedBy": "multi-user.target", "Wants": "system.slice", "WatchdogTimestamp": "Tue 2020-03-24 18:15:11 CST", "WatchdogTimestampMonotonic": "32583844837", "WatchdogUSec": "0"}}
#我们切换到web1上查看
[root@ansible-web1] 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 Tue 2020-03-24 18:15:11 CST; 57s ago
file模块
#文件模块:file
参数详解
owner:修改属主
group:修改属组
mode:修改权限
path:修改文件的路径
recurse:递归设置的文件,只对目录有效,yes表示使用递归参数
state=touch:创建一个新的空文件
state=directory:创建一个新的目录,当目录存在时不会进行修改
#我们创建一个文件bai.txt属主为xiaobai
#先切换到web1
[root@xiaobai-web1] useradd xiaobai
[root@xiaobai-web1] id xiaobai
uid=1000(xiaobai) gid=1000(xiaobai) groups=1000(xiaobai)
#我们回到server端
[root@xiaobai-server] ansible webgrp1 -m file -a 'path=/tmp/bai.txt mode=777 owner=xiaobai state=touch' -o #不写属主属组的话默认为root
ansible-web1 | SUCCESS => {"changed": true, "dest": "/tmp/bai.txt", "gid": 0, "group": "root", "mode": "0777", "owner": "xiaobai", "secontext": "unconfined_u:object_r:user_tmp_t:s0", "size": 0, "state": "file", "uid": 1000}
#到web1上查看这个文件
[root@xiaobai-web1] ll /tmp
total 4
-rwxrwxrwx. 1 xiaobai root 0 Mar 24 18:36 bai.txt
#下面我们在web2上用递归修改目录属主属组
#首先我们先在web2上创建一个目录及文件查看一下
[root@xiaobai-web2] ll /xb
total 0
-rw-r--r--. 1 root root 0 Mar 24 18:43 little.txt
-rw-r--r--. 1 root root 0 Mar 24 18:43 xiaobai.txt
[root@xiaobai-web2] ll -d /xb
drwxr-xr-x. 2 root root 43 Mar 24 18:43 /xb
[root@xiaobai-web2] useradd bai
[root@xiaobai-web2] groupadd little
#创建好后我们到server端
[root@xiaobai-server] ansible webgrp2 -m file -a 'path=/xb owner=bai group=little state=directory recurse=yes' -o
ansible-web2 | SUCCESS => {"changed": true, "gid": 1001, "group": "little", "mode": "0755", "owner": "bai", "path": "/xb", "secontext": "unconfined_u:object_r:default_t:s0", "size": 43, "state": "directory", "uid": 1000}
#我们到web2上查看
[root@xiaobai-web2] ll /xb
total 0
-rw-r--r--. 1 bai little 0 Mar 24 18:43 little.txt
-rw-r--r--. 1 bai little 0 Mar 24 18:43 xiaobai.txt
[root@xiaobai-web2] ll -d /xb
drwxr-xr-x. 2 bai little 43 Mar 24 18:43 /xb
setup模块
#收集信息模块:setup
#只查询ipv4的地址
[root@xiaobai-server] ansible webgrp1 -m setup -a 'filter=ansible_all_ipv4_addresses' #只查询ipv4的地址
ansible-web1 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.202.132"
]
},
"changed": false
}
#收集webgrp1主机组里主机的所有信息
[root@xiaobai-server] ansible webgrp1 -m setup #收集所有信息
ansible-web1 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.202.132"
],
"ansible_all_ipv6_addresses": [
"fe80::2e79:bd5a:6d58:a893"
],
"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-957.el7.x86_64",
"LANG": "en_US.UTF-8",
"crashkernel": "auto",
"quiet": true,
"rd.lvm.lv": "centos/swap",
"rhgb": true,
"ro": true,
"root": "/dev/mapper/centos-root"
},
"ansible_date_time": {
"date": "2020-03-24",
"day": "24",
"epoch": "1585047488",
"hour": "18",
"iso8601": "2020-03-24T10:58:08Z",
"iso8601_basic": "20200324T185808894833",
"iso8601_basic_short": "20200324T185808",
"iso8601_micro": "2020-03-24T10:58:08.894982Z",
"minute": "58",
"month": "03",
"second": "08",
"time": "18:58:08",
"tz": "CST",
"tz_offset": "+0800",
"weekday": "Tuesday",
"weekday_number": "2",
"weeknumber": "12",
"year": "2020"
},
"ansible_default_ipv4": {
"address": "192.168.202.132",
"alias": "ens33",
"broadcast": "192.168.202.255",
"gateway": "192.168.202.2",
"interface": "ens33",
"macaddress": "00:0c:29:f7:1f:e6",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.202.0",
"type": "ether"
},
"ansible_default_ipv6": {},
"ansible_device_links": {
"ids": {
"dm-0": [
"dm-name-centos-root",
"dm-uuid-LVM-kUjX5p1YdG1naOljSdzSwpk0UCSeGeOtPcCa58AvQKI8n9CajEf1Hzn50QZQQTrA"
],
"dm-1": [
"dm-name-centos-swap",
"dm-uuid-LVM-kUjX5p1YdG1naOljSdzSwpk0UCSeGeOtXXNODp0UGQAS3yvvMnU2RWkzXfo6YaBc"
],
"sda2": [
"lvm-pv-uuid-GKKbuL-uFaK-awoj-8BuW-Omp5-8bKZ-tw1i6N"
],
"sr0": [
"ata-VMware_Virtual_IDE_CDROM_Drive_10000000000000000001"
]
},
"labels": {
"sr0": [
"CentOS\\x207\\x20x86_64"
]
},
"masters": {
"sda2": [
"dm-0",
"dm-1"
]
},
"uuids": {
"dm-0": [
"eb751dd6-cbdb-484b-97e7-40f3b9b91ffe"
],
"dm-1": [
"a159b7a9-ac25-45e8-a137-3f0a534d32ff"
],
"sda1": [
"13f65528-6d61-4f08-b422-a7534b88322d"
],
"sr0": [
"2018-11-25-23-54-16-00"
]
}
},
"ansible_devices": {
"dm-0": {
"holders": [],
"host": "",
"links": {
"ids": [
"dm-name-centos-root",
"dm-uuid-LVM-kUjX5p1YdG1naOljSdzSwpk0UCSeGeOtPcCa58AvQKI8n9CajEf1Hzn50QZQQTrA"
],
"labels": [],
"masters": [],
"uuids": [
"eb751dd6-cbdb-484b-97e7-40f3b9b91ffe"
]
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "",
"sectors": "35643392",
"sectorsize": "512",
"size": "17.00 GB",
"support_discard": "0",
"vendor": null,
"virtual": 1
},
"dm-1": {
"holders": [],
"host": "",
"links": {
"ids": [
"dm-name-centos-swap",
"dm-uuid-LVM-kUjX5p1YdG1naOljSdzSwpk0UCSeGeOtXXNODp0UGQAS3yvvMnU2RWkzXfo6YaBc"
],
"labels": [],
"masters": [],
"uuids": [
"a159b7a9-ac25-45e8-a137-3f0a534d32ff"
]
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "",
"sectors": "4194304",
"sectorsize": "512",
"size": "2.00 GB",
"support_discard": "0",
"vendor": null,
"virtual": 1
},
"sda": {
"holders": [],
"host": "SCSI storage controller: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01)",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": "VMware Virtual S",
"partitions": {
"sda1": {
"holders": [],
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": [
"13f65528-6d61-4f08-b422-a7534b88322d"
]
},
"sectors": "2097152",
"sectorsize": 512,
"size": "1.00 GB",
"start": "2048",
"uuid": "13f65528-6d61-4f08-b422-a7534b88322d"
},
"sda2": {
"holders": [
"centos-root",
"centos-swap"
],
"links": {
"ids": [
"lvm-pv-uuid-GKKbuL-uFaK-awoj-8BuW-Omp5-8bKZ-tw1i6N"
],
"labels": [],
"masters": [
"dm-0",
"dm-1"
],
"uuids": []
},
"sectors": "39843840",
"sectorsize": 512,
"size": "19.00 GB",
"start": "2099200",
"uuid": null
}
},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "deadline",
"sectors": "41943040",
"sectorsize": "512",
"size": "20.00 GB",
"support_discard": "0",
"vendor": "VMware,",
"virtual": 1
},
"sr0": {
"holders": [],
"host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)",
"links": {
"ids": [
"ata-VMware_Virtual_IDE_CDROM_Drive_10000000000000000001"
],
"labels": [
"CentOS\\x207\\x20x86_64"
],
"masters": [],
"uuids": [
"2018-11-25-23-54-16-00"
]
},
"model": "VMware IDE CDR10",
"partitions": {},
"removable": "1",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "deadline",
"sectors": "8962048",
"sectorsize": "2048",
"size": "17.09 GB",
"support_discard": "0",
"vendor": "NECVMWar",
"virtual": 1
}
},
"ansible_distribution": "CentOS",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/redhat-release",
"ansible_distribution_file_variety": "RedHat",
"ansible_distribution_major_version": "7",
"ansible_distribution_release": "Core",
"ansible_distribution_version": "7.6.1810",
"ansible_dns": {
"nameservers": [
"192.168.202.2"
],
"search": [
"localdomain"
]
},
"ansible_domain": "",
"ansible_effective_group_id": 0,
"ansible_effective_user_id": 0,
"ansible_ens33": {
"active": true,
"device": "ens33",
"features": {
"busy_poll": "off [fixed]",
"fcoe_mtu": "off [fixed]",
"generic_receive_offload": "on",
"generic_segmentation_offload": "on",
"highdma": "off [fixed]",
"hw_tc_offload": "off [fixed]",
"l2_fwd_offload": "off [fixed]",
"large_receive_offload": "off [fixed]",
"loopback": "off [fixed]",
"netns_local": "off [fixed]",
"ntuple_filters": "off [fixed]",
"receive_hashing": "off [fixed]",
"rx_all": "off",
"rx_checksumming": "off",
"rx_fcs": "off",
"rx_gro_hw": "off [fixed]",
"rx_udp_tunnel_port_offload": "off [fixed]",
"rx_vlan_filter": "on [fixed]",
"rx_vlan_offload": "on",
"rx_vlan_stag_filter": "off [fixed]",
"rx_vlan_stag_hw_parse": "off [fixed]",
"scatter_gather": "on",
"tcp_segmentation_offload": "on",
"tx_checksum_fcoe_crc": "off [fixed]",
"tx_checksum_ip_generic": "on",
"tx_checksum_ipv4": "off [fixed]",
"tx_checksum_ipv6": "off [fixed]",
"tx_checksum_sctp": "off [fixed]",
"tx_checksumming": "on",
"tx_fcoe_segmentation": "off [fixed]",
"tx_gre_csum_segmentation": "off [fixed]",
"tx_gre_segmentation": "off [fixed]",
"tx_gso_partial": "off [fixed]",
"tx_gso_robust": "off [fixed]",
"tx_ipip_segmentation": "off [fixed]",
"tx_lockless": "off [fixed]",
"tx_nocache_copy": "off",
"tx_scatter_gather": "on",
"tx_scatter_gather_fraglist": "off [fixed]",
"tx_sctp_segmentation": "off [fixed]",
"tx_sit_segmentation": "off [fixed]",
"tx_tcp6_segmentation": "off [fixed]",
"tx_tcp_ecn_segmentation": "off [fixed]",
"tx_tcp_mangleid_segmentation": "off",
"tx_tcp_segmentation": "on",
"tx_udp_tnl_csum_segmentation": "off [fixed]",
"tx_udp_tnl_segmentation": "off [fixed]",
"tx_vlan_offload": "on [fixed]",
"tx_vlan_stag_hw_insert": "off [fixed]",
"udp_fragmentation_offload": "off [fixed]",
"vlan_challenged": "off [fixed]"
},
"hw_timestamp_filters": [],
"ipv4": {
"address": "192.168.202.132",
"broadcast": "192.168.202.255",
"netmask": "255.255.255.0",
"network": "192.168.202.0"
},
"ipv6": [
{
"address": "fe80::2e79:bd5a:6d58:a893",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "00:0c:29:f7:1f:e6",
"module": "e1000",
"mtu": 1500,
"pciid": "0000:02:01.0",
"promisc": false,
"speed": 1000,
"timestamping": [
"tx_software",
"rx_software",
"software"
],
"type": "ether"
},
"ansible_env": {
"HOME": "/root",
"LANG": "en_US.UTF-8",
"LESSOPEN": "||/usr/bin/lesspipe.sh %s",
"LOGNAME": "root",
"LS_COLORS": "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:",
"MAIL": "/var/mail/root",
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin",
"PWD": "/root",
"SELINUX_LEVEL_REQUESTED": "",
"SELINUX_ROLE_REQUESTED": "",
"SELINUX_USE_CURRENT_RANGE": "",
"SHELL": "/bin/bash",
"SHLVL": "2",
"SSH_CLIENT": "192.168.202.131 35900 22",
"SSH_CONNECTION": "192.168.202.131 35900 192.168.202.132 22",
"SSH_TTY": "/dev/pts/2",
"TERM": "xterm",
"USER": "root",
"XDG_RUNTIME_DIR": "/run/user/0",
"XDG_SESSION_ID": "22",
"_": "/usr/bin/python"
},
"ansible_fips": false,
"ansible_form_factor": "Other",
"ansible_fqdn": "ansible-web1",
"ansible_hostname": "ansible-web1",
"ansible_interfaces": [
"lo",
"ens33"
],
"ansible_kernel": "3.10.0-957.el7.x86_64",
"ansible_lo": {
"active": true,
"device": "lo",
"features": {
"busy_poll": "off [fixed]",
"fcoe_mtu": "off [fixed]",
"generic_receive_offload": "on",
"generic_segmentation_offload": "on",
"highdma": "on [fixed]",
"hw_tc_offload": "off [fixed]",
"l2_fwd_offload": "off [fixed]",
"large_receive_offload": "off [fixed]",
"loopback": "on [fixed]",
"netns_local": "on [fixed]",
"ntuple_filters": "off [fixed]",
"receive_hashing": "off [fixed]",
"rx_all": "off [fixed]",
"rx_checksumming": "on [fixed]",
"rx_fcs": "off [fixed]",
"rx_gro_hw": "off [fixed]",
"rx_udp_tunnel_port_offload": "off [fixed]",
"rx_vlan_filter": "off [fixed]",
"rx_vlan_offload": "off [fixed]",
"rx_vlan_stag_filter": "off [fixed]",
"rx_vlan_stag_hw_parse": "off [fixed]",
"scatter_gather": "on",
"tcp_segmentation_offload": "on",
"tx_checksum_fcoe_crc": "off [fixed]",
"tx_checksum_ip_generic": "on [fixed]",
"tx_checksum_ipv4": "off [fixed]",
"tx_checksum_ipv6": "off [fixed]",
"tx_checksum_sctp": "on [fixed]",
"tx_checksumming": "on",
"tx_fcoe_segmentation": "off [fixed]",
"tx_gre_csum_segmentation": "off [fixed]",
"tx_gre_segmentation": "off [fixed]",
"tx_gso_partial": "off [fixed]",
"tx_gso_robust": "off [fixed]",
"tx_ipip_segmentation": "off [fixed]",
"tx_lockless": "on [fixed]",
"tx_nocache_copy": "off [fixed]",
"tx_scatter_gather": "on [fixed]",
"tx_scatter_gather_fraglist": "on [fixed]",
"tx_sctp_segmentation": "on",
"tx_sit_segmentation": "off [fixed]",
"tx_tcp6_segmentation": "on",
"tx_tcp_ecn_segmentation": "on",
"tx_tcp_mangleid_segmentation": "on",
"tx_tcp_segmentation": "on",
"tx_udp_tnl_csum_segmentation": "off [fixed]",
"tx_udp_tnl_segmentation": "off [fixed]",
"tx_vlan_offload": "off [fixed]",
"tx_vlan_stag_hw_insert": "off [fixed]",
"udp_fragmentation_offload": "on",
"vlan_challenged": "on [fixed]"
},
"hw_timestamp_filters": [],
"ipv4": {
"address": "127.0.0.1",
"broadcast": "host",
"netmask": "255.0.0.0",
"network": "127.0.0.0"
},
"ipv6": [
{
"address": "::1",
"prefix": "128",
"scope": "host"
}
],
"mtu": 65536,
"promisc": false,
"timestamping": [
"rx_software",
"software"
],
"type": "loopback"
},
"ansible_local": {},
"ansible_lsb": {},
"ansible_lvm": {
"lvs": {
"root": {
"size_g": "17.00",
"vg": "centos"
},
"swap": {
"size_g": "2.00",
"vg": "centos"
}
},
"pvs": {
"/dev/sda2": {
"free_g": "0",
"size_g": "19.00",
"vg": "centos"
}
},
"vgs": {
"centos": {
"free_g": "0",
"num_lvs": "2",
"num_pvs": "1",
"size_g": "19.00"
}
}
},
"ansible_machine": "x86_64",
"ansible_machine_id": "1cdb67a530594940ab0e1a29348b1e30",
"ansible_memfree_mb": 453,
"ansible_memory_mb": {
"nocache": {
"free": 748,
"used": 224
},
"real": {
"free": 453,
"total": 972,
"used": 519
},
"swap": {
"cached": 0,
"free": 2047,
"total": 2047,
"used": 0
}
},
"ansible_memtotal_mb": 972,
"ansible_mounts": [
{
"block_available": 4128697,
"block_size": 4096,
"block_total": 4452864,
"block_used": 324167,
"device": "/dev/mapper/centos-root",
"fstype": "xfs",
"inode_available": 8878784,
"inode_total": 8910848,
"inode_used": 32064,
"mount": "/",
"options": "rw,seclabel,relatime,attr2,inode64,noquota",
"size_available": 16911142912,
"size_total": 18238930944,
"uuid": "eb751dd6-cbdb-484b-97e7-40f3b9b91ffe"
},
{
"block_available": 225740,
"block_size": 4096,
"block_total": 259584,
"block_used": 33844,
"device": "/dev/sda1",
"fstype": "xfs",
"inode_available": 523962,
"inode_total": 524288,
"inode_used": 326,
"mount": "/boot",
"options": "rw,seclabel,relatime,attr2,inode64,noquota",
"size_available": 924631040,
"size_total": 1063256064,
"uuid": "13f65528-6d61-4f08-b422-a7534b88322d"
}
],
"ansible_nodename": "ansible-web1",
"ansible_os_family": "RedHat",
"ansible_pkg_mgr": "yum",
"ansible_processor": [
"0",
"GenuineIntel",
"Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz"
],
"ansible_processor_cores": 1,
"ansible_processor_count": 1,
"ansible_processor_threads_per_core": 1,
"ansible_processor_vcpus": 1,
"ansible_product_name": "VMware Virtual Platform",
"ansible_product_serial": "VMware-56 4d 93 af 89 6e 1a cd-2f 3e 7c fd 73 f7 1f e6",
"ansible_product_uuid": "AF934D56-6E89-CD1A-2F3E-7CFD73F71FE6",
"ansible_product_version": "None",
"ansible_python": {
"executable": "/usr/bin/python",
"has_sslcontext": true,
"type": "CPython",
"version": {
"major": 2,
"micro": 5,
"minor": 7,
"releaselevel": "final",
"serial": 0
},
"version_info": [
2,
7,
5,
"final",
0
]
},
"ansible_python_version": "2.7.5",
"ansible_real_group_id": 0,
"ansible_real_user_id": 0,
"ansible_selinux": {
"config_mode": "enforcing",
"mode": "enforcing",
"policyvers": 31,
"status": "enabled",
"type": "targeted"
},
"ansible_selinux_python_present": true,
"ansible_service_mgr": "systemd",
"ansible_ssh_host_key_ecdsa_public": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBIq0qk7jFyk+YqTijw17IFvm1wmQ8+9E/NumRM8ounOt7GE0TSlbu9FGzH/sUPJH7bY46MLRT7D4D7pH4iCxvk8=",
"ansible_ssh_host_key_ed25519_public": "AAAAC3NzaC1lZDI1NTE5AAAAIIi0k8IQhFj4cJ6uVvbYSFa/tLlMsRQ3ztpRUZdc0oTm",
"ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAADAQABAAABAQDLbSiEXWbKAgimeNerIdz/OfyFnKmP4+axcHCTlWKGPd2cvYJ1dMOEd7ujHpVX4crGzu4DkNDJ2Ub6q0ptfMuvvwenuAT0cOk81+XnPpJS5uHnE+XuoFeUO3A4UEt5Dm2rJrr9nf0AUdPWAlpNxk0j7Q+j4mpMpbLV0wOxZ9GQHavjZ4q6W3TTvSE2A16IVEM2sfU8cuF1Mx2r7iw4XKxCCH4kQqBCEkNXYi0uYnWKQ9JY/cilIBfgiN5/L5EwQ3J+soC2uZ+JDbTrYyqa9P+LIB3wFny5t/TfAXwX0jC8cdSfP23Qvb9QtEZgevnvAArXXZi56Px0+hweBobAoQJx",
"ansible_swapfree_mb": 2047,
"ansible_swaptotal_mb": 2047,
"ansible_system": "Linux",
"ansible_system_capabilities": [
"cap_chown",
"cap_dac_override",
"cap_dac_read_search",
"cap_fowner",
"cap_fsetid",
"cap_kill",
"cap_setgid",
"cap_setuid",
"cap_setpcap",
"cap_linux_immutable",
"cap_net_bind_service",
"cap_net_broadcast",
"cap_net_admin",
"cap_net_raw",
"cap_ipc_lock",
"cap_ipc_owner",
"cap_sys_module",
"cap_sys_rawio",
"cap_sys_chroot",
"cap_sys_ptrace",
"cap_sys_pacct",
"cap_sys_admin",
"cap_sys_boot",
"cap_sys_nice",
"cap_sys_resource",
"cap_sys_time",
"cap_sys_tty_config",
"cap_mknod",
"cap_lease",
"cap_audit_write",
"cap_audit_control",
"cap_setfcap",
"cap_mac_override",
"cap_mac_admin",
"cap_syslog",
"35",
"36+ep"
],
"ansible_system_capabilities_enforced": "True",
"ansible_system_vendor": "VMware, Inc.",
"ansible_uptime_seconds": 35161,
"ansible_user_dir": "/root",
"ansible_user_gecos": "root",
"ansible_user_gid": 0,
"ansible_user_id": "root",
"ansible_user_shell": "/bin/bash",
"ansible_user_uid": 0,
"ansible_userspace_architecture": "x86_64",
"ansible_userspace_bits": "64",
"ansible_virtualization_role": "guest",
"ansible_virtualization_type": "VMware",
"gather_subset": [
"all"
],
"module_setup": true
},
"changed": false
}