Ansible Inventory 主机清单

Inventory是什么


Inventory 文件主要用来填写被管理主机以及主机组信息;(逻辑上定义);默认Inventory文件为/etc/ansible/hosts;

当然也可以自定义一个文件,当执行 ansible 命令时使用-i选项指定 Inventory 文件位置;

inventory里面可以定义单个主机,一般都是去定义主机组。

列出主机中有多少台主机 

[root@master ansible]# ansible webservers --list-hosts
  hosts (1):
    192.168.111.6

可以看到要输入yes/no

[root@master ansible]# ansible webservers -m ping
The authenticity of host '192.168.111.7 (192.168.111.7)' can't be established.
ECDSA key fingerprint is SHA256:Tixbvr6R13gu8is3+O4LSUjPJ3fO4yaWU+yENPu/l5g.
ECDSA key fingerprint is MD5:d8:b1:96:80:65:aa:bc:2e:5d:e4:60:c3:82:93:ab:a8.
Are you sure you want to continue connecting (yes/no)? The authenticity of host '192.168.111.8 (192.168.111.8)' can't be established.
ECDSA key fingerprint is SHA256:na7DOfswnmTKyNunAo8lwUDGcCFRmpiTTjDGRkT1bHU.
ECDSA key fingerprint is MD5:fd:a6:49:e7:e1:e5:db:24:25:0f:f5:08:27:2f:fb:bd.
Are you sure you want to continue connecting (yes/no)? yes

将这行注释去掉,那么就是不去验证了 

[root@master ansible]# vim /etc/ansible/ansible.cfg
host_key_checking = False

Ansible连接被控端主机


 因为在配置文件中谢啦远程端口为22,所以这里可以省略。

[root@master ansible]# cat hosts 
[webservers]
192.168.111.6 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123456
192.168.111.7 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123456


[root@master ansible]# ansible webservers -m ping
192.168.111.7 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
192.168.111.6 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

写用户名密码很费劲,还有一种方式就是先免密,然后直接定义逻辑组即可完成通信

[root@master ~]# ssh-keygen 
[root@master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
[root@master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

可以自己写个脚本免交互的配置互信。

猜你喜欢

转载自blog.csdn.net/qq_34556414/article/details/125398221