summaryrefslogtreecommitdiffstats
path: root/qtip
diff options
context:
space:
mode:
authorYujun Zhang <zhang.yujunz@zte.com.cn>2017-04-07 07:31:51 +0000
committerGerrit Code Review <gerrit@opnfv.org>2017-04-07 07:31:51 +0000
commit02e2c3372a654ffeb6cb34c6a0076da51cb568ea (patch)
treecb4c565ca0cd470c8c171ef623d49b891758e513 /qtip
parent135e068512d41da5e65737e4acae763d511d71ac (diff)
parent62460407a8fbcc99c154df3eabb8732c1908992e (diff)
Merge "Add ansible module `fuel`"
Diffstat (limited to 'qtip')
-rw-r--r--qtip/ansible_library/__init__.py0
-rw-r--r--qtip/ansible_library/modules/__init__.py0
-rw-r--r--qtip/ansible_library/modules/fuel.py124
3 files changed, 124 insertions, 0 deletions
diff --git a/qtip/ansible_library/__init__.py b/qtip/ansible_library/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/qtip/ansible_library/__init__.py
diff --git a/qtip/ansible_library/modules/__init__.py b/qtip/ansible_library/modules/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/qtip/ansible_library/modules/__init__.py
diff --git a/qtip/ansible_library/modules/fuel.py b/qtip/ansible_library/modules/fuel.py
new file mode 100644
index 00000000..ea2d78b9
--- /dev/null
+++ b/qtip/ansible_library/modules/fuel.py
@@ -0,0 +1,124 @@
+#!/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 json
+
+from ansible.module_utils.basic import AnsibleModule
+
+
+ANSIBLE_METADATA = {'metadata_version': '1.0',
+ 'status': ['preview'],
+ 'supported_by': 'community'}
+
+DOCUMENTATION = '''
+---
+module: fuel
+short_description: collecting facts from fuel environments
+description:
+ - Use this module to create a dynamic inventory from fuel master.
+version_added: "2.2"
+author: "Yujun Zhang (@yujunz)"
+options:
+notes:
+requirements:
+ - Host 'fuel-master' is in ~/.ssh/config
+'''
+
+RETURN = '''
+ansible_facts:
+ description: facts collected for ansible
+ returned: success
+ type: dictionary
+ contains:
+ hosts:
+ description: host grouped by hostname, cluster, role and manufacture
+ type: dict
+ hosts_meta:
+ description: hosts meta data indexed by hostname
+ type: dict
+'''
+
+EXAMPLES = '''
+---
+- hosts: fuel-master
+ tasks:
+ - name: collect facts of fuel hosts
+ fuel:
+ - debug: var=hostvars
+ - name: add compute node to ansible inventory
+ add_host:
+ name: "{{ hosts_meta[item]['ip'] }}"
+ groups: fuel-compute
+ ansible_user: root
+ ansible_ssh_common_args: '-o StrictHostKeyChecking=No -o ProxyJump=fuel-master'
+ with_items: "{{ hosts.compute }}"
+- hosts: fuel-compute
+ tasks:
+ - name: check ssh connection
+ ping:
+'''
+
+
+def generate_inventory(nodes):
+ """Generate ansible inventory from node list in json format
+
+ Modified from https://github.com/martineg/ansible-fuel-inventory/blob/master/fuel.py
+ """
+ hosts = defaultdict(list)
+ hosts_meta = {}
+
+ for node in nodes:
+ # skip deleting, offline, deploying and discovering/unprovisioned nodes
+ if node['pending_deletion'] or (not node['online']) \
+ or node['status'] == 'deploying' or node['status'] == 'discover':
+ continue
+
+ hostname = node['hostname']
+ cluster_id = node['cluster']
+ hw_vendor = node['meta']['system']['manufacturer'].lower()
+
+ [hosts[role.strip()].append(hostname) for role in node['roles'].split(",")]
+ hosts["cluster-{0}".format(cluster_id)].append(hostname)
+ hosts["hw-{0}-servers".format(hw_vendor)].append(hostname)
+
+ node_meta = {
+ 'name': node['name'],
+ 'online': node['online'],
+ 'os_platform': node['os_platform'],
+ 'status': node['status'],
+ 'ip': node['ip'],
+ 'mac': node['mac'],
+ 'cluster': cluster_id,
+ 'ansible_ssh_host': node['ip']
+ }
+ hosts["node-{}".format(node['id'])].append(hostname)
+ hosts_meta[hostname] = node_meta
+
+ return {'hosts': hosts, 'hosts_meta': hosts_meta}
+
+
+def main():
+ module = AnsibleModule(argument_spec=dict())
+
+ cmd = [module.get_bin_path('fuel', True), 'node', '--json']
+ (rc, out, err) = module.run_command(cmd)
+
+ if rc is not None and rc != 0:
+ return module.fail_json(msg=err)
+
+ nodes = json.loads(out)
+
+ module.exit_json(changed=False, ansible_facts=generate_inventory(nodes))
+
+
+if __name__ == '__main__':
+ main()