Ansible Dynamic Inventory Example

Posted on Fri 12 May 2017 in ansible, linux, rhel, centos

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python
import argparse
import json

ANSIBLE_INV = {
    "dbserver": {
        "hosts": ["10.65.100.10"],
        "vars": {
            "ansible_password": "Secret1234",
            "ansible_user": "devops",
        }
    },
}

def output_list_inventory(json_output):
    print json.dumps(json_output)

def main():

    # Argument parsing
    parser = argparse.ArgumentParser(description="Ansible dynamic inventory")
    parser.add_argument("--list", help="Ansible inventory of all of the groups",
        action="store_true", dest="list_inventory")
    parser.add_argument("--host",
        help="Ansible inventory of a particular host", action="store",
        dest="ansible_host", type=str)

    cli_args = parser.parse_args()
    list_inventory = cli_args.list_inventory
    ansible_host = cli_args.ansible_host

    if list_inventory:
        output_list_inventory(ANSIBLE_INV)


if __name__ == "__main__":
    main()