ansible的主机清单详解
基于密码的连接
如果是第一次登陆,需要免ssh验证的话,可以设置:
vim /etc/ansible/ansible.cfg
host_key_checking = False
cat /etc/ansible/hosts
[all]
10.0.0.11 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1qaz@WSX'
10.0.0.12 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1qaz@WSX'
10.0.0.13 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1qaz@WSX'
10.0.0.14 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1qaz@WSX'
10.0.0.15 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass='1qaz@WSX'
或者使用共用变量:
cat /etc/ansible/hosts
[all]
10.0.0.11 ansible_ssh_port=22 ansible_ssh_user=root
10.0.0.12 ansible_ssh_port=22 ansible_ssh_user=root
10.0.0.13 ansible_ssh_port=22 ansible_ssh_user=root
10.0.0.14 ansible_ssh_port=22 ansible_ssh_user=root
10.0.0.15 ansible_ssh_port=22 ansible_ssh_user=root
[all:vars]
ansible_ssh_pass='1qaz@WSX'
基于密钥连接
基于密钥连接第一种就是对主机清单的所有机器配置免密连接,这种不必介绍,我们介绍另一种方式,如下:
cat /etc/ansible/hosts
[all]
10.0.0.11 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa
10.0.0.12 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa
10.0.0.13 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa
10.0.0.14 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa
10.0.0.15 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa
# 这种办法指定了私钥文件,我们需要提前把公钥传到对应的主机上才可以
ssh-copy-id root@10.0.0.11
ssh-copy-id root@10.0.0.12
ssh-copy-id root@10.0.0.13
ssh-copy-id root@10.0.0.14
ssh-copy-id root@10.0.0.15
子组的方式
cat /etc/ansible/hosts
[redis]
10.0.0.11 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa
10.0.0.12 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa
[mysql]
10.0.0.13 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa
10.0.0.14 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa
10.0.0.15 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_private_key_file=~/.ssh/id_rsa
[redis_mysql:children]
redis
mysql
# 这种方式我们执行可以直接指定 redis_mysql 为一个组,会执行redis和mysql组下面的所有机器
ansible redis_mysql -m shell -a "touch /tmp/testok.txt"

浙公网安备 33010602011771号