<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <p>Hi Juju,</p>
    <p>I'm curious how you run playbook from charms? <br>
    </p>
    <p>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?</p>
    <p>------------------------------------------------------------------<br>
    </p>
    <p>Previously I have been using <a moz-do-not-send="true"
        href="https://github.com/chuckbutler/charms.ansible">charms.ansible</a>.
      But I always felt calling CLI isn't as flexible as using a
      programming API. Inspired by this <a moz-do-not-send="true"
        href="https://serversforhackers.com/running-ansible-2-programmatically">blog</a>,
      I created a new layer-ansible that takes advantage of the <a
        moz-do-not-send="true"
        href="http://docs.ansible.com/ansible/dev_guide/developing_api.html">Ansible
        Python API</a>.</p>
    <p>I have posted the <a moz-do-not-send="true"
        href="https://github.com/lenovo/charms.ansible">code here</a>,
      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.</p>
    <p>To use it in charm is pretty simple (see <a
        moz-do-not-send="true"
        href="https://github.com/lenovo/charms.ansible#charm-integration">details</a>):<br>
    </p>
    <ol style="box-sizing: border-box; padding-left: 2em; margin-top:
      0px; margin-bottom: 0px !important; color: rgb(36, 41, 46);
      font-family: -apple-system, BlinkMacSystemFont, "Segoe
      UI", Helvetica, Arial, sans-serif, "Apple Color
      Emoji", "Segoe UI Emoji", "Segoe UI
      Symbol"; font-size: 16px; font-style: normal;
      font-variant-ligatures: normal; font-variant-caps: normal;
      font-weight: normal; letter-spacing: normal; orphans: 2;
      text-align: start; text-indent: 0px; text-transform: none;
      white-space: normal; widows: 2; word-spacing: 0px;
      -webkit-text-stroke-width: 0px; text-decoration-style: initial;
      text-decoration-color: initial;">
      <li style="box-sizing: border-box;">
        <p style="box-sizing: border-box; margin-top: 16px;
          margin-bottom: 16px;">Include layer. In<span> </span><code
            style="box-sizing: border-box; font-family: SFMono-Regular,
            Consolas, "Liberation Mono", Menlo, Courier,
            monospace; font-size: 13.6px; padding: 0.2em 0px; margin:
            0px; background-color: rgba(27, 31, 35, 0.05);
            border-radius: 3px;">layer.yaml</code>:</p>
        <div class="highlight highlight-source-yaml" style="box-sizing:
          border-box; margin-bottom: 16px;">
          <pre style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin-top: 0px; margin-bottom: 0px; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(246, 248, 250); border-radius: 3px; word-break: normal;"><span class="pl-ent" style="box-sizing: border-box; color: rgb(34, 134, 58);">includes</span>:
  - <span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">'</span>layer:basic<span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">'</span></span>
  - <span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">'</span>layer:ansible<span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">'</span></span></pre>
        </div>
      </li>
      <li style="box-sizing: border-box; margin-top: 0.25em;">
        <p style="box-sizing: border-box; margin-top: 16px;
          margin-bottom: 16px;">Create a<span> </span><code
            style="box-sizing: border-box; font-family: SFMono-Regular,
            Consolas, "Liberation Mono", Menlo, Courier,
            monospace; font-size: 13.6px; padding: 0.2em 0px; margin:
            0px; background-color: rgba(27, 31, 35, 0.05);
            border-radius: 3px;">playbooks</code><span> </span>folder
          and place /playbooks here:</p>
        <pre style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin-top: 0px; margin-bottom: 16px; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(246, 248, 250); border-radius: 3px;"><code style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; padding: 0px; margin: 0px; background: transparent; border-radius: 3px; word-break: normal; white-space: pre; border: 0px; display: inline; overflow: visible; line-height: inherit; word-wrap: normal;">.
├── config.yaml
├── icon.svg
├── layer.yaml
├── metadata.yaml
├── playbooks
│   └── test.yaml
└── reactive
    └── solution.py
</code></pre>
      </li>
      <li style="box-sizing: border-box; margin-top: 0.25em;">
        <p style="box-sizing: border-box; margin-top: 16px;
          margin-bottom: 16px;">Using<span> </span><code
            style="box-sizing: border-box; font-family: SFMono-Regular,
            Consolas, "Liberation Mono", Menlo, Courier,
            monospace; font-size: 13.6px; padding: 0.2em 0px; margin:
            0px; background-color: rgba(27, 31, 35, 0.05);
            border-radius: 3px;">config.yaml</code><span> </span>to pass
          in playbook for each action that is defined in the charm
          states. For example, define<span> </span><code
            style="box-sizing: border-box; font-family: SFMono-Regular,
            Consolas, "Liberation Mono", Menlo, Courier,
            monospace; font-size: 13.6px; padding: 0.2em 0px; margin:
            0px; background-color: rgba(27, 31, 35, 0.05);
            border-radius: 3px;">test.yaml</code><span> </span>for an
          action in<span> </span><code style="box-sizing: border-box;
            font-family: SFMono-Regular, Consolas, "Liberation
            Mono", Menlo, Courier, monospace; font-size: 13.6px;
            padding: 0.2em 0px; margin: 0px; background-color: rgba(27,
            31, 35, 0.05); border-radius: 3px;">state-0</code>:</p>
        <div class="highlight highlight-source-yaml" style="box-sizing:
          border-box; margin-bottom: 16px;">
          <pre style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin-top: 0px; margin-bottom: 0px; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(246, 248, 250); border-radius: 3px; word-break: normal;"><span class="pl-ent" style="box-sizing: border-box; color: rgb(34, 134, 58);">options</span>:
  <span class="pl-ent" style="box-sizing: border-box; color: rgb(34, 134, 58);">state-0-playbook</span>:
    <span class="pl-ent" style="box-sizing: border-box; color: rgb(34, 134, 58);">type</span>: <span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);">string</span>
    <span class="pl-ent" style="box-sizing: border-box; color: rgb(34, 134, 58);">default</span>: <span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">"</span>test.yaml<span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">"</span></span>
    <span class="pl-ent" style="box-sizing: border-box; color: rgb(34, 134, 58);">description</span>: <span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">"</span>Playbook for...<span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">"</span></span></pre>
        </div>
      </li>
      <li style="box-sizing: border-box; margin-top: 0.25em;">
        <p style="box-sizing: border-box; margin-top: 16px;
          margin-bottom: 16px;">Define the playbook. For example, a<span> </span><em
            style="box-sizing: border-box;">hello world</em><span> </span>that
          will create a file `/tmp/testfile.txt'.</p>
        <div class="highlight highlight-source-yaml" style="box-sizing:
          border-box; margin-bottom: 16px;">
          <pre style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin-top: 0px; margin-bottom: 0px; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(246, 248, 250); border-radius: 3px; word-break: normal;">- <span class="pl-ent" style="box-sizing: border-box; color: rgb(34, 134, 58);">name</span>: <span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);">This is a hello-world example</span>
  <span class="pl-ent" style="box-sizing: border-box; color: rgb(34, 134, 58);">hosts</span>: <span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);">127.0.0.1</span>
  <span class="pl-ent" style="box-sizing: border-box; color: rgb(34, 134, 58);">tasks</span>:
  - <span class="pl-ent" style="box-sizing: border-box; color: rgb(34, 134, 58);">name</span>: <span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);">Create a file called '/tmp/testfile.txt' with the content 'hello world'.</span>
    <span class="pl-ent" style="box-sizing: border-box; color: rgb(34, 134, 58);">copy</span>: <span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);">content="hello world\n" dest=/tmp/testfile.txt</span>
    <span class="pl-ent" style="box-sizing: border-box; color: rgb(34, 134, 58);">tags</span>:
      - <span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);">sth</span></pre>
        </div>
        <p style="box-sizing: border-box; margin-top: 16px;
          margin-bottom: 16px;">Note that<span> </span><code
            style="box-sizing: border-box; font-family: SFMono-Regular,
            Consolas, "Liberation Mono", Menlo, Courier,
            monospace; font-size: 13.6px; padding: 0.2em 0px; margin:
            0px; background-color: rgba(27, 31, 35, 0.05);
            border-radius: 3px;">tags</code><span> </span>value<span> </span><code
            style="box-sizing: border-box; font-family: SFMono-Regular,
            Consolas, "Liberation Mono", Menlo, Courier,
            monospace; font-size: 13.6px; padding: 0.2em 0px; margin:
            0px; background-color: rgba(27, 31, 35, 0.05);
            border-radius: 3px;">sth</code><span> </span>must match
          playbook run call (see below).</p>
      </li>
      <li style="box-sizing: border-box; margin-top: 0.25em;">
        <p style="box-sizing: border-box; margin-top: 16px;
          margin-bottom: 16px;">In charm<span> </span><code
            style="box-sizing: border-box; font-family: SFMono-Regular,
            Consolas, "Liberation Mono", Menlo, Courier,
            monospace; font-size: 13.6px; padding: 0.2em 0px; margin:
            0px; background-color: rgba(27, 31, 35, 0.05);
            border-radius: 3px;">.py</code><span> </span>file,<span> </span><code
            style="box-sizing: border-box; font-family: SFMono-Regular,
            Consolas, "Liberation Mono", Menlo, Courier,
            monospace; font-size: 13.6px; padding: 0.2em 0px; margin:
            0px; background-color: rgba(27, 31, 35, 0.05);
            border-radius: 3px;">from charms.layer.task import Runner</code>,
          then in<span> </span><code style="box-sizing: border-box;
            font-family: SFMono-Regular, Consolas, "Liberation
            Mono", Menlo, Courier, monospace; font-size: 13.6px;
            padding: 0.2em 0px; margin: 0px; background-color: rgba(27,
            31, 35, 0.05); border-radius: 3px;">state-0</code><span> </span>to
          call given playbook:</p>
        <div class="highlight highlight-source-python"
          style="box-sizing: border-box; margin-bottom: 16px;">
          <pre style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; margin-top: 0px; margin-bottom: 0px; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(246, 248, 250); border-radius: 3px; word-break: normal;">playbook <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> config[<span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">'</span>state-0-playbook<span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">'</span></span>]
runner <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> Runner(
    <span class="pl-v" style="box-sizing: border-box; color: rgb(227, 98, 9);">tags</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> <span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">'</span>sth<span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">'</span></span>, <span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);"><span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);">#</span> <-- must match the tag in the playbook</span>
    <span class="pl-v" style="box-sizing: border-box; color: rgb(227, 98, 9);">connection</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> <span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">'</span>local<span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">'</span></span>, <span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);"><span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);">#</span> <-- must be "local"</span>
    <span class="pl-v" style="box-sizing: border-box; color: rgb(227, 98, 9);">hostnames</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> <span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">'</span>127.0.0.1<span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">'</span></span>, <span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);"><span class="pl-c" style="box-sizing: border-box; color: rgb(106, 115, 125);">#</span> <-- assuming execution in localhost</span>
    <span class="pl-v" style="box-sizing: border-box; color: rgb(227, 98, 9);">playbooks</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> [playbook],
    <span class="pl-v" style="box-sizing: border-box; color: rgb(227, 98, 9);">private_key_file</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> <span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">'</span><span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">'</span></span>,
    <span class="pl-v" style="box-sizing: border-box; color: rgb(227, 98, 9);">run_data</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> {},
    <span class="pl-v" style="box-sizing: border-box; color: rgb(227, 98, 9);">become_pass</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> <span class="pl-s" style="box-sizing: border-box; color: rgb(3, 47, 98);"><span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">'</span><span class="pl-pds" style="box-sizing: border-box; color: rgb(3, 47, 98);">'</span></span>,
    <span class="pl-v" style="box-sizing: border-box; color: rgb(227, 98, 9);">verbosity</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 92, 197);">0</span>
)
stats <span class="pl-k" style="box-sizing: border-box; color: rgb(215, 58, 73);">=</span> runner.run()</pre>
        </div>
      </li>
    </ol>
    <pre class="moz-signature" cols="72">-- 
Feng xia
Engineer
Lenovo USA

Phone: 5088011794
<a class="moz-txt-link-abbreviated" href="mailto:fxia1@lenovo.com">fxia1@lenovo.com</a>
        
Lenovo.com 
Twitter | Facebook | Instagram | Blogs | Forums
</pre>
  </body>
</html>