日常放大佬阎 https://www.jianshu.com/p/201ae65bb819 可能会消失
https://www.jianshu.com/p/0c3356e4164b ansible不完全手册
在做ansible的时候,首先要确保能够免密登录然后保证python版本在2.6以上 都有open-ssh
yum -y install ansible
ssh-keygen -t rsa
cd ~/.ssh && ls
id_rsa id_rsa.pub
// 将公钥 id_rsa.pub copy 到 被管理服务器上 authorized_keys ⽂件中, 确保⽂件的权限为 0600
// managedhost 为被管理服务器,copy的过程中需要⽤户名及密码
ssh-copy-id root@192.168.74.161
ssh-copy-id root@192.168.74.162
ansible all -i 192.168.74.161,192.168.74.162 -m ping
首先传送公钥 实现可以ping通
192.168.74.162 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.74.161 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
加入了本地解析
vi /etc/hosts
大佬闫当时讲课是把所有三台机器都做了本地解析,这都是一样的
然后分别在127.0.0.1 添加了自己的主机名
[root@xiaobaicai .ssh]# ansible all -i host1,host2 -m ping
host2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
host1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
vim /etc/ansible/hosts
这个就是资源池
如果想拷贝一份的话,cp /etc/ansible/hosts{,.bak}
[db_servers]
host2
host1
[root@xiaobaicai jiaobeng]# ansible db_servers -m ping
host2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
host1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[db_servers]
host2
[nginx_servers]
host1
#host1
[web:children]
#nginx_servers
#[nginx_servers]
db_servers
nginx_servers
ansible web -m ping 默认先访问web,我这里web还没有定义
和上面一样的结果
ansible all -i inventory.ini --list-hosts
之前要在vim /etc/ansible/inventory.ini 写入ip
[root@xiaobaicai ansible]# ansible all -i inventory.ini -a "ls /root"
host1 | CHANGED | rc=0 >>
1904Y)k.txt
anaconda-ks.cfg
a.txt
b.txt
mysql80-community-release-el7-1.noarch.rpm
[root@xiaobaicai ansible]# ansible all -i inventory.ini -m ping
host1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
ansible -i /etc/ansible/hosts.py openstack -m ping -o
需要编辑一个脚本
vim /etc/ansible/hosts.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import json
import argparse
def lists():
"""
indent 定义输出时的格式缩进的空格数
"""
dic = {}
host_list = [ '192.168.74.{}'.format(str(i) ) for i in range(160,165) ]
hosts_dict = {'hosts': host_list}
dic['openstack'] = hosts_dict
return json.dumps(dic,indent=4)
def hosts(name):
dic = {'ansibl_ssh_pass': '12345'}
return json.dumps(dic)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-l', '--list', help='host list', action='store_true')
parser.add_argument('-H', '--host', help='hosts vars')
args = vars(parser.parse_args())
if args['list']:
print( lists() )
elif args['host']:
print( hosts(args['host']) )
else:
parser.print_help()
[root@xiaobaicai ansible]# ansible -i /etc/ansible/hosts.py openstack -m ping -o
192.168.74.162 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.74.161 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.74.160 | UNREACHABLE!: Failed to connect to the host via ssh: ssh: connect to host 192.168.74.160 port 22: No route to host
192.168.74.163 | UNREACHABLE!: Failed to connect to the host via ssh: ssh: connect to host 192.168.74.163 port 22: No route to host
192.168.74.164 | UNREACHABLE!: Failed to connect to the host via ssh: ssh: connect to host 192.168.74.164 port 22: No route to host
ansible host'*' -m ping
这样也可以出现两个 host1 和 host2 的