動態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