隐藏页面特效

Ansible—Inventory主机清单

含义

清查;存货清单;财产目录;主机清单

1、增加主机组

官方链接
http://docs.ansible.com/ansible/intro_inventory.html#
vi /etc/ansible/hosts
## db-[99:101]-node.example.com
用中括号[]扩起来,中括号里添加信息为 组名(可自定义)
[webserver]
host1
host2

 

测试

[root@ansible ~]# ansible webserver -m ping -o
host2 | UNREACHABLE!: Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
host1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
[root@ansible ~]#

host2失败原因:因为未对host2主机进行配置免密登陆

2、增加用户名 密码

[root@ansible ~]# vi /etc/ansible/hosts
## db-[99:101]-node.example.com
[webserver]
host1
host2 ansible_ssh_user='root' ansible_ssh_pass='123'

测试为全部成功

[root@ansible ~]# ansible webserver -m ping -o
host1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
host2 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

另一种方法

vi /etc/ansible/hosts
## db-[99:101]-node.example.com
[webserver]
host[1:2]
host3 ansible_ssh_port='2222'
[1:2]指的是host主机1和主机2
host3指定了sshd端口号,不指定会执行失败

 

3、增加端口

这里我把host2的sshd程序端口修改为‘2222’进行测试

[root@ansible ~]# ansible webserver -m ping -o
host2 | UNREACHABLE!: Failed to connect to the host via ssh: ssh: connect to host host2 port 22: Connection refused
host1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
[root@ansible ~]#
失败,因为默认端口已更改

 

配置文件下指定host2端口

vi /etc/ansible/hosts
## db-[99:101]-node.example.com
[webserver]
host1
host2 ansible_ssh_user='root' ansible_ssh_pass='123' ansible_ssh_port='2222'
增加端口信息
[root@ansible ~]# ansible webserver -m ping -o
host1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
host2 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
成功

4、组:变量

ansible内部变量可以帮助我们简化主机清单的设置

vi /etc/ansible/hosts
[webserver]
host[1:3]
[webserver:vars] vars指定是‘变量的意思’
ansible_ssh_user='root'
ansible_ssh_pass='123'

常用变量:

5、子分组

将不同的分组进行组合

vim /etc/ansible/hosts
[nginx]
host[1:2]
[apache]
host3
[webserver:children] children内置变量,可以理解定义一个组叫webserver
nginx
apache
[webserver:vars]
ansible_ssh_user='root'
ansible_ssh_pass='123'
ansible webserver -m ping -o 这里的webserver指的是组名

 

6、自定义主机列表

创建一个文件名字为hostlist
vi hostlist
[dockers]
host1
host2
[dockers:vars]
ansible_ssh_user='root'
ansible_ssh_pass='123'
ansible -i hostlist dockers -m ping -o
ansible -i 绝对路径 dockers -m ping -o
-i 指的是链接外部主机清单,后边应该加上绝对路径(如果本身就在文件目录下直接指定’文件名‘即可)
hostlist 文件名
dockers 主机组 组名


__EOF__

本文作者Echo
本文链接https://www.cnblogs.com/Jqazc/p/16739173.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   我真的兔了  阅读(54)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示