How do you run playbook from charm?

fengxia fxia1 at lenovo.com
Tue Jul 11 20:20:15 UTC 2017


Hi Juju,

I'm curious how you run playbook from charms?

Here are two approaches I have done so far. It gets me going. But I'm 
wondering whether there is a better way to do this. Comments?

------------------------------------------------------------------

Previously I have been using charms.ansible 
<https://github.com/chuckbutler/charms.ansible>. But I always felt 
calling CLI isn't as flexible as using a programming API. Inspired by 
this blog 
<https://serversforhackers.com/running-ansible-2-programmatically>, I 
created a new layer-ansible that takes advantage of the Ansible Python 
API <http://docs.ansible.com/ansible/dev_guide/developing_api.html>.

I have posted the code here <https://github.com/lenovo/charms.ansible>, 
which is forked from original one and two methods are completely 
compatible (two code bases don't really overlap). By pointing LAYER_PATH 
to this clone, I can use either method to run playbook. I kept both in 
one repo for convenience.

To use it in charm is pretty simple (see details 
<https://github.com/lenovo/charms.ansible#charm-integration>):

 1.

    Include layer. In|layer.yaml|:

    includes:
       -'layer:basic'
       -'layer:ansible'

 2.

    Create a|playbooks|folder and place /playbooks here:

    |. ├── config.yaml ├── icon.svg ├── layer.yaml ├── metadata.yaml ├──
    playbooks │   └── test.yaml └── reactive └── solution.py |

 3.

    Using|config.yaml|to pass in playbook for each action that is
    defined in the charm states. For example, define|test.yaml|for an
    action in|state-0|:

    options:
       state-0-playbook:
         type:string
         default:"test.yaml"
         description:"Playbook for..."

 4.

    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|value|sth|must match playbook run call (see below).

 5.

    In charm|.py|file,|from charms.layer.task import Runner|, then
    in|state-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()

-- 
Feng xia
Engineer
Lenovo USA

Phone: 5088011794
fxia1 at lenovo.com
  	
Lenovo.com
Twitter | Facebook | Instagram | Blogs | Forums

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/juju/attachments/20170711/beec2779/attachment.html>


More information about the Juju mailing list