Adding extra-vars to playooks
[osm/devops.git] / descriptor-packages / tools / charm-generator / generator / ansible-charm / templates / ansible_lib.py.j2
1 {#-
2 # Copyright 2019 Whitestack, LLC
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 #         http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 #
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: esousa@whitestack.com or glavado@whitestack.com
18 -#}
19 {%- if license is defined -%}
20 # Copyright {{ license.year }} {{ license.company }}
21 #
22 # Licensed under the Apache License, Version 2.0 (the "License"); you may
23 # not use this file except in compliance with the License. You may obtain
24 # a copy of the License at
25 #
26 #         http://www.apache.org/licenses/LICENSE-2.0
27 #
28 # Unless required by applicable law or agreed to in writing, software
29 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
30 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
31 # License for the specific language governing permissions and limitations
32 # under the License.
33 #
34 # For those usages not covered by the Apache License, Version 2.0 please
35 # contact: {{ license.email }}
36 {%- endif %}
37
38 import fnmatch
39 import os
40 import yaml
41 import subprocess
42
43 from charmhelpers.core.hookenv import config
44
45
46 def create_hosts(cfg, hosts):
47     inventory_path = '/etc/ansible/hosts'
48
49     with open(inventory_path, 'w') as f:
50         f.write('[{}]\n'.format(hosts))
51         h1 = '{0} ansible_connection=ssh ansible_ssh_user={1} ansible_ssh_pass={2} ' \
52              'ansible_python_interpreter=/usr/bin/python3\n'.format(cfg['ssh-hostname'], cfg['ssh-username'],
53                                                                     cfg['ssh-password'])
54         f.write(h1)
55
56
57 def create_ansible_cfg():
58     ansible_config_path = '/etc/ansible/ansible.cfg'
59
60     with open(ansible_config_path, 'w') as f:
61         f.write('[defaults]\n')
62         f.write('host_key_checking = False\n')
63
64
65 # Function to find the playbook path
66 def find(pattern, path):
67     result = ''
68     for root, dirs, files in os.walk(path):
69         for name in files:
70             if fnmatch.fnmatch(name, pattern):
71                 result = os.path.join(root, name)
72     return result
73
74
75 def execute_playbook(playbook_file, vars_dict=None):
76     playbook_path = find(playbook_file, '/var/lib/juju/agents/')
77
78     cfg = config()
79
80     with open(playbook_path, 'r') as f:
81         playbook_data = yaml.load(f)
82
83     hosts = 'all'
84     if 'hosts' in playbook_data[0].keys() and playbook_data[0]['hosts']:
85         hosts = playbook_data[0]['hosts']
86
87     create_ansible_cfg()
88     create_hosts(cfg, hosts)
89
90     call = 'ansible-playbook %s ' % playbook_path
91
92     if vars_dict and isinstance(vars_dict, dict) and len(vars_dict) > 0:
93         call += '--extra-vars '
94
95         string_var = ''
96         for v in vars_dict.items():
97             string_var += '%s=%s ' % v
98
99         string_var = string_var.strip()
100         call += '"%s"' % string_var
101
102     call = call.strip()
103     result = subprocess.check_output(call, shell=True)
104
105     return result