|
动态inventory,可以通过cmdb或者zabbix主机信息获取
只需要把ansible.cfg中inventroy的定义值改成一个执行脚本即可
脚本需要支持两个参数:
--list或者-l ,这个参数显示所有主机的信息(json格式)、
--host或者-H ,参数后面指定一个host,会显示这台主机的所有信息
[root@Server129 sh]# python hosts.py --list
[root@Server129 sh]# cat hosts.py
#!/usr/bin/python
import argparse
import sys
import json
def list():
r={}
h=['192.168.37.128'+str(i) for i in range(1,4)]
hosts={'host':h}
r['webservers']=hosts
return json.dumps(r,indent=4)
def hosts(name):
r={'ansible_ssh_pass':'123456'}
cpis=dict(r.items())
return json.dumps(cpis)
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 list()
elif args['host']:
print hosts(args['host'])
else:
parser.print_help()
获取主机名
[root@Server129 ansible]# ansible docker -m command -a 'hostname' -i inventory.cfg
192.168.37.128 | SUCCESS | rc=0 >>
Agent128
192.168.37.130 | SUCCESS | rc=0 >>
Agent130
[root@Server129 ansible]#
复制文件
[root@Server129 sh]# ansible docker -m copy -a 'src=/root/sh/hosts.py dest=/root/host.py owner=root group=root mode=644 backup=yes' -o -i /etc/ansible/inventory.cfg
192.168.37.128 | SUCCESS => {"changed": true, "checksum": "195b22880b74606da9e764190e71c56c70483fc0", "dest": "/root/host.py", "gid": 0, "group": "root", "md5sum": "de1d8575943b2a4dea93286ad8891a10", "mode": "0644", "owner": "root", "size": 683, "src": "/root/.ansible/tmp/ansible-tmp-1481962078.42-107945087575250/source", "state": "file", "uid": 0}
192.168.37.130 | SUCCESS => {"changed": true, "checksum": "195b22880b74606da9e764190e71c56c70483fc0", "dest": "/root/host.py", "gid": 0, "group": "root", "md5sum": "de1d8575943b2a4dea93286ad8891a10", "mode": "0644", "owner": "root", "size": 683, "src": "/root/.ansible/tmp/ansible-tmp-1481962082.57-54346149537398/source", "state": "file", "uid": 0}
包和服务管理
安装httpd
[root@Server129 sh]# ansible docker -m yum -a 'name=httpd state=latest' -f 5 -o -i /etc/ansible/inventory.cfg
验证结果
[root@Server129 sh]# ansible docker -m shell -a 'rpm -qa|grep httpd' -f 5 -o -i /etc/ansible/inventory.cfg
192.168.37.128 | SUCCESS | rc=0 | (stdout) httpd-2.2.15-55.el6.centos.2.x86_64\nhttpd-tools-2.2.15-55.el6.centos.2.x86_64
192.168.37.130 | SUCCESS | rc=0 | (stdout) httpd-2.2.15-55.el6.centos.2.x86_64\nhttpd-tools-2.2.15-55.el6.centos.2.x86_64
启动httpd
[root@Server129 sh]# ansible docker -m shell -a '/etc/init.d/httpd start' -f 5 -o -i /etc/ansible/inventory.cfg
192.168.37.128 | SUCCESS | rc=0 | (stdout) Starting httpd: [ OK ] (stderr) httpd: apr_sockaddr_info_get() failed for Agent128\nhttpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
192.168.37.130 | SUCCESS | rc=0 | (stdout) Starting httpd: [ OK ] (stderr) httpd: apr_sockaddr_info_get() failed for Agent130\nhttpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
用户管理
首先通过openssl命令生成一个密码,因为ansible user的password参数需要接受加密后的值
[root@Server129 sh]# echo ansible|openssl passwd -1 -stdin
$1$Mgtzov84$cDDoc/n34xdbVaJihHN0U1
用user模块新建用户
[root@Server129 sh]# ansible docker -m user -a 'name=jack password="$1$Mgtzov84$cDDoc/n34xdbVaJihHN0U1"' -f 5 -o -i /etc/ansible/inventory.cfg
192.168.37.128 | SUCCESS => {"changed": true, "comment": "", "createhome": true, "group": 501, "home": "/home/jack", "name": "jack", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/bash", "state": "present", "system": false, "uid": 501}
192.168.37.130 | SUCCESS => {"changed": true, "comment": "", "createhome": true, "group": 501, "home": "/home/jack", "name": "jack", "password": "NOT_LOGGING_PASSWORD", "shell": "/bin/bash", "state": "present", "system": false, "uid": 501}
Ansible playbook
facts组件是ansible用于采集被管机器设备信息的一个功能,可以用setup模块查机器的所有facts信息
可以用filter查看指定信息。整个facts信息被包装纸json格式的数据结构中,ansible_facts是最上层的值
[root@Server129 sh]# ansible docker -m setup -f 5 -o -i /etc/ansible/inventory.cfg
显示结果太多,不贴出来了
[root@Server129 sh]# ansible docker -m setup -a 'filter=ansible_all_ipv4_addresses' -f 5 -o -i /etc/ansible/inventory.cfg
192.168.37.128 | SUCCESS => {"ansible_facts": {"ansible_all_ipv4_addresses": ["192.168.37.128"]}, "changed": false}
192.168.37.130 | SUCCESS => {"ansible_facts": {"ansible_all_ipv4_addresses": ["192.168.37.130"]}, "changed": false}
Ansible Role |
|