summaryrefslogtreecommitdiffstats
path: root/qtip/ansible_library/modules/apex_generate_inventory.py
blob: 0c9500f19434a035de043b672f9326c44c650a45 (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/python

###############################################################
# Copyright (c) 2017 ZTE Corporation
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

from collections import defaultdict
import re

from ansible.module_utils.basic import AnsibleModule


ANSIBLE_METADATA = {'metadata_version': '1.0',
                    'status': ['preview'],
                    'supported_by': 'community'}

DOCUMENTATION = '''
---
module: apex
short_description: collecting facts from apex environments
description:
    - Use this module to create a dynamic inventory from apex undercloud.
version_added: "2.2"
author: "Zhihui Wu"
options:
  baremetal_info:
    description:
      - return value from "openstack baremetal node list
        --fields instance_uuid properties provision_state --format json"
  server_info:
    description:
      - return value from "openstack server list --format json"
notes:
requirements:
    - Host 'apex-undercloud' is in ~/.ssh/config
'''

RETURN = '''
ansible_facts:
  description: facts collected for ansible
  returned: success
  type: dictionary
  contains:
    hosts:
      description: host grouped by role
      type: dict
    hosts_meta:
      description: hosts meta data indexed by hostname
      type: dict
'''

EXAMPLES = '''
---
- hosts: apex-undercloud
  tasks:
  - name: collect facts of apex hosts
    apex_generate_inventory:
      baremetal_info: "{{ baremetal_info.stdout | from_json }}"
      server_info: "{{ server_info.stdout | from_json }}"
'''


def generate_inventory(baremetal_info, server_info):
    """Generate ansible inventory in json format"""

    hosts = defaultdict(list)
    hosts_meta = {}

    for node in baremetal_info:
        if node['Provisioning State'].lower() == 'active':
            role = re.findall('.*profile:(compute|control)', node['Properties']['capabilities'])[0]
            for server in server_info:
                if server['ID'] == node['Instance UUID']:
                    node_ip = re.findall('.+=(\d+.\d+.\d+.\d+)$', server['Networks'])[0]
                    hosts[role].append(node_ip)
                    # To match ssh.cfg.j2 template
                    hosts_meta[node_ip] = {'ansible_ssh_host': node_ip,
                                           'ansible_user': 'heat-admin'}

    for host in hosts:
        hosts[host].sort()

    return {'hosts': hosts, 'hosts_meta': hosts_meta}


def main():
    module = AnsibleModule(
        argument_spec=dict(
            baremetal_info=dict(type='list'),
            server_info=dict(type='list')
        )
    )

    baremetal_info = module.params['baremetal_info']
    server_info = module.params['server_info']

    module.exit_json(changed=True,
                     ansible_facts=generate_inventory(baremetal_info, server_info))


if __name__ == '__main__':
    main()