ansible achieve dynamic inventory script example

A ansible dynamic resource inventory, the script specification need to achieve --list, - host option, and outputs the data to json.

   1. --list Output:

[root@master_101 ansible]# ./dy_host.py --list
{
    "all": [
        "192.168.8.101",
        "192.168.8.102"
    ],
    "web": {
        "hosts": [
            "192.168.8.101"
        ],
        "vars": {
            "aa": 10
        }
    },
    "web2": {
        "hosts": [
            "192.168.8.101",
            "192.168.8.102"
        ]
    }
}

   ## where the hosts, vars is the key character, can not be changed

 2. --host output:

[root@master_101 ansible]# ./dy_host.py --host 192.168.8.102
{
    "ansible_host": "192.168.8.102",
    "ansible_port": 22,
    "ansible_user": "sun",
    "ansible_password": "qweasd",
    "ansible_become": "true",
    "ansible_become_method": "su",
    "ansible_become_user": "root",
    "ansible_become_pass": "qweasd"
}

3. _meta keywords

       If inventory script top-level element returned is "_meta", it may return variables of all hosts. If this element contains a value called "hostvars". This inventory script for each host with a call, they will not call --host option to operate on the target host, but the use of information hostvars in the target host to the target host operation.

  [root@master_101 ansible]# ./dy_host.py --list
{
    "_meta": {
        "hostvars": {
            "192.168.8.101": {
                "ansible_user": "root",
                "ansible_password": "qweasd"
            },
            "192.168.8.102": {
                "ansible_user": "sun",
                "ansible_password": "qweasd",
                "ansible_become": "true",
                "ansible_become_method": "su",
                "ansible_become_user": "root",
                "ansible_become_pass": "qweasd"
            }
        }
    },
    "all": [
        "192.168.8.101",
        "192.168.8.102"
    ],
    "web": {
        "hosts": [
            "192.168.8.101"
        ],
        "vars": {
            "aa": 10
        }
    },
    "web2": {
        "hosts": [
            "192.168.8.101",
            "192.168.8.102"
        ]
    }
}

4 Script example

[root@master_101 ansible]# cat dy_host.py

#!/usr/bin/env python3
#coding:utf8
import json
import sys
def group():
    hosts = {
        '_meta' : {
              "hostvars": {
                     '192.168.8.101':{'ansible_user':'root','ansible_password':'qweasd'},
                     '192.168.8.102':{'ansible_user':'sun','ansible_password': 'qweasd',
                                      'ansible_become':'true',
                                      'ansible_become_method':'su',
                                      'ansible_become_user':'root',
                                      'ansible_become_pass':'qweasd'
                                      }
                   }  
        },
        'all': ['192.168.8.101','192.168.8.102'],
        'web':{"hosts": ['192.168.8.101'],'vars': {'aa':10},},
        'web2': {"hosts":['192.168.8.101','192.168.8.102']}
    }
    print(json.dumps(hosts,indent=4))
 
def host(ip):
    hosts = {
        "192.168.8.101": {
            "ansible_host":"192.168.8.101",
            "ansible_port":22,
            "ansible_user":"root",
            "ansible_pass":"qweasd"
        },
        "192.168.8.102": {
            "ansible_host":"192.168.8.102",
            "ansible_port":22,
            "ansible_user":"sun",
            "ansible_password":"qweasd",
            'ansible_become':'true',
            'ansible_become_method':'su',
            'ansible_become_user':'root',
            'ansible_become_pass':'qweasd'
        }
    }
    j = json.dumps(hosts[ip],indent=4)
    print(j)
    
def main():
    if len(sys.argv) == 2 and (sys.argv[1] == '--list'):
        group()
    elif len(sys.argv) == 3 and (sys.argv[1] == '--host'):
        host(sys.argv[2])
    else:
        print("Usage: %s --list or --host <hostname>" % sys.argv[0])
if __name__ == '__main__':
    main()


Guess you like

Origin blog.51cto.com/5766902/2402580