updated to include modules and expand use of how the region variable is set.
diff --git a/library/cassandra_hosts.py b/library/cassandra_hosts.py index a3b086f..db2c2b0 100644 --- a/library/cassandra_hosts.py +++ b/library/cassandra_hosts.py
@@ -1,7 +1,8 @@ from ansible.module_utils.basic import * import ast import json -import future_builtins +import tempfile +import os GROUPS = 'groups' PUBLIC_ADDRESS = 'public_address' @@ -86,8 +87,9 @@ inventory_hostname = module.params['inventory_hostname'] hostvars = module.params['hostvars'] - json_file_name = '/tmp/hostvars_params.json' - with open(json_file_name, 'w') as hostvars_file: + json_file = '/tmp/hostvars_params.json' + # json_file = tempfile.mkstemp(suffix='json', text=True) + with open(json_file, 'w') as hostvars_file: hostvars_file.write(hostvars) # hostvars = hostvars.decode('base64') @@ -99,13 +101,16 @@ hostvars = hostvars.replace(": u'", ": '") hostvars = hostvars.replace("[u'", "['") hostvars = hostvars.replace("'", "\"") - with open('hostvars_params.json', 'w') as file: + with open(json_file, 'w') as file: file.write(hostvars) try: hostvars = ast.literal_eval(hostvars) except SyntaxError as e: msg = "ast.literal_eval conversion failed on line {0} with {1}".format(e.lineno, e.msg) + msg += "This occurred due to an operating system setting. There is a way around." + msg += "This means that you will need re-run with --tags=apigee-silent-config to generate the silent-install.conf file." + msg += "Then complete the installation with --skip-tags=os-pre-req,apigee-pre-req." module.fail_json( changed=False, msg=msg, @@ -113,7 +118,7 @@ return hostvars = json.dumps(hostvars) - with open('hostvars_dumps.json', 'w') as hostvars_file: + with open(json_file, 'w') as hostvars_file: hostvars_file.write(hostvars) try:
diff --git a/library/what_region.py b/library/what_region.py new file mode 100644 index 0000000..c97b4fd --- /dev/null +++ b/library/what_region.py
@@ -0,0 +1,82 @@ +from ansible.module_utils.basic import * +import ast +import json +import tempfile + + + + +def main(): + module = AnsibleModule( + argument_spec=dict( + inventory_hostname=dict(required=True, type='str'), + groups=dict(required=True, type="str") + ) + ) + + inventory_hostname = module.params['inventory_hostname'] + groups = module.params['groups'] + json_file_name = '/tmp/hostvars_params.json' + json_file = tempfile.mkstemp(suffix='json', text=True) + with open(json_file_name, 'w') as hostvars_file: + hostvars_file.write(hostvars) + + # hostvars = hostvars.decode('base64') + try: + hostvars = ast.literal_eval(hostvars) + except SyntaxError as e: + hostvars = hostvars.replace('{u', '{') + hostvars = hostvars.replace(", u'", ", '") + hostvars = hostvars.replace(": u'", ": '") + hostvars = hostvars.replace("[u'", "['") + hostvars = hostvars.replace("'", "\"") + with open('hostvars_params.json', 'w') as file: + file.write(hostvars) + + try: + hostvars = ast.literal_eval(hostvars) + except SyntaxError as e: + msg = "ast.literal_eval conversion failed on line {0} with {1}".format(e.lineno, e.msg) + module.fail_json( + changed=False, + msg=msg, + ) + return + + hostvars = json.dumps(hostvars) + with open('hostvars_dumps.json', 'w') as hostvars_file: + hostvars_file.write(hostvars) + + try: + hostvars = json.loads(hostvars) + except SyntaxError as e: + msg = "json.loads conversion failed: {0} {1}".format(e.lineno, e.msg) + module.fail_json( + changed=False, + msg=msg, + ) + return + + + try: + cass_hosts = build_cass_hosts_config(inventory_hostname, hostvars) + except SyntaxError as e: + msg = "build_cass_hosts_config failed on line {0} with {1}".format(e.lineno, e.msg) + module.fail_json( + changed=False, + msg=msg, + ) + return + + cass_hosts = json.dumps(cass_hosts) + cass_hosts = json.loads(cass_hosts) + + module.exit_json( + changed=True, + ansible_facts=dict( + cassandra_hosts=cass_hosts + ) + ) + +if __name__ == '__main__': + main()
diff --git a/tasks/main.yml b/tasks/main.yml index fcbb69b..1a0ac14 100644 --- a/tasks/main.yml +++ b/tasks/main.yml
@@ -1,7 +1,6 @@ --- # tasks file for apigee-opdk-setup-silent-file - name: Remove the old response file - become: yes file: path: "{{ opdk_installation_config_file }}" state: absent
diff --git a/templates/silent-install.conf.j2 b/templates/silent-install.conf.j2 index e31f37d..00682ba 100644 --- a/templates/silent-install.conf.j2 +++ b/templates/silent-install.conf.j2
@@ -21,15 +21,20 @@ ENABLE_AX={{ opdk_enable_ax }} MP_POD={{ opdk_mp_pod }} +{# TODO: Build a module that discover the region of the inventory_hostname and update the REGION #} {% if groups['dc-1'] is defined and inventory_hostname in groups['dc-1'] %} REGION=dc-1 {% elif groups['dc-2'] is defined and inventory_hostname in groups['dc-2'] %} REGION=dc-2 +{% elif groups['dc-3'] is defined and inventory_hostname in groups['dc-3'] %} +REGION=dc-3 {% endif %} USE_ZK_CLUSTER={{ opdk_use_zk_cluster }} +{# TODO: Build a module to construct the ZK_HOSTS value#} ZK_HOSTS="{% for host in groups['ds'] %}{{ hostvars[host][interface_name].ipv4.address }}{% if hostvars[host]['zk_observer'] is defined and hostvars[host]['zk_observer'] == 'true' %}:observer{% endif %}{% if not loop.last %} {% endif %}{% endfor %}" +{# TODO: Build a module to construct the ZK_CLIENT_HOSTS value#} {% if groups['dc-1'] is defined and inventory_hostname in groups['dc-1'] %} ZK_CLIENT_HOSTS="{% for host in groups['dc-1-ds'] %}{{ hostvars[host][interface_name].ipv4.address }}{% if not loop.last %} {% endif %}{% endfor %}" {% elif groups['dc-2'] is defined and inventory_hostname in groups['dc-2']%}