含义
清查;存货清单;财产目录;主机清单
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 主机组 组名
标签:python,ping,ansible,host2,webserver,Ansible,Inventory,清单,ssh
From: https://www.cnblogs.com/Jqazc/p/16739173.html