1 # OSM devops/charms - Ansible charm inside OSM devops
3 # Copyright 2017-2018 Universidad Carlos III de Madrid
4 # Copyright 2018 Altran
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 from charmhelpers
.core
.hookenv
import (
26 from charms
.reactive
import (
27 remove_state
as remove_flag
,
28 set_state
as set_flag
,
31 import charms
.sshproxy
33 from subprocess
import (
39 #from charms.ansible import apply_playbook
46 # Sets the status of the charm to show in OSM: configured
47 @when('config.changed')
49 set_flag('ansible-charm.configured')
50 status_set('active', 'ready!')
54 # Edits ansible config files and executes ansible-playbook
55 @when('ansible-charm.configured')
56 @when('actions.ansible-playbook')
57 def ansible_playbook():
59 # Retrieve the ssh parameter
61 # edit ansible hosts file with the VNF parameters
62 h
= open("/etc/ansible/hosts", "wt")
64 h1
= "{} ansible_connection=ssh ansible_ssh_user={} ansible_ssh_pass={} ansible_python_interpreter=/usr/bin/python3\n".format(cfg
['ssh-hostname'],cfg
['ssh-username'],cfg
['ssh-password'])
67 # edit ansible config to enable ssh connection with th VNF
68 c
= open("/etc/ansible/ansible.cfg", "wt")
69 c
.write("[defaults]\n")
70 c
.write("host_key_checking = False\n")
72 # execute the ansible playbook
73 path
= find('playbook.yaml','/var/lib/juju/agents/')
74 call
= ['ansible-playbook', path
]
75 subprocess
.check_call(call
)
76 except Exception as e
:
77 action_fail('command failed: {}, errors: {}'.format(e
, e
.output
))
78 remove_flag('actions.ansible-playbook')
81 remove_flag('actions.ansible-playbook')
84 # Function to find the playbook path
85 def find(pattern
, path
):
87 for root
, dirs
, files
in os
.walk(path
):
89 if fnmatch
.fnmatch(name
, pattern
):
90 result
= os
.path
.join(root
, name
)