An example of implementing ansible dynamic inventory script

Posted by DJTim666 on Sun, 03 Nov 2019 00:56:30 +0100

1. The inventory of ansible resources is dynamic. The script specification states that you need to implement the -- list, - host option and output json data.

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 hosts and vars are key characters and cannot 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. Use of meta keyword

If the top-level element returned by the inventory script is "meta", it may return variables of all hosts. If the element contains a value named "hostvars", when the inventory script calls each host, it will not call the -- host option to operate on the target host, but use the information of the target host in hostvars to operate on the target host Machine 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()


Topics: Linux ansible JSON