This was initially based on charms.ansible, but have later on adopted implementation that requires fewer dependencies and prerequisite steps so I think it is a cleaner solution than its original.
Design objectives:
- Design as reusable layer(s)
- Be compatible with Ubuntu and CentOS
- Simple pattern to execute a playbook
Assumption:
- Playbooks will be local (in charm) so to maintain the atomic nature of a HW charm — the model contains both the declaration of attributes and actions to handle runtime state transitions.
This method is inspired by this article. This is to take advantage of the Ansible Python API.
-
Install prerequisites. Installing
Ansible
will fail on a vanilla Ubuntu because it misses a few dependencies. Using layer-basic by listing them out inlayer.yaml
:includes: - 'layer:basic' options: basic: packages: - libffi-dev - libssl-dev - python - python3-dev
- Install Ansible. Use Python wheel supported
by layer-basic. In
wheelhouse.txt
:
```
ansible==2.2.0
```
-
ansible.cfg
. Instead of using a global config, this is local so each charm can have its own variation if desired.[defaults] inventory = ./hosts log_path = /var/log/ansible/ansible.log remote_tmp = $HOME/.ansible/tmp local_tmp = $HOME/.ansible/tmp [ssh_connection] ssh_args = -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -o ControlMaster=auto -o ControlPersist=60s control_path = ~/.ansible/cp/ansible-ssh-%%h-%%p-%%r
-
Options. Constructed a class to be the abstraction of Ansible options:
class Options(object): """ Options class to replace Ansible OptParser """ .... verbosity=None, inventory=None, listhosts=None, subset=None, module_paths=None, extra_vars=None, forks=None, ask_vault_pass=None, vault_password_files=None, new_vault_password_file=None, output_file=None, tags=None, skip_tags=[], one_line=None, tree=None, ask_sudo_pass=None, ask_su_pass=None, sudo=None, sudo_user=None, become=None, become_method=None, become_user=None, become_ask_pass=None, ask_pass=None, private_key_file=None, remote_user=None, connection=None, timeout=None, ssh_common_args=None, sftp_extra_args=None, scp_extra_args=None, ssh_extra_args=None, poll_interval=None, seconds=None, check=None, syntax=None, diff=None, force_handlers=None, flush_cache=None, listtasks=None, listtags=[], module_path=None
-
Playbook execution. Running it is to use Ansible's API call
PlaybookExecutor
.self.pbex = playbook_executor.PlaybookExecutor( playbooks=pbs, inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader, options=self.options, passwords=passwords) .... self.pbex.run()
Integrating with charm takes the followings:
-
Include layer. In
layer.yaml
:includes: - 'layer:basic' - 'layer:ansible'
-
Create a
playbooks
folder and place playbooks here:. ├── config.yaml ├── icon.svg ├── layer.yaml ├── metadata.yaml ├── playbooks │ └── test.yaml └── reactive └── solution.py
-
Using
config.yaml
to pass in playbook for each action that is defined in the charm states. For example, definetest.yaml
for an action instate-0
:options: state-0-playbook: type: string default: "test.yaml" description: "Playbook for..."
-
Define the playbook. For example, a hello world that will create a file `/tmp/testfile.txt'.
- name: This is a hello-world example hosts: 127.0.0.1 tasks: - name: Create a file called '/tmp/testfile.txt' with the content 'hello world'. copy: content="hello world\n" dest=/tmp/testfile.txt tags: - sth
Note that
tags
valuesth
must match playbook run call (see below). -
In charm
.py
file,from charms.layer.task import Runner
, then instate-0
to call given playbook:playbook = config['state-0-playbook'] runner = Runner( tags = 'sth', # <-- must match the tag in the playbook connection = 'local', # <-- must be "local" hostnames = '127.0.0.1', # <-- assuming execution in localhost playbooks = [playbook], private_key_file = '', run_data = {}, become_pass = '', verbosity = 0 ) stats = runner.run()