<div dir="ltr"><div><div><div><div><div><div><div>I am not Ansible expert, this maybe worth asking on Ansible users list,<br></div>but one thing which you can do is following:<br><br></div>in tasks/main.yml<br><br></div>- include: &quot;{{ item }}&quot;<br></div>  with_items: &quot;{{ resources | default(&#39;vms&#39;) }}.yml&quot;<br><br>then have following files:<br> - tasks/vms.yml<br> - tasks/clusters.yml<br> - tasks/disks.yml<br> - ...<br><br></div>then run it as:<br><br></div> $ ansible-playbook -e &quot;resources: [vms,disks]&quot; play.yml<br> $ ansible-playbook -e &quot;resources: [clusters] clusters_pattern=name=x*&quot; play.yml<br><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, May 11, 2017 at 6:34 PM, Nathanaël Blanchet <span dir="ltr">&lt;<a href="mailto:blanchet@abes.fr" target="_blank">blanchet@abes.fr</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
  
  <div bgcolor="#FFFFFF" text="#000000">
    <p>Thanks, all works as expected.</p>
    <p>Now, I try make roles.</p>
    <p>here it is : <br>
    </p>
    <p><b>cat /etc/ansible/roles/ovirt_vm_<wbr>facts/tasks/main.yml</b></p>
    <p>  - name: get vm facts<br>
          ovirt_vms_facts:<br>
            auth:<br>
              url: <a class="m_7298570659494759391moz-txt-link-freetext">https://</a>{{ engine }}.<a href="http://v100.abes.fr/ovirt-engine/api" target="_blank">v100.abes.fr/ovirt-engine/<wbr>api</a><br>
              username: &quot;{{ username }}&quot;<br>
              password: &quot;{{ password }}&quot;<br>
              insecure: &quot;{{ insecure }}&quot;<br>
            pattern: &quot;{{ pattern }}&quot;<br>
        - debug:<br>
            msg: &quot;{{ ovirt_vms | map(attribute=&#39;name&#39;) | join(&#39; &#39;) }}&quot;</p>
    <p><b>cat /etc/ansible/roles/ovirt_vm_<wbr>facts/vars/main.yml</b></p>
    <p>pattern: &quot;os=rhel_5x64 or os=rhel_5&quot;<br>
      engine: air<span class=""><br>
      username: admin@internal<br>
      password: ovirt123<br>
      insecure: true</span></p>
    <p>and I call those files with <b>/tmp/ovirt_vm_facts.yml</b> with
      extra variables if needed<br>
    </p>
    <p><span class="">- hosts: localhost<br>
        connection: local<br></span>
        roles:<br>
          - ovirt_vm_fact</p>
    <p>running ansible-playbook /tmp/ovirt_vm_facts.ym -e
      &#39;pattern=&quot;name=acier* and cluster=Dev&quot;&#39;<br>
    </p>
    <p><b>But..</b></p>
    <p>I don&#39;t want to do the same for each ansible module shipped with
      2.3.</p>
    <p>Knowing that each *facts module has the same structure
      auth/pattern, is there a way to play a generic playbook
      independant from the role, like:</p>
    <p><span class="">- hosts: localhost<br>
        connection: local<br></span>
        roles:<br>
          - ovirt_facts (with somewhere a variable &quot;myfact&quot;)<br>
    </p>
    <p>which would play an unique role <b>/etc/ansible/roles/ovirt_<wbr>facts/tasks/main.yml
        with</b></p>
    <p><br>
      - name: get vm facts<br>
          myfact<br>
            auth:<br>
              ....<br>
    </p>
    <p>Hope to be clear enough, thank your for your help.<br>
    </p><div><div class="h5">
    <br>
    <div class="m_7298570659494759391moz-cite-prefix">Le 10/05/2017 à 13:14, Ondra Machacek a
      écrit :<br>
    </div>
    <blockquote type="cite">
      <div dir="ltr"><br>
        <div class="gmail_extra"><br>
          <div class="gmail_quote">On Wed, May 10, 2017 at 12:38 PM,
            Nathanaël Blanchet <span dir="ltr">&lt;<a href="mailto:blanchet@abes.fr" target="_blank">blanchet@abes.fr</a>&gt;</span> wrote:<br>
            <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
              <div bgcolor="#FFFFFF"><span class="m_7298570659494759391gmail-"> Le 10/05/2017
                  à 10:29, Ondra Machacek a écrit :<br>
                  <blockquote type="cite">
                    <div dir="ltr">
                      <div>
                        <div>
                          <div>
                            <div>
                              <div>
                                <div>
                                  <div>
                                    <div>
                                      <div>
                                        <div>Not sure I understand. You
                                          can use Ansible module to list
                                          VMs, you<br>
                                        </div>
                                        can use ovirt_vms _facts[1]
                                        module.<br>
                                        <br>
                                      </div>
                                      For example to get only stopped
                                      VMs, you can use following:<br>
                                      <br>
                                    </div>
                                    - name: Get stopped VMs<br>
                                  </div>
                                    ovirt_vms_facts:<br>
                                </div>
                                    auth:<br>
                              </div>
                                    url: <a href="https://ovirt.example.com/ovirt-engine/api" target="_blank">https://ovirt.example.com/ovir<wbr>t-engine/api</a><br>
                            </div>
                                  username: admin@internal<br>
                          </div>
                                password: password<br>
                        </div>
                              ca_file: /etc/pki/ovirt-engine/ca.pem<br>
                      </div>
                          pattern: &quot;status = down&quot;<br>
                    </div>
                  </blockquote>
                </span> I want a list of vms sorted by any possible
                attributes, i.e. sorted by datacenter or cluster, not
                only by status.<br>
                <ul>
                  <li>For example, ovirt_vms_facts doesn&#39;t allow to get
                    the datacenter the vm belongs to , and the pattern
                    filter doesn&#39;t return a list of vms as expected:</li>
                </ul>
                <br>
                - hosts: localhost<br>
                  connection: local<br>
                  tasks:<span class="m_7298570659494759391gmail-"><br>
                    - name: Get stopped VMs<br>
                      ovirt_vms_facts:<br>
                        auth:<br>
                </span>         url: <a class="m_7298570659494759391gmail-m_344037001981379143moz-txt-link-freetext" href="https://acore.v100.abes.fr/ovirt-engine/api" target="_blank">https://acore.v100.abes.fr/ovi<wbr>rt-engine/api</a><br>
                        username: admin@internal<br>
                        password: ovirt123<br>
                        insecure: true<br>
                        pattern: name=centos* and cluster=west<br>
                  - debug:<br>
                      var: ovirt_vms<br>
                  - shell: echo &quot;{{ <a href="http://ovirt_vms.name" target="_blank">ovirt_vms.name</a>
                }}&quot;<br>
              </div>
            </blockquote>
            <div><br>
              <br>
            </div>
            <div>Ok, I understand, now.<br>
            </div>
            <div>You have few options here.<br>
            </div>
            <div>If you want for example only VMs from datacenter &#39;X&#39;,
              you can use following:<br>
              <br>
              - hosts: localhost<br>
                connection: local<br>
                tasks:<span class="m_7298570659494759391gmail-im"><br>
                  - name: Get VMs from datacenter X<br>
                    ovirt_vms_facts:</span><br>
              <span class="m_7298570659494759391gmail-im">       pattern: datacenter=X<br>
                      auth:<br>
              </span>         url: <a class="m_7298570659494759391gmail-m_344037001981379143moz-txt-link-freetext" href="https://acore.v100.abes.fr/ovirt-engine/api" target="_blank">https://acore.v100.abes.fr/ovi<wbr>rt-engine/api</a><br>
                      username: admin@internal<br>
                      password: ovirt123<br>
                      insecure: true<br>
                - debug:<br>
                    msg: &quot;{{ ovirt_vms | map(attribute=&#39;name&#39;) | list
              }}&quot;<br>
            </div>
            <div> </div>
            <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
              <div bgcolor="#FFFFFF"> <br>
                How can I successfully display this list of vms like
                described here :<br>
                <table cellpadding="4" border="1">
                  <tbody>
                    <tr>
                      <th class="m_7298570659494759391gmail-m_344037001981379143head">name</th>
                      <th class="m_7298570659494759391gmail-m_344037001981379143head">description</th>
                      <th class="m_7298570659494759391gmail-m_344037001981379143head">returned</th>
                      <th class="m_7298570659494759391gmail-m_344037001981379143head">type</th>
                      <th class="m_7298570659494759391gmail-m_344037001981379143head">sample</th>
                    </tr>
                    <tr>
                      <td> ovirt_vms </td>
                      <td> List of dictionaries describing the VMs. VM
                        attribues are mapped to dictionary keys, all VMs
                        attributes can be found at following url: <a class="m_7298570659494759391gmail-m_344037001981379143moz-txt-link-freetext" href="https://ovirt.example.com/ovirt-engine/api/model#types/vm" target="_blank">https://ovirt.example.com/ovir<wbr>t-engine/api/model#types/vm</a>.
                      </td>
                      <td align="center"> On success. </td>
                      <td align="center"> list </td>
                      <td align="center"> <br>
                      </td>
                    </tr>
                  </tbody>
                </table>
                <br>
                <ul>
                  <li>About the python SDK:</li>
                </ul>
                list_vms.py could be what I expect, but
                vm.datacenter_name doesn&#39;t exist</div>
            </blockquote>
            <div><br>
            </div>
            <div>Yes, because the &#39;data_center&#39; parameter isn&#39;t part of
              the VM type. Take a look here[1],<br>
            </div>
            <div>you will find there all attributes the VM has. As you
              can see there is no &#39;datacenter&#39;, but<br>
            </div>
            <div>there is a &#39;cluster&#39;, but as said in documentation,
              it&#39;s just a reference to the cluster. That<br>
            </div>
            <div>means that it contains the ID of the cluster and the
              &#39;href&#39; to the cluster, so you can get<br>
            </div>
            <div>the cluster object by following that reference using
              this code:<br>
              <br>
            </div>
            <div> vm_cluster = connection.follow_link(vm.<wbr>cluster)<br>
            </div>
            <div> print <a href="http://vm_cluster.name" target="_blank">vm_cluster.name</a><br>
              <br>
            </div>
            <div> # Same for datacenter:<br>
            </div>
            <div> vm_dc = connection.follow_link(vm_<wbr>cluster.data_center)<br>
            </div>
            <div> print <a href="http://vm_dc.name" target="_blank">vm_dc.name</a><br>
              <br>
            </div>
            What you need to understand is, what is the difference
            between the &#39;full&#39;<br>
          </div>
          <div class="gmail_quote">object and the reference to the
            object. It&#39;s good to take a look at the XML<br>
          </div>
          <div class="gmail_quote">output of the API and learn to read
            the API specification documentation,<br>
          </div>
          <div class="gmail_quote">when working with oVirt Ansible and
            Python SDK.<br>
          </div>
          <div class="gmail_quote">
            <div><br>
              [1] <a href="http://ovirt.github.io/ovirt-engine-api-model/master/#types/vm" target="_blank">http://ovirt.github.io/ovirt-<wbr>engine-api-model/master/#<wbr>types/vm</a><br>
            </div>
            <div> </div>
            <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
              <div bgcolor="#FFFFFF">
                <div>
                  <div class="m_7298570659494759391gmail-h5"><br>
                    <blockquote type="cite">
                      <div dir="ltr">
                        <div>
                          <div>
                            <div>
                              <div>
                                <div>
                                  <div>
                                    <div>
                                      <div>
                                        <div>
                                          <div><br>
                                            [1] <a href="http://docs.ansible.com/ansible/ovirt_vms_facts_module.html" target="_blank">http://docs.ansible.com/ansibl<wbr>e/ovirt_vms_facts_module.html</a></div>
                                        </div>
                                      </div>
                                    </div>
                                  </div>
                                </div>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                      <div class="gmail_extra"><br>
                        <div class="gmail_quote">On Wed, May 10, 2017 at
                          10:13 AM, Nathanaël Blanchet <span dir="ltr">&lt;<a href="mailto:blanchet@abes.fr" target="_blank">blanchet@abes.fr</a>&gt;</span>
                          wrote:<br>
                          <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
                            <div bgcolor="#FFFFFF">
                              <p>Thanks, I knew this python script, but
                                I believed it was possible to do the
                                same with an ansible module.</p>
                              <p>That&#39;s mean we need several tools to do
                                different tasks, it is not a very
                                convergent way to proceed... but it is
                                feasable.<br>
                              </p>
                              <div>
                                <div class="m_7298570659494759391gmail-m_344037001981379143h5">
                                  <br>
                                  <div class="m_7298570659494759391gmail-m_344037001981379143m_-2614652977139427754moz-cite-prefix">Le
                                    10/05/2017 à 07:56, Ondra Machacek a
                                    écrit :<br>
                                  </div>
                                  <blockquote type="cite">
                                    <div dir="ltr"><br>
                                      <div class="gmail_extra"><br>
                                        <div class="gmail_quote">On Tue,
                                          May 9, 2017 at 6:52 PM,
                                          Nathanaël Blanchet <span dir="ltr">&lt;<a href="mailto:blanchet@abes.fr" target="_blank">blanchet@abes.fr</a>&gt;</span>
                                          wrote:<br>
                                          <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hello,<br>
                                            <br>
                                            I didn&#39;t find anyway to easy
                                            list all my vms thanks to
                                            the ansible modules...<br>
                                            I tried the ovirt4.py script
                                            which is able to list the
                                            whole facts, so vms list,
                                            when the number of them is
                                            small in a test datacenter,
                                            but in a production
                                            datacenter, I get an issue:<br>
                                            <br>
                                              File &quot;./ovirt4.py&quot;, line
                                            262, in &lt;module&gt;<br>
                                                main()<br>
                                              File &quot;./ovirt4.py&quot;, line
                                            254, in main<br>
                                                vm_name=args.host,<br>
                                              File &quot;./ovirt4.py&quot;, line
                                            213, in get_data<br>
                                                vms[name] =
                                            get_dict_of_struct(connection,
                                            vm)<br>
                                              File &quot;./ovirt4.py&quot;, line
                                            185, in get_dict_of_struct<br>
                                                (<a href="http://device.name" rel="noreferrer" target="_blank">device.name</a>,
                                            [ip.address for ip in
                                            device.ips]) for device in
                                            devices<br>
                                              File &quot;./ovirt4.py&quot;, line
                                            185, in &lt;genexpr&gt;<br>
                                                (<a href="http://device.name" rel="noreferrer" target="_blank">device.name</a>,
                                            [ip.address for ip in
                                            device.ips]) for device in
                                            devices<br>
                                            TypeError: &#39;NoneType&#39; object
                                            is not iterable<br>
                                          </blockquote>
                                          <div><br>
                                          </div>
                                          <div>This error was fixed
                                            already, try to download
                                            latest ovirt4.py file.<br>
                                            <br>
                                          </div>
                                          <div>The other way is to use
                                            fatcs[1] module.<br>
                                            <br>
                                            [1] <a href="http://docs.ansible.com/ansible/ovirt_vms_facts_module.html" target="_blank">http://docs.ansible.com/ansibl<wbr>e/ovirt_vms_facts_module.html</a><br>
                                          </div>
                                          <div> </div>
                                          <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <br>
                                            <br>
                                            What is the simpliest way to
                                            get this basic information
                                            with sdk4??? (with sdk3 :
                                            ovirt-shell -E &quot;list vms&quot;)<br>
                                          </blockquote>
                                          <div><br>
                                          </div>
                                          <div>Please take a look at the
                                            following example:<br>
                                            <br>
                                             <a href="https://github.com/oVirt/ovirt-engine-sdk/blob/master/sdk/examples/list_vms.py" target="_blank">https://github.com/oVirt/ovir<wbr>t-engine-sdk/blob/master/sdk/e<wbr>xamples/list_vms.py</a><br>
                                          </div>
                                          <div> </div>
                                          <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <br>
                                            <br>
                                            <br>
                                            -- <br>
                                            Nathanaël Blanchet<br>
                                            <br>
                                            Supervision réseau<br>
                                            Pôle Infrastrutures
                                            Informatiques<br>
                                            227 avenue
                                            Professeur-Jean-Louis-Viala<br>
                                            34193 MONTPELLIER CEDEX 5   
                                               <br>
                                            Tél. 33 (0)4 67 54 84 55<br>
                                            Fax  33 (0)4 67 54 84 14<br>
                                            <a href="mailto:blanchet@abes.fr" target="_blank">blanchet@abes.fr</a><br>
                                            <br>
______________________________<wbr>_________________<br>
                                            Users mailing list<br>
                                            <a href="mailto:Users@ovirt.org" target="_blank">Users@ovirt.org</a><br>
                                            <a href="http://lists.ovirt.org/mailman/listinfo/users" rel="noreferrer" target="_blank">http://lists.ovirt.org/mailman<wbr>/listinfo/users</a><br>
                                          </blockquote>
                                        </div>
                                        <br>
                                      </div>
                                    </div>
                                  </blockquote>
                                  <br>
                                  <pre class="m_7298570659494759391gmail-m_344037001981379143m_-2614652977139427754moz-signature" cols="72">-- 
Nathanaël Blanchet

Supervision réseau
Pôle Infrastrutures Informatiques
227 avenue Professeur-Jean-Louis-Viala
34193 MONTPELLIER CEDEX 5         
Tél. 33 (0)4 67 54 84 55
Fax  33 (0)4 67 54 84 14
<a class="m_7298570659494759391gmail-m_344037001981379143m_-2614652977139427754moz-txt-link-abbreviated" href="mailto:blanchet@abes.fr" target="_blank">blanchet@abes.fr</a> </pre>
                                </div>
                              </div>
                            </div>
                          </blockquote>
                        </div>
                        <br>
                      </div>
                    </blockquote>
                    <br>
                    <pre class="m_7298570659494759391gmail-m_344037001981379143moz-signature" cols="72">-- 
Nathanaël Blanchet

Supervision réseau
Pôle Infrastrutures Informatiques
227 avenue Professeur-Jean-Louis-Viala
34193 MONTPELLIER CEDEX 5         
Tél. 33 (0)4 67 54 84 55
Fax  33 (0)4 67 54 84 14
<a class="m_7298570659494759391gmail-m_344037001981379143moz-txt-link-abbreviated" href="mailto:blanchet@abes.fr" target="_blank">blanchet@abes.fr</a> </pre>
                  </div>
                </div>
              </div>
            </blockquote>
          </div>
          <br>
        </div>
      </div>
    </blockquote>
    <br>
    <pre class="m_7298570659494759391moz-signature" cols="72">-- 
Nathanaël Blanchet

Supervision réseau
Pôle Infrastrutures Informatiques
227 avenue Professeur-Jean-Louis-Viala
34193 MONTPELLIER CEDEX 5         
Tél. 33 (0)4 67 54 84 55
Fax  33 (0)4 67 54 84 14
<a class="m_7298570659494759391moz-txt-link-abbreviated" href="mailto:blanchet@abes.fr" target="_blank">blanchet@abes.fr</a> </pre>
  </div></div></div>

</blockquote></div><br></div>