目录
ansible 环境安装部署
管理端安装 ansible
ansible 目录结构
配置主机清单
配置密钥对验证
ansible 命令行模块
1.command 模块
2.shell 模块
3.cron 模块
4.user 模块
5.group 模块
6.copy 模块
7.file 模块
8.hostname 模块
9.ping 模块
10.yum 模块
11.service/systemd 模块
12.script 模块
13. mount 模块
14. archive 模块
15. unarchive 模块
16. replace 模块
17.setup 模块
inventory 主机清单
inventory 中的变量
1.主机变量
2.组变量
3.组嵌套
Ansible是一个基于Python开发的配置管理和应用部署工具,现在也在自动化管理领域大放异彩。它融合了众多老牌运维工具的优点,Pubbet和Saltstack能实现的功能,Ansible基本上都可以实现。
Ansible能批量配置、部署、管理上千台主机。比如以前需要切换到每个主机上执行的一或多个操作,使用Ansible只需在固定的一台Ansible控制节点上去完成所有主机的操作。
Ansible是基于模块工作的,它只是提供了一种运行框架,它本身没有完成任务的能力,真正执行操作的是Ansible的模块, 比如copy模块用于拷贝文件到远程主机上,service模块用于管理服务的启动、停止、重启等。
Ansible其中一个比较鲜明的特性是Agentless,即无Agent的存在,它就像普通命令一样,并非C/S软件,也只需在某个作为控制节点的主机上安装一次Ansible即可,通常它基于ssh连接来控制远程主机,远程主机上不需要安装Ansible或其它额外的服务。
使用者在使用时,在服务器终端输入命令或者playbooks,会通过预定好的规则将playbook拆解为play,再组织成ansible可以识别的任务,调用模块和插件,根据主机清单通过SSH将临时文件发给远程的客户端执行并返回结果,执行结束后自动删除
Ansible的另一个比较鲜明的特性是它的绝大多数模块都具备幂等性(idempotence)。所谓幂等性,指的是无论执行多少次同样的运算,结果都是相同的,即一条命令,任意多次执行所产生的影响均与一次执行的影响相同。比如执行 systemctl stop xxx 命令来停止服务,当发现要停止的目标服务已经处于停止状态,它什么也不会做, 所以多次停止的结果仍然是停止,不会改变结果,它是幂等的,而 systemctl restart xxx 是非幂等的。
Ansible的很多模块在执行时都会先判断目标节点是否要执行任务,所以,可以放心大胆地让Ansible去执行任务,重复执行某个任务绝大多数时候不会产生任何副作用。
管理端: 192.168.110.100 ansible
被管理端:192.168.110.90
被管理端:192.168.110.70
yum install -y epel-release //先安装 epel 源
yum install -y ansible
/etc/ansible/
├── ansible.cfg #ansible的配置文件,一般无需修改
├── hosts #ansible的主机清单,用于存储需要管理的远程主机的相关信息
└── roles/ #公共角色目录
cd /etc/ansible
vim hosts
[webservers] #配置组名
192.168.110.90 #组里包含的被管理的主机IP地址或主机名(主机名需要先修改/etc/hosts文件)
[dbservers]
192.168.110.70
ssh-keygen -t rsa
yum install -y sshpass
vim /etc/ssh/ssh_config
StrictHostKeyChecking no #把注释取消,把yes改为no
sshpass -p 'zxr123' ssh-copy-id [-o StrictHostKeyChecking=no] [email protected]
sshpass -p 'zxr123' ssh-copy-id [-o StrictHostKeyChecking=no] [email protected]
命令格式:ansible <组名> -m <模块> -a <参数列表>
ansible-doc -l #列出所有已安装的模块,按q退出
在远程主机执行命令,不支持管道,重定向等shell的特性。
ansible-doc -s command #-s 列出指定模块的描述信息和操作动作
ansible 192.168.110.90 -m command -a 'date' #指定 ip 执行 date
192.168.110.90 | CHANGED | rc=0 >>
2023年 07月 27日 星期四 18:16:41 CST
ansible webservers -m command -a 'date' #指定组执行 date
192.168.110.90 | CHANGED | rc=0 >>
2023年 07月 27日 星期四 18:18:14 CST
ansible dbservers -m command -a 'date'
192.168.110.70 | CHANGED | rc=0 >>
2023年 07月 27日 星期四 18:18:41 CST
ansible all -m command -a 'date' #all 代表所有 hosts 主机
192.168.110.70 | CHANGED | rc=0 >>
2023年 07月 27日 星期四 18:19:08 CST
192.168.110.90 | CHANGED | rc=0 >>
2023年 07月 27日 星期四 18:19:08 CST
ansible all -a 'ls /' #如省略 -m 模块,则默认运行 command 模块
192.168.110.70 | CHANGED | rc=0 >>
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
192.168.110.90 | CHANGED | rc=0 >>
bin
boot
data
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
常用的参数:
chdir:在远程主机上运行命令前提前进入目录
creates:判断指定文件是否存在,如果存在,不执行后面的操作
removes:判断指定文件是否存在,如果存在,执行后面的操作
192.168.110.90 | CHANGED | rc=0 >>
anaconda-ks.cfg
initial-setup-ks.cfg
公共
模板
视频
图片
文档
下载
音乐
桌面
192.168.110.70 | CHANGED | rc=0 >>
anaconda-ks.cfg
initial-setup-ks.cfg
公共
模板
视频
图片
文档
下载
音乐
桌面
在远程主机执行命令,相当于调用远程主机的shell进程,然后在该shell下打开一个子shell运行命令(支持管道符号等功能)
ansible-doc -s shell
ansible dbservers -m shell -a 'echo 123456 | passwd --stdin test"'
ansible dbservers -m shell -a 'echo $(ifconfig ens32 | awk "NR==2 {print $2}") | cut -d " " -f2'
192.168.110.70 | CHANGED | rc=0 >>
192.168.110.70
ansible dbservers -m shell -a 'echo $(ifconfig ens32 | awk "NR==2 {print \$2}")'
192.168.110.70 | CHANGED | rc=0 >>
192.168.110.70
在远程主机定义任务计划。其中有两种状态(state):present表示添加(可以省略),absent表示移除。
ansible-doc -s cron #按 q 退出
常用的参数:
minute/hour/day/month/weekday:分/时/日/月/周
job:任务计划要执行的命令
name:任务计划的名称
user:指定计划任务属于哪个用户,默认是root用户
ansible webservers -m cron -a 'minute="*/1" job="/bin/echo helloworld" name="test crontab"'
192.168.110.90 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"test crontab"
]
}
ansible webservers -a 'crontab -l'
192.168.110.90 | CHANGED | rc=0 >>
#Ansible: test crontab
*/1 * * * * /bin/echo helloworld
ansible webservers -m cron -a 'name="test crontab" state=absent'
192.168.110.90 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": []
}
用户管理的模块
ansible-doc -s user
常用的参数:
name:用户名,必选参数
state=present|absent:创建账号或者删除账号,present表示创建,absent表示删除
system=yes|no:是否为系统账号
uid:用户uid
group:用户基本组
groups: 用户所属附加组
shell:默认使用的shell
create_home=yse|no: 是否创建家目录
password:用户的密码,建议使用加密后的字符串
remove=yes|no:当state=absent时,是否删除用户的家目录
ansible dbservers -m user -a 'name="test01"' #创建用户test01
192.168.110.70 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1001,
"home": "/home/test01",
"name": "test01",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1001
}
ansible dbservers -m command -a 'tail /etc/passwd'
192.168.110.70 | CHANGED | rc=0 >>
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
gnome-initial-setup:x:988:982::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
zxr:x:1000:1000:zxr:/home/zxr:/bin/bash
test01:x:1001:1001::/home/test01:/bin/bash
ansible dbservers -m user -a 'name="test01" state=absent' #删除用户test01
192.168.110.70 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"force": false,
"name": "test01",
"remove": false,
"state": "absent"
}
用户组管理的模块
ansible-doc -s group
ansible dbservers -m group -a 'name=mysql gid=306 system=yes' #创建mysql组
192.168.110.70 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 306,
"name": "mysql",
"state": "present",
"system": true
}
ansible dbservers -a 'tail /etc/group'
192.168.110.70 | CHANGED | rc=0 >>
nfsnobody:x:65534:
gnome-initial-setup:x:982:
sshd:x:74:
slocate:x:21:
avahi:x:70:
postdrop:x:90:
postfix:x:89:
tcpdump:x:72:
zxr:x:1000:zxr
mysql:x:306:
ansible dbservers -m user -a 'name=test01 uid=306 system=yes group=mysql' #将test01用户添加到mysql组中
192.168.110.70 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 306,
"home": "/home/test01",
"name": "test01",
"shell": "/bin/bash",
"state": "present",
"stderr": "useradd:警告:此主目录已经存在。\n不从 skel 目录里向其中复制任何文件。\n",
"stderr_lines": [
"useradd:警告:此主目录已经存在。",
"不从 skel 目录里向其中复制任何文件。"
],
"system": true,
"uid": 306
}
ansible dbservers -a 'tail /etc/passwd'
192.168.110.70 | CHANGED | rc=0 >>
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
gnome-initial-setup:x:988:982::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
zxr:x:1000:1000:zxr:/home/zxr:/bin/bash
test01:x:306:306::/home/test01:/bin/bash
ansible dbservers -a 'id test01'
192.168.110.70 | CHANGED | rc=0 >>
uid=306(test01) gid=306(mysql) 组=306(mysql)
用于复制指定主机文件到远程主机的
ansible-doc -s copy
常用的参数:
dest:指出复制文件的目标及位置,使用绝对路径,如果源是目录,指目标也要是目录,如果目标文件已经存在会覆盖原有的内容
src:指出源文件的路径,可以使用相对路径或绝对路径,支持直接指定目录,如果源是目录则目标也要是目录
mode:指出复制时,目标文件的权限
owner:指出复制时,目标文件的属主
group:指出复制时,目标文件的属组
content:指出复制到目标主机上的内容,不能与src一起使用
ansible dbservers -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=root mode=640'
192.168.110.70 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "a93e3b96865a2a3fa4d88221db3b2cafbdaee9ac",
"dest": "/opt/fstab.bak",
"gid": 0,
"group": "root",
"md5sum": "878d3b777c97cc833cc5fb5bbb261349",
"mode": "0640",
"owner": "root",
"secontext": "system_u:object_r:usr_t:s0",
"size": 465,
"src": "/root/.ansible/tmp/ansible-tmp-1690454394.34-7438-50460992613640/source",
"state": "file",
"uid": 0
}
ansible dbservers -a 'ls -l /opt'
192.168.110.70 | CHANGED | rc=0 >>
总用量 4
-rw-r-----. 1 root root 465 7月 27 18:39 fstab.bak
drwxr-xr-x. 2 root root 6 10月 31 2018 rh
ansible dbservers -a 'cat /opt/fstab.bak'
192.168.110.70 | CHANGED | rc=0 >>
#
# /etc/fstab
# Created by anaconda on Fri Jun 2 16:56:02 2023
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=5cef2918-6cbd-4fab-a032-6459274e4141 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
ansible dbservers -m copy -a 'content="helloworld" dest=/opt/hello.txt' #将helloworld写入/opt/hello.txt文件中
192.168.110.70 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "6adfb183a4a2c94a2f92dab5ade762a47889a5a1",
"dest": "/opt/hello.txt",
"gid": 0,
"group": "root",
"md5sum": "fc5e038d38a57032085441e7fe7010b0",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:usr_t:s0",
"size": 10,
"src": "/root/.ansible/tmp/ansible-tmp-1690454577.74-7857-13428756128512/source",
"state": "file",
"uid": 0
}
ansible dbservers -a 'cat /opt/hello.txt'
192.168.110.70 | CHANGED | rc=0 >>
helloworld
设置文件属性
ansible-doc -s file
ansible dbservers -m file -a 'owner=test01 group=mysql mode=644 path=/opt/fstab.bak' #修改文件的属主属组权限等
192.168.110.70 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"gid": 306,
"group": "mysql",
"mode": "0644",
"owner": "test01",
"path": "/opt/fstab.bak",
"secontext": "system_u:object_r:usr_t:s0",
"size": 465,
"state": "file",
"uid": 306
}
ansible dbservers -m file -a 'path=/opt/fstab.link src=/opt/fstab.bak state=link' #设置/opt/fstab.link为/opt/fstab.bak的链接文件
192.168.110.70 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/opt/fstab.link",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"secontext": "unconfined_u:object_r:usr_t:s0",
"size": 14,
"src": "/opt/fstab.bak",
"state": "link",
"uid": 0
}
ansible dbservers -m file -a "path=/opt/abc.txt state=touch" #创建一个文件
192.168.110.70 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"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
}
ansible dbservers -m file -a "path=/opt/abc.txt state=absent" #删除一个文件
192.168.110.70 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/opt/abc.txt",
"state": "absent"
}
用于管理远程主机上的主机名
ansible dbservers -m hostname -a "name=mysql01"
192.168.110.70 | CHANGED => {
"ansible_facts": {
"ansible_domain": "",
"ansible_fqdn": "mysql01",
"ansible_hostname": "mysql01",
"ansible_nodename": "mysql01",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "mysql01"
}
检测远程主机的连通性
ansible all -m ping
192.168.110.90 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.110.70 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
在远程主机上安装与卸载软件包
ansible-doc -s yum
ansible webservers -m yum -a 'name=httpd' #安装服务
192.168.110.90 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"installed": [
"httpd"
]
},
"msg": "file:///mnt/repodata/repomd.xml: [Errno 14] curl#37 - \"Couldn't open file /mnt/repodata/repomd.xml\"\nTrying other mirror.\n",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\n * base: mirrors.ustc.edu.cn\n * extras: mirrors.ustc.edu.cn\n * updates: mirrors.aliyun.com\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-99.el7.centos.1 will be installed\n--> Processing Dependency: httpd-tools = 2.4.6-99.el7.centos.1 for package: httpd-2.4.6-99.el7.centos.1.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-99.el7.centos.1.x86_64\n--> Running transaction check\n---> Package httpd-tools.x86_64 0:2.4.6-99.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-99.el7.centos.1 updates 2.7 M\nInstalling for dependencies:\n httpd-tools x86_64 2.4.6-99.el7.centos.1 updates 94 k\n mailcap noarch 2.1.41-2.el7 base 31 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package (+2 Dependent packages)\n\nTotal download size: 2.8 M\nInstalled size: 9.6 M\nDownloading packages:\n--------------------------------------------------------------------------------\nTotal 3.8 MB/s | 2.8 MB 00:00 \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : httpd-tools-2.4.6-99.el7.centos.1.x86_64 1/3 \n Installing : mailcap-2.1.41-2.el7.noarch 2/3 \n Installing : httpd-2.4.6-99.el7.centos.1.x86_64 3/3 \n Verifying : httpd-2.4.6-99.el7.centos.1.x86_64 1/3 \n Verifying : mailcap-2.1.41-2.el7.noarch 2/3 \n Verifying : httpd-tools-2.4.6-99.el7.centos.1.x86_64 3/3 \n\nInstalled:\n httpd.x86_64 0:2.4.6-99.el7.centos.1 \n\nDependency Installed:\n httpd-tools.x86_64 0:2.4.6-99.el7.centos.1 mailcap.noarch 0:2.1.41-2.el7 \n\nComplete!\n"
]
}
ansible webservers -m yum -a 'name=httpd state=absent' #卸载服务
192.168.110.90 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"removed": [
"httpd"
]
},
"msg": "file:///mnt/repodata/repomd.xml: [Errno 14] curl#37 - \"Couldn't ope n file /mnt/repodata/repomd.xml\"\n正在尝试其它镜像。\n",
"rc": 0,
"results": [
"已加载插件:fastestmirror, langpacks\n正在解决依赖关系\n--> 正在检查事 务\n---> 软件包 httpd.x86_64.0.2.4.6-99.el7.centos.1 将被 删除\n--> 解决依赖关系 完成\n\n依赖关系解决\n\n======================================================== ========================\n Package 架构 版本 源 大小\n===================================================== ===========================\n正在删除:\n httpd x86_64 2.4.6-99.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-99.el7.centos.1.x86_64 1/1 \n 验证中 : httpd- 2.4.6-99.el7.centos.1.x86_64 1/1 \n\n删除:\n httpd.x86 _64 0:2.4.6-99.el7.centos.1 \n\n完毕!\ n"
]
}
用于管理远程主机上的管理服务的运行状态
ansible-doc -s service
常用的参数:
name:被管理的服务名称
state=started|stopped|restarted:动作包含启动关闭或者重启
enabled=yes|no:表示是否设置该服务开机自启
runlevel:如果设定了enabled开机自启去,则要定义在哪些运行目标下自启动
ansible webservers -a 'systemctl status httpd' #查看web服务器httpd运行状态
192.168.110.90 | CHANGED | rc=0 >>
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since 四 2023-07-27 19:02:18 CST; 16s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 4775 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
Tasks: 6
CGroup: /system.slice/httpd.service
├─4775 /usr/sbin/httpd -DFOREGROUND
├─4778 /usr/sbin/httpd -DFOREGROUND
├─4779 /usr/sbin/httpd -DFOREGROUND
├─4780 /usr/sbin/httpd -DFOREGROUND
├─4781 /usr/sbin/httpd -DFOREGROUND
└─4782 /usr/sbin/httpd -DFOREGROUND
7月 27 19:02:18 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
7月 27 19:02:18 localhost.localdomain httpd[4775]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
7月 27 19:02:18 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
ansible webservers -m service -a 'enabled=true name=httpd state=started' #启动httpd服务
192.168.110.90 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"enabled": true,
"name": "httpd",
"state": "started",
"status": {
"ActiveEnterTimestamp": "四 2023-07-27 19:02:18 CST",
"ActiveEnterTimestampMonotonic": "3754857708",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "active",
"After": "-.mount tmp.mount remote-fs.target systemd-journald.socket network.target nss-lookup.target basic.target system.slice",
"AllowIsolate": "no",
"AmbientCapabilities": "0",
"AssertResult": "yes",
"AssertTimestamp": "四 2023-07-27 19:02:18 CST",
"AssertTimestampMonotonic": "3754788195",
"Before": "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",
"CollectMode": "inactive",
"ConditionResult": "yes",
"ConditionTimestamp": "四 2023-07-27 19:02:18 CST",
"ConditionTimestampMonotonic": "3754788194",
"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": "4775",
"ExecMainStartTimestamp": "四 2023-07-27 19:02:18 CST",
"ExecMainStartTimestampMonotonic": "3754789462",
"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=[四 2023-07-27 19:02:18 CST] ; stop_time=[n/a] ; pid=4775 ; 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": "四 2023-07-27 19:02:18 CST",
"InactiveExitTimestampMonotonic": "3754789494",
"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": "15594",
"LimitRSS": "18446744073709551615",
"LimitRTPRIO": "0",
"LimitRTTIME": "18446744073709551615",
"LimitSIGPENDING": "15594",
"LimitSTACK": "18446744073709551615",
"LoadState": "loaded",
"MainPID": "4775",
"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 system.slice -.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": "6",
"TasksMax": "18446744073709551615",
"TimeoutStartUSec": "1min 30s",
"TimeoutStopUSec": "1min 30s",
"TimerSlackNSec": "50000",
"Transient": "no",
"Type": "notify",
"UMask": "0022",
"UnitFilePreset": "disabled",
"UnitFileState": "disabled",
"WatchdogTimestamp": "四 2023-07-27 19:02:18 CST",
"WatchdogTimestampMonotonic": "3754857677",
"WatchdogUSec": "0"
}
}
实现远程批量运行本地的 shell 脚本
ansible-doc -s script
vim test.sh
#!/bin/bash
echo "hello ansible from script" > /opt/script.txt
chmod +x test.sh
ansible webservers -m script -a 'test.sh'
192.168.110.90 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.110.90 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.110.90 closed."
],
"stdout": "",
"stdout_lines": []
}
ansible webservers -a 'cat /opt/script.txt'
192.168.110.90 | CHANGED | rc=0 >>
hello ansible from script
vim test.sh
#!/bin/bash
echo $1 > /opt/test.txt
echo s2 >> /opt/test .txt
ansible dbservers -m script -a 'test.sh abc 123!
挂载文件系统
ansible-doc -s mount
常用的参数:
src:定义挂载设备的路径
path:定义挂载到哪个目录,必须指定
fstype:指定挂载文件的系统类型,必须指定,xfs、iso9660、nfs...
opts:定义挂载的参数,defaults、rw、ro...
state:定义挂载的状态,mounted(进行挂载,修改/etc/fstab信息)、absent(永久性卸载,并修改 /etc/fstab信息)、unmounted(临时卸载,不修改/etc/fstab信息)
ansible dbservers -m mount -a 'src=/dev/sr0 path=/mnt state=mounted fstype=iso9660'
192.168.110.70 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dump": "0",
"fstab": "/etc/fstab",
"fstype": "iso9660",
"name": "/mnt",
"opts": "defaults",
"passno": "0",
"src": "/dev/sr0"
}
[root@localhost opt]# df -hT
文件系统 类型 容量 已用 可用 已用% 挂载点
devtmpfs devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 2.0G 13M 2.0G 1% /run
tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 36G 4.4G 31G 13% /
/dev/sda1 xfs 1014M 172M 843M 17% /boot
tmpfs tmpfs 394M 12K 394M 1% /run/user/42
tmpfs tmpfs 394M 0 394M 0% /run/user/0
/dev/sr0 iso9660 4.4G 4.4G 0 100% /mnt
打包压缩
ansible-doc -s archive
常用的参数:
path: 必须参数,远程主机上需要被打包压缩的源文件/目录
dest: 打包压缩后的包文件路径(包文件的父目录必须存在);如果包文件已存在,则会被覆盖
format: 指定压缩类型,包括: bz2、gz(默认)、tar、xz、zip
remove=yes|no: 是否删除源文件
ansible dbservers -m archive -a "path=/etc/yum.repos.d/ dest=/opt/repo.zip format=zip"
192.168.110.70 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"archived": [
"/etc/yum.repos.d/local.repo",
"/etc/yum.repos.d/repo.bak/CentOS-Base.repo",
"/etc/yum.repos.d/repo.bak/CentOS-CR.repo",
"/etc/yum.repos.d/repo.bak/CentOS-Debuginfo.repo",
"/etc/yum.repos.d/repo.bak/CentOS-fasttrack.repo",
"/etc/yum.repos.d/repo.bak/CentOS-Media.repo",
"/etc/yum.repos.d/repo.bak/CentOS-Sources.repo",
"/etc/yum.repos.d/repo.bak/CentOS-Vault.repo",
"/etc/yum.repos.d/repo.bak/CentOS-x86_64-kernel.repo"
],
"arcroot": "/etc/yum.repos.d/",
"changed": true,
"dest": "/opt/repo.zip",
"expanded_exclude_paths": [],
"expanded_paths": [
"/etc/yum.repos.d/"
],
"gid": 0,
"group": "root",
"missing": [],
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:usr_t:s0",
"size": 4934,
"state": "file",
"uid": 0
}
在192.168.110.70主机进行查看,确实生成了repo.zip文件
[root@localhost opt]# ls
fstab.bak fstab.link hello.txt repo.zip rh
ansible dbservers -m archive -a "path=/opt/abc.txt,/opt/123.txt dest=/opt/abc123.tar.gz format=gz remove=yes"
192.168.110.70 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"archived": [],
"arcroot": "/opt/",
"changed": true,
"dest": "/opt/abc123.tar.gz",
"expanded_exclude_paths": [],
"expanded_paths": [
"/opt/abc.txt",
"/opt/123.txt"
],
"gid": 0,
"group": "root",
"missing": [
"/opt/abc.txt",
"/opt/123.txt"
],
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:usr_t:s0",
"size": 61,
"state": "file",
"uid": 0
}
解包解压缩
ansible-doc -s unarchive
常用的参数:
copy:默认为 copy=yes ,拷贝的文件从 ansible 主机复制到远程主机,copy=no 表示在远程主机上寻找源文件解压
src:tar包源路径,可以是 ansible 主机上的路径,也可以是远程主机上的路径,如果是远程主机上的路径,则需设置 copy=no
dest:解压后文件的目标绝对路径
remote_src: 和 copy 功能一样且互斥,设置 remote_src=yes 表示文件在远程主机上,设置为 remote_src=no 表示文件在 ansible 主机上
ansible dbservers -m unarchive -a "src=/opt/nginx-1.24.0.tar.gz dest=/root copy=yes" #将 ansible 主机的压缩文件拷贝到到远程主机并解压,修改文件所属组和用户
192.168.110.70 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/root",
"extract_results": {
"cmd": [
"/usr/bin/gtar",
"--extract",
"-C",
"/root",
"-z",
"-f",
"/root/.ansible/tmp/ansible-tmp-1690457088.96-12391-121726958971989/source"
],
"err": "",
"out": "",
"rc": 0
},
"gid": 0,
"group": "root",
"handler": "TgzArchive",
"mode": "0550",
"owner": "root",
"secontext": "system_u:object_r:admin_home_t:s0",
"size": 4096,
"src": "/root/.ansible/tmp/ansible-tmp-1690457088.96-12391-121726958971989/source",
"state": "directory",
"uid": 0
}
或者
ansible dbservers -m unarchive -a "src=/opt/abc.tar.gz dest=/root remote_src=no"
在192.168.110.70查看root目录
[root@localhost opt]# ls /root
anaconda-ks.cfg nginx-1.24.0 模板 图片 下载 桌面
initial-setup-ks.cfg 公共 视频 文档 音乐
ansible dbservers -m unarchive -a "src=/opt/123.tar.gz dest=/root copy=no" #在远程主机解包
192.168.110.70 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/root",
"extract_results": {
"cmd": [
"/usr/bin/gtar",
"--extract",
"-C",
"/root",
"-z",
"-f",
"/opt/yxc.tar.gz"
],
"err": "",
"out": "",
"rc": 0
},
"gid": 0,
"group": "root",
"handler": "TgzArchive",
"mode": "0550",
"owner": "root",
"secontext": "system_u:object_r:admin_home_t:s0",
"size": 4096,
"src": "/opt/yxc.tar.gz",
"state": "directory",
"uid": 0
}
或者
ansible dbservers -m unarchive -a "src=/opt/123.tar.gz dest=/root remote_src=yes"
类似于sed命令,主要也是基于正则进行匹配和替换
ansible-doc -s replace
常用的参数:
path:必须参数,指定要修改的文件
regexp:必须参数,指定一个正则表达式
replace:替换regexp参数匹配到的字符串
backup=yes|no: 修改源文件前创建一个包含时间戳信息的备份文件
before:如果指定,则仅替换/删除此匹配之前的内容,可以和after参数结合使用
after:如果指定,则仅替换/删除此匹配之后的内容,可以和before参数结合使用
owner:修改文件用户名
group:修改文件组名
mode:修改文件权限
vim /opt/test.txt
11 22 33 44 55 66
aa bb cc dd ee ff
1a 2b 3c 4d 5e 6f
ansible dbservers -m replace -a "path=/opt/test.txt regexp='33' replace='cc'" #匹配 333 并修改为 ccc
192.168.110.70 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"msg": "1 replacements made"
}
11 22 cc 44 55 66
aa bb cc dd ee ff
1a 2b 3c 4d 5e 6f
ansible dbservers -m replace -a "path=/opt/test.txt regexp='^(.*)' replace='#\1'" #匹配到任意一个或多个开头的行增加注释
192.168.110.70 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"msg": "4 replacements made"
}
#11 22 cc 44 55 66
#aa bb cc dd ee ff
#1a 2b 3c 4d 5e 6f
ansible dbservers -m replace -a "path=/opt/test.txt regexp='^#(.*)' replace='\1'" #取消注释
192.168.110.70 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"msg": "3 replacements made"
}
11 22 cc 44 55 66
aa bb cc dd ee ff
1a 2b 3c 4d 5e 6f
ansible dbservers -m replace -a "path=/opt/test.txt regexp='^(a.*)' replace='#\1'" #匹配以 a 开头的后面有一个或者多个字符的行,并在前面添加 # 注释
192.168.110.70 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"msg": "1 replacements made"
}
11 22 cc threethree 55 66
#aa bb cc dd ee ff
1a 2b 3c 4d 5e 6f
ansible dbservers -m replace -a "path=/opt/test.txt regexp='3' replace='three' before=cc"
192.168.110.70 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"msg": "2 replacements made"
}
11 22 cc threethree 55 66
aa bb cc dd ee ff
1a 2b 3c 4d 5e 6f
facts 组件是用来收集被管理节点信息的,使用 setup 模块可以获取这些信息
ansible-doc -s setup
ansible webservers -m setup #获取mysql组主机的facts信息
192.168.110.90 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.110.90",
"192.168.122.1"
],
"ansible_all_ipv6_addresses": [
"fe80::992:7f22:e337:709a"
],
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "x86_64",
"ansible_bios_date": "11/12/2020",
"ansible_bios_version": "6.00",
"ansible_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.10.0-1160.el7.x86_64",
"LANG": "zh_CN.UTF-8",
"quiet": true,
"rd.lvm.lv": "centos/swap",
"rhgb": true,
"ro": true,
"root": "/dev/mapper/centos-root"
},
"ansible_date_time": {
"date": "2023-07-27",
"day": "27",
"epoch": "1690458049",
"hour": "19",
"iso8601": "2023-07-27T11:40:49Z",
"iso8601_basic": "20230727T194049404100",
"iso8601_basic_short": "20230727T194049",
"iso8601_micro": "2023-07-27T11:40:49.404100Z",
"minute": "40",
"month": "07",
"second": "49",
"time": "19:40:49",
"tz": "CST",
"tz_offset": "+0800",
"weekday": "星期四",
"weekday_number": "4",
"weeknumber": "30",
"year": "2023"
},
"ansible_default_ipv4": {
"address": "192.168.110.90",
"alias": "ens32",
"broadcast": "192.168.110.255",
"gateway": "192.168.110.2",
"interface": "ens32",
"macaddress": "00:0c:29:34:c3:f1",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.110.0",
"type": "ether"
},
"ansible_default_ipv6": {},
"ansible_device_links": {
"ids": {
"dm-0": [
"dm-name-centos-root",
"dm-uuid-LVM-hJTgeZV0N9q7nfGK5E9QDe1ew7OgUq1G0RnC6vjsObSPV2CKdBd2Dl6svlTDCPio"
],
"dm-1": [
"dm-name-centos-swap",
"dm-uuid-LVM-hJTgeZV0N9q7nfGK5E9QDe1ew7OgUq1GYOKsNtYiiM4F5QX5efYDdzOAwaSEsdzP"
],
"sda2": [
"lvm-pv-uuid-ATgT9o-q7Ts-R0Ib-4Zsz-gFfh-gwuY-6mlQQt"
],
"sr0": [
"ata-VMware_Virtual_IDE_CDROM_Drive_10000000000000000001"
]
},
"labels": {
"sr0": [
"CentOS\\x207\\x20x86_64"
]
},
"masters": {
"sda2": [
"dm-0",
"dm-1"
]
},
"uuids": {
"dm-0": [
"512e053e-94d9-400f-b4a5-990703560011"
],
"dm-1": [
"f1d08392-df6a-4310-a002-b48ca037974c"
],
"sda1": [
"6266bc55-79d5-402f-91a7-c28ebc8d36cb"
],
"sr0": [
"2020-11-04-11-36-43-00"
]
}
},
"ansible_devices": {
"dm-0": {
"holders": [],
"host": "",
"links": {
"ids": [
"dm-name-centos-root",
"dm-uuid-LVM-hJTgeZV0N9q7nfGK5E9QDe1ew7OgUq1G0RnC6vjsObSPV2CKdBd2Dl6svlTDCPio"
],
"labels": [],
"masters": [],
"uuids": [
"512e053e-94d9-400f-b4a5-990703560011"
]
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "",
"sectors": "73646080",
"sectorsize": "512",
"size": "35.12 GB",
"support_discard": "0",
"vendor": null,
"virtual": 1
},
"dm-1": {
"holders": [],
"host": "",
"links": {
"ids": [
"dm-name-centos-swap",
"dm-uuid-LVM-hJTgeZV0N9q7nfGK5E9QDe1ew7OgUq1GYOKsNtYiiM4F5QX5efYDdzOAwaSEsdzP"
],
"labels": [],
"masters": [],
"uuids": [
"f1d08392-df6a-4310-a002-b48ca037974c"
]
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "",
"sectors": "8126464",
"sectorsize": "512",
"size": "3.88 GB",
"support_discard": "0",
"vendor": null,
"virtual": 1
},
"sda": {
"holders": [],
"host": "SCSI storage controller: Broadcom / LSI 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": [
"6266bc55-79d5-402f-91a7-c28ebc8d36cb"
]
},
"sectors": "2097152",
"sectorsize": 512,
"size": "1.00 GB",
"start": "2048",
"uuid": "6266bc55-79d5-402f-91a7-c28ebc8d36cb"
},
"sda2": {
"holders": [
"centos-root",
"centos-swap"
],
"links": {
"ids": [
"lvm-pv-uuid-ATgT9o-q7Ts-R0Ib-4Zsz-gFfh-gwuY-6mlQQt"
],
"labels": [],
"masters": [
"dm-0",
"dm-1"
],
"uuids": []
},
"sectors": "81786880",
"sectorsize": 512,
"size": "39.00 GB",
"start": "2099200",
"uuid": null
}
},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "deadline",
"sectors": "83886080",
"sectorsize": "512",
"size": "40.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": [
"2020-11-04-11-36-43-00"
]
},
"model": "VMware IDE CDR10",
"partitions": {},
"removable": "1",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "deadline",
"sectors": "9203712",
"sectorsize": "2048",
"size": "4.39 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.9",
"ansible_dns": {
"nameservers": [
"192.168.110.2"
]
},
"ansible_domain": "localdomain",
"ansible_effective_group_id": 0,
"ansible_effective_user_id": 0,
"ansible_ens32": {
"active": true,
"device": "ens32",
"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.110.90",
"broadcast": "192.168.110.255",
"netmask": "255.255.255.0",
"network": "192.168.110.0"
},
"ipv6": [
{
"address": "fe80::992:7f22:e337:709a",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "00:0c:29:34:c3:f1",
"module": "e1000",
"mtu": 1500,
"pciid": "0000:02:00.0",
"promisc": false,
"speed": 1000,
"timestamping": [
"tx_software",
"rx_software",
"software"
],
"type": "ether"
},
"ansible_env": {
"HOME": "/root",
"LANG": "C",
"LC_ALL": "C",
"LC_NUMERIC": "C",
"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.110.100 34118 22",
"SSH_CONNECTION": "192.168.110.100 34118 192.168.110.90 22",
"SSH_TTY": "/dev/pts/1",
"TERM": "xterm",
"USER": "root",
"XDG_DATA_DIRS": "/root/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share",
"XDG_RUNTIME_DIR": "/run/user/0",
"XDG_SESSION_ID": "32",
"_": "/usr/bin/python"
},
"ansible_fibre_channel_wwn": [],
"ansible_fips": false,
"ansible_form_factor": "Other",
"ansible_fqdn": "localhost.localdomain",
"ansible_hostname": "localhost",
"ansible_hostnqn": "",
"ansible_interfaces": [
"lo",
"ens32",
"virbr0-nic",
"virbr0"
],
"ansible_is_chroot": false,
"ansible_iscsi_iqn": "iqn.1994-05.com.redhat:d61bd50d66d",
"ansible_kernel": "3.10.0-1160.el7.x86_64",
"ansible_kernel_version": "#1 SMP Mon Oct 19 16:18:59 UTC 2020",
"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": "",
"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": "35.12",
"vg": "centos"
},
"swap": {
"size_g": "3.88",
"vg": "centos"
}
},
"pvs": {
"/dev/sda2": {
"free_g": "0.00",
"size_g": "39.00",
"vg": "centos"
}
},
"vgs": {
"centos": {
"free_g": "0.00",
"num_lvs": "2",
"num_pvs": "1",
"size_g": "39.00"
}
}
},
"ansible_machine": "x86_64",
"ansible_machine_id": "56c56608bff34ef8a0c395066eb191f4",
"ansible_memfree_mb": 2770,
"ansible_memory_mb": {
"nocache": {
"free": 3306,
"used": 625
},
"real": {
"free": 2770,
"total": 3931,
"used": 1161
},
"swap": {
"cached": 0,
"free": 3967,
"total": 3967,
"used": 0
}
},
"ansible_memtotal_mb": 3931,
"ansible_mounts": [
{
"block_available": 215704,
"block_size": 4096,
"block_total": 259584,
"block_used": 43880,
"device": "/dev/sda1",
"fstype": "xfs",
"inode_available": 523948,
"inode_total": 524288,
"inode_used": 340,
"mount": "/boot",
"options": "rw,seclabel,relatime,attr2,inode64,noquota",
"size_available": 883523584,
"size_total": 1063256064,
"uuid": "6266bc55-79d5-402f-91a7-c28ebc8d36cb"
},
{
"block_available": 1587537,
"block_size": 4096,
"block_total": 9201265,
"block_used": 7613728,
"device": "/dev/mapper/centos-root",
"fstype": "xfs",
"inode_available": 12700455,
"inode_total": 13101192,
"inode_used": 400737,
"mount": "/",
"options": "rw,seclabel,relatime,attr2,inode64,noquota",
"size_available": 6502551552,
"size_total": 37688381440,
"uuid": "512e053e-94d9-400f-b4a5-990703560011"
}
],
"ansible_nodename": "localhost.localdomain",
"ansible_os_family": "RedHat",
"ansible_pkg_mgr": "yum",
"ansible_proc_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.10.0-1160.el7.x86_64",
"LANG": "zh_CN.UTF-8",
"quiet": true,
"rd.lvm.lv": [
"centos/root",
"centos/swap"
],
"rhgb": true,
"ro": true,
"root": "/dev/mapper/centos-root"
},
"ansible_processor": [
"0",
"GenuineIntel",
"11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz",
"1",
"GenuineIntel",
"11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz"
],
"ansible_processor_cores": 1,
"ansible_processor_count": 2,
"ansible_processor_threads_per_core": 1,
"ansible_processor_vcpus": 2,
"ansible_product_name": "VMware Virtual Platform",
"ansible_product_serial": "VMware-56 4d 51 84 c4 5b 84 cc-2c 9e 67 5c 89 34 c3 f1",
"ansible_product_uuid": "84514D56-5BC4-CC84-2C9E-675C8934C3F1",
"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": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBNzyuLM4qzFFelYr5QIDKGI80S4WUIHiZEZBKC6e3UwwIc1rCWpYXhaLfcjc3XCRLhYMUCKJLIFZEcHccB3bTTU=",
"ansible_ssh_host_key_ed25519_public": "AAAAC3NzaC1lZDI1NTE5AAAAIEcN4TTRNEO2wevqbqWEuL3U23N9V7GBmiNm+7m94bcU",
"ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAADAQABAAABAQCcM56Mx4Mh08z3da0jycoZtgEd/kOls9niGAK4iLJrAFCtZbN0naBL0yn4V2KtpTh/acDdwvVVypMFHsg3M4JXpr8HBEiqHRhqLRU+ftT/8jmdaLE5pTttr7djuI+2HTtmh40BwYUiWZTRT8j9huvl3p0BraIzUhkQDvwkkWyLlz8j6rPJnjLvWFyDhNRwpop0StL3OBoMfvFEQioU91YrgImwuizYcrnEvNdBxHLqNu13cgCiIq53UoUD+FcTAg1lu51bg8MS1qsUT7bABg+ijXmOmtqB4NRpX4eKNC55p+7CcNyEsklVxow1M6YG80ox4I18hDJlIeti042mJsJv",
"ansible_swapfree_mb": 3967,
"ansible_swaptotal_mb": 3967,
"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": 6065,
"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_virbr0": {
"active": false,
"device": "virbr0",
"features": {
"busy_poll": "off [fixed]",
"fcoe_mtu": "off [fixed]",
"generic_receive_offload": "on",
"generic_segmentation_offload": "on",
"highdma": "off [requested on]",
"hw_tc_offload": "off [fixed]",
"l2_fwd_offload": "off [fixed]",
"large_receive_offload": "off [fixed]",
"loopback": "off [fixed]",
"netns_local": "on [fixed]",
"ntuple_filters": "off [fixed]",
"receive_hashing": "off [fixed]",
"rx_all": "off [fixed]",
"rx_checksumming": "off [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",
"tx_checksum_ipv4": "off [fixed]",
"tx_checksum_ipv6": "off [fixed]",
"tx_checksum_sctp": "off [fixed]",
"tx_checksumming": "on",
"tx_fcoe_segmentation": "off [requested on]",
"tx_gre_csum_segmentation": "on",
"tx_gre_segmentation": "on",
"tx_gso_partial": "on",
"tx_gso_robust": "off [requested on]",
"tx_ipip_segmentation": "on",
"tx_lockless": "on [fixed]",
"tx_nocache_copy": "off",
"tx_scatter_gather": "on",
"tx_scatter_gather_fraglist": "on",
"tx_sctp_segmentation": "off [requested on]",
"tx_sit_segmentation": "on",
"tx_tcp6_segmentation": "on",
"tx_tcp_ecn_segmentation": "on",
"tx_tcp_mangleid_segmentation": "on",
"tx_tcp_segmentation": "on",
"tx_udp_tnl_csum_segmentation": "on",
"tx_udp_tnl_segmentation": "on",
"tx_vlan_offload": "on",
"tx_vlan_stag_hw_insert": "on",
"udp_fragmentation_offload": "off [requested on]",
"vlan_challenged": "off [fixed]"
},
"hw_timestamp_filters": [],
"id": "8000.525400fc1bcc",
"interfaces": [
"virbr0-nic"
],
"ipv4": {
"address": "192.168.122.1",
"broadcast": "192.168.122.255",
"netmask": "255.255.255.0",
"network": "192.168.122.0"
},
"macaddress": "52:54:00:fc:1b:cc",
"mtu": 1500,
"promisc": false,
"stp": true,
"timestamping": [
"rx_software",
"software"
],
"type": "bridge"
},
"ansible_virbr0_nic": {
"active": false,
"device": "virbr0-nic",
"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 [fixed]",
"rx_checksumming": "off [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": "off",
"tx_checksum_fcoe_crc": "off [fixed]",
"tx_checksum_ip_generic": "off [requested on]",
"tx_checksum_ipv4": "off [fixed]",
"tx_checksum_ipv6": "off [fixed]",
"tx_checksum_sctp": "off [fixed]",
"tx_checksumming": "off",
"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",
"tx_scatter_gather": "on",
"tx_scatter_gather_fraglist": "on",
"tx_sctp_segmentation": "off [fixed]",
"tx_sit_segmentation": "off [fixed]",
"tx_tcp6_segmentation": "off [requested on]",
"tx_tcp_ecn_segmentation": "off [requested on]",
"tx_tcp_mangleid_segmentation": "off",
"tx_tcp_segmentation": "off [requested on]",
"tx_udp_tnl_csum_segmentation": "off [fixed]",
"tx_udp_tnl_segmentation": "off [fixed]",
"tx_vlan_offload": "on",
"tx_vlan_stag_hw_insert": "on",
"udp_fragmentation_offload": "off [requested on]",
"vlan_challenged": "off [fixed]"
},
"hw_timestamp_filters": [],
"macaddress": "52:54:00:fc:1b:cc",
"mtu": 1500,
"promisc": true,
"timestamping": [
"rx_software",
"software"
],
"type": "ether"
},
"ansible_virtualization_role": "guest",
"ansible_virtualization_type": "VMware",
"discovered_interpreter_python": "/usr/bin/python",
"gather_subset": [
"all"
],
"module_setup": true
},
"changed": false
}
ansible dbservers -m setup -a 'filter=*ipv4' #使用filter可以筛选指定的facts信息
192.168.110.70 | SUCCESS => {
"ansible_facts": {
"ansible_default_ipv4": {
"address": "192.168.110.70",
"alias": "ens32",
"broadcast": "192.168.110.255",
"gateway": "192.168.110.2",
"interface": "ens32",
"macaddress": "00:0c:29:e4:96:c1",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.110.0",
"type": "ether"
},
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
Inventory支持对主机进行分组,每个组内可以定义多个主机,每个主机都可以定义在任何一个或多个主机组内。
如果是名称类似的主机,可以使用列表的方式标识各个主机。
vim /etc/ansible/hosts
[webservers]
192.168.80.11:2222 #冒号后定义远程连接端口,默认是 ssh 的 22 端口
192.168.80.1[2:5]
[dbservers]
db-[a:f].example.org #支持匹配 a~f
随便输出一个命令就会显示为下面这样,连接一个地址之后,就会连接地址池中其他地址,当然其他地址就会报错
192.168.110.70 | SUCCESS => {
"ansible_facts": {
"ansible_default_ipv4": {
"address": "192.168.110.70",
"alias": "ens32",
"broadcast": "192.168.110.255",
"gateway": "192.168.110.2",
"interface": "ens32",
"macaddress": "00:0c:29:e4:96:c1",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.110.0",
"type": "ether"
},
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
192.168.110.71 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.110.71 port 22: No route to host",
"unreachable": true
}
192.168.110.74 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.110.74 port 22: No route to host",
"unreachable": true
}
192.168.110.72 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.110.72 port 22: No route to host",
"unreachable": true
}
192.168.110.73 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.110.73 port 22: No route to host",
"unreachable": true
}
192.168.110.75 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.110.75 port 22: No route to host",
"unreachable": true
}
而连接192.168.110.90时,就会因为端口不对而无法连接
192.168.110.90 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.110.90 port 2222: No route to host",
"unreachable": true
}
Inventory变量名 | 含义 |
ansible_host | ansible连接节点时的IP地址 |
ansible_port | 连接对方的端口号,ssh连接时默认为22 |
ansible_user | 连接对方主机时使用的用户名。不指定时,将使用执行ansible或ansible-playbook命令的用户 |
ansible_password | 连接时的用户的ssh密码,仅在未使用密钥对验证的情况下有效 |
ansible_ssh_private_key_file | 指定密钥认证ssh连接时的私钥文件 |
ansible_ssh_common_args | 提供给ssh、sftp、scp命令的额外参数 |
ansible_become_method | 指定提升权限的方式,例如可使用sudo/su/runas等方式 |
ansible_become_user | 提升为哪个用户的权限,默认提升为root |
ansible_become_password | 提升为指定用户权限时的密码 |
vim /etc/ansible/hosts
[webservers]
192.168.110.90 ansible_port=22 ansible_user=root ansible_password=zxr123
随便输出一个命令,然后把192.168.110.90主机的用户切换到zxr,并且把登录密码也输入正确
192.168.110.90 | SUCCESS => {
"ansible_facts": {
"ansible_default_ipv4": {
"address": "192.168.110.90",
"alias": "ens32",
"broadcast": "192.168.110.255",
"gateway": "192.168.110.2",
"interface": "ens32",
"macaddress": "00:0c:29:34:c3:f1",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.110.0",
"type": "ether"
},
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
vim /etc/ansible/hosts
[webservers:vars] #表示为 webservers 组内所有主机定义变量
ansible_user=root
ansible_password=abc1234
[webservers]
192.168.110.90
[webservers:vars]
ansible_user=zxr
ansible_password=zxr123
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
# If you have multiple hosts following a pattern you can specify
192.168.110.90 | SUCCESS => {
"ansible_facts": {
"ansible_default_ipv4": {
"address": "192.168.110.90",
"alias": "ens32",
"broadcast": "192.168.110.255",
"gateway": "192.168.110.2",
"interface": "ens32",
"macaddress": "00:0c:29:34:c3:f1",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.110.0",
"type": "ether"
},
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
vim /etc/ansible/hosts
[all:vars] #表示为所有组内的所有主机定义变量
ansible_port=22
[nginx]
192.168.80.20
192.168.80.21
192.168.80.22
[apache]
192.168.80.3[0:3]
[webs:children] #表示为 webs 主机组中包含了 nginx 组和 apache 组内的所有主机
nginx
apache
[webservers]
192.168.110.90
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
# If you have multiple hosts following a pattern you can specify
# them like this:
## www[001:006].example.com
# Ex 3: A collection of database servers in the 'dbservers' group
[dbservers]
192.168.110.70
##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57
[webs:children]
webservers
dbservers
ansible webs:children -m setup -a 'filter=*ipv4'
[WARNING]: Could not match supplied host pattern, ignoring: children
192.168.110.90 | SUCCESS => {
"ansible_facts": {
"ansible_default_ipv4": {
"address": "192.168.110.90",
"alias": "ens32",
"broadcast": "192.168.110.255",
"gateway": "192.168.110.2",
"interface": "ens32",
"macaddress": "00:0c:29:34:c3:f1",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.110.0",
"type": "ether"
},
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
192.168.110.70 | SUCCESS => {
"ansible_facts": {
"ansible_default_ipv4": {
"address": "192.168.110.70",
"alias": "ens32",
"broadcast": "192.168.110.255",
"gateway": "192.168.110.2",
"interface": "ens32",
"macaddress": "00:0c:29:e4:96:c1",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.110.0",
"type": "ether"
},
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}