Ansible—Inventory主机清单

含义

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

1、增加主机组

官方链接
    http://docs.ansible.com/ansible/intro_inventory.html#

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

Ansible—Inventory主机清单

测试

[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'

Ansible—Inventory主机清单

测试为全部成功

[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端口号,不指定会执行失败

Ansible—Inventory主机清单

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 ~]#
失败,因为默认端口已更改

Ansible—Inventory主机清单

配置文件下指定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"}
成功

Ansible—Inventory主机清单

4、组:变量

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

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

Ansible—Inventory主机清单

常用变量:

Ansible—Inventory主机清单

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指的是组名

Ansible—Inventory主机清单

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  主机组  组名

Ansible—Inventory主机清单

Original: https://www.cnblogs.com/Jqazc/p/16739173.html
Author: 我真的兔了
Title: Ansible—Inventory主机清单

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/611176/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球