ansible組件

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章