[ovirt-users] ansible and list of vms

Ondra Machacek omachace at redhat.com
Fri May 12 19:16:17 UTC 2017


This worked for me:

---
- hosts: localhost
  connection: local
  vars_files:
    - ../vars.yml

  tasks:
  - name: Obtain SSO token
    ovirt_auth:
      url: "{{ url }}"
      username: "{{ username }}"
      password: "{{ password }}"
      insecure: "{{ insecure }}"

  - name: List vms
    ovirt_vms_facts:
      fetch_nested: true
      nested_attributes:
        - description
      auth: "{{ ovirt_auth }}"

  - name: set vms
    set_fact:
       vm: "{{ item.name }}: {{ item.snapshots |
map(attribute='description') | join(',') }}"
    with_items: "{{ ovirt_vms }}"
    loop_control:
      label: "{{ item.name }}"
    register: all_vms

  - name: make a list
    set_fact: vms="{{ all_vms.results | map(attribute='ansible_facts.vm') |
list }}"

  - name: Print vms
    debug:
      var: vms

Can you please open an issue on Ansible github[1], and describe what
exactly issue do you have?
Thanks!

[1] https://github.com/ansible/ansible/issues


On Fri, May 12, 2017 at 7:11 PM, Nathanaël Blanchet <blanchet at abes.fr>
wrote:

> Great, it works like a charm!
>
> But how could I obtain a several map display?
>
> For example, I need to display the vm name ; its relative snaphost id and
> snaphot description.
>
> I tried to change filter, but notyhing works
>
> msg: "{{ ovirt_snapshots | map(attribute='description') |
> map(attribute='id') | join(' ') }}"
>
> or something like
>
> msg: "{{ ovirt_snapshots.vm | map(attribute='name') | map(attribute='id')
> | join(' ') }}"
>
> Thanks for helping.
>
> ps: I tried nearly all modules, and the permissions one is bugged, only
> internal-authz domain displays users, none of ldap one can display.
>
> Le 11/05/2017 à 20:14, Ondra Machacek a écrit :
>
> I am not Ansible expert, this maybe worth asking on Ansible users list,
> but one thing which you can do is following:
>
> in tasks/main.yml
>
> - include: "{{ item }}"
>   with_items: "{{ resources | default('vms') }}.yml"
>
> then have following files:
>  - tasks/vms.yml
>  - tasks/clusters.yml
>  - tasks/disks.yml
>  - ...
>
> then run it as:
>
>  $ ansible-playbook -e "resources: [vms,disks]" play.yml
>  $ ansible-playbook -e "resources: [clusters] clusters_pattern=name=x*"
> play.yml
>
>
> On Thu, May 11, 2017 at 6:34 PM, Nathanaël Blanchet <blanchet at abes.fr>
> wrote:
>
>> Thanks, all works as expected.
>>
>> Now, I try make roles.
>>
>> here it is :
>>
>> *cat /etc/ansible/roles/ovirt_vm_facts/tasks/main.yml*
>>
>>   - name: get vm facts
>>     ovirt_vms_facts:
>>       auth:
>>         url: https://{{ engine }}.v100.abes.fr/ovirt-engine/api
>>         username: "{{ username }}"
>>         password: "{{ password }}"
>>         insecure: "{{ insecure }}"
>>       pattern: "{{ pattern }}"
>>   - debug:
>>       msg: "{{ ovirt_vms | map(attribute='name') | join(' ') }}"
>>
>> *cat /etc/ansible/roles/ovirt_vm_facts/vars/main.yml*
>>
>> pattern: "os=rhel_5x64 or os=rhel_5"
>> engine: air
>> username: admin at internal
>> password: ovirt123
>> insecure: true
>>
>> and I call those files with */tmp/ovirt_vm_facts.yml* with extra
>> variables if needed
>>
>> - hosts: localhost
>>   connection: local
>>   roles:
>>     - ovirt_vm_fact
>>
>> running ansible-playbook /tmp/ovirt_vm_facts.ym -e 'pattern="name=acier*
>> and cluster=Dev"'
>>
>> *But..*
>>
>> I don't want to do the same for each ansible module shipped with 2.3.
>>
>> 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:
>>
>> - hosts: localhost
>>   connection: local
>>   roles:
>>     - ovirt_facts (with somewhere a variable "myfact")
>>
>> which would play an unique role */etc/ansible/roles/ovirt_facts/tasks/main.yml
>> with*
>>
>>
>> - name: get vm facts
>>     myfact
>>       auth:
>>         ....
>>
>> Hope to be clear enough, thank your for your help.
>>
>> Le 10/05/2017 à 13:14, Ondra Machacek a écrit :
>>
>>
>>
>> On Wed, May 10, 2017 at 12:38 PM, Nathanaël Blanchet <blanchet at abes.fr>
>> wrote:
>>
>>> Le 10/05/2017 à 10:29, Ondra Machacek a écrit :
>>>
>>> Not sure I understand. You can use Ansible module to list VMs, you
>>> can use ovirt_vms _facts[1] module.
>>>
>>> For example to get only stopped VMs, you can use following:
>>>
>>> - name: Get stopped VMs
>>>   ovirt_vms_facts:
>>>     auth:
>>>       url: https://ovirt.example.com/ovirt-engine/api
>>>       username: admin at internal
>>>       password: password
>>>       ca_file: /etc/pki/ovirt-engine/ca.pem
>>>     pattern: "status = down"
>>>
>>> I want a list of vms sorted by any possible attributes, i.e. sorted by
>>> datacenter or cluster, not only by status.
>>>
>>>    - For example, ovirt_vms_facts doesn't allow to get the datacenter
>>>    the vm belongs to , and the pattern filter doesn't return a list of vms as
>>>    expected:
>>>
>>>
>>> - hosts: localhost
>>>   connection: local
>>>   tasks:
>>>   - name: Get stopped VMs
>>>     ovirt_vms_facts:
>>>       auth:
>>>         url: https://acore.v100.abes.fr/ovirt-engine/api
>>>         username: admin at internal
>>>         password: ovirt123
>>>         insecure: true
>>>         pattern: name=centos* and cluster=west
>>>   - debug:
>>>       var: ovirt_vms
>>>   - shell: echo "{{ ovirt_vms.name }}"
>>>
>>
>>
>> Ok, I understand, now.
>> You have few options here.
>> If you want for example only VMs from datacenter 'X', you can use
>> following:
>>
>> - hosts: localhost
>>   connection: local
>>   tasks:
>>   - name: Get VMs from datacenter X
>>     ovirt_vms_facts:
>>       pattern: datacenter=X
>>       auth:
>>         url: https://acore.v100.abes.fr/ovirt-engine/api
>>         username: admin at internal
>>         password: ovirt123
>>         insecure: true
>>   - debug:
>>       msg: "{{ ovirt_vms | map(attribute='name') | list }}"
>>
>>
>>>
>>> How can I successfully display this list of vms like described here :
>>> name description returned type sample
>>> ovirt_vms List of dictionaries describing the VMs. VM attribues are
>>> mapped to dictionary keys, all VMs attributes can be found at following
>>> url: https://ovirt.example.com/ovirt-engine/api/model#types/vm. On
>>> success. list
>>>
>>>
>>>    - About the python SDK:
>>>
>>> list_vms.py could be what I expect, but vm.datacenter_name doesn't exist
>>>
>>
>> Yes, because the 'data_center' parameter isn't part of the VM type. Take
>> a look here[1],
>> you will find there all attributes the VM has. As you can see there is no
>> 'datacenter', but
>> there is a 'cluster', but as said in documentation, it's just a reference
>> to the cluster. That
>> means that it contains the ID of the cluster and the 'href' to the
>> cluster, so you can get
>> the cluster object by following that reference using this code:
>>
>>  vm_cluster = connection.follow_link(vm.cluster)
>>  print vm_cluster.name
>>
>>  # Same for datacenter:
>>  vm_dc = connection.follow_link(vm_cluster.data_center)
>>  print vm_dc.name
>>
>> What you need to understand is, what is the difference between the 'full'
>> object and the reference to the object. It's good to take a look at the
>> XML
>> output of the API and learn to read the API specification documentation,
>> when working with oVirt Ansible and Python SDK.
>>
>> [1] http://ovirt.github.io/ovirt-engine-api-model/master/#types/vm
>>
>>
>>>
>>>
>>> [1] http://docs.ansible.com/ansible/ovirt_vms_facts_module.html
>>>
>>> On Wed, May 10, 2017 at 10:13 AM, Nathanaël Blanchet <blanchet at abes.fr>
>>> wrote:
>>>
>>>> Thanks, I knew this python script, but I believed it was possible to do
>>>> the same with an ansible module.
>>>>
>>>> That's mean we need several tools to do different tasks, it is not a
>>>> very convergent way to proceed... but it is feasable.
>>>>
>>>> Le 10/05/2017 à 07:56, Ondra Machacek a écrit :
>>>>
>>>>
>>>>
>>>> On Tue, May 9, 2017 at 6:52 PM, Nathanaël Blanchet <blanchet at abes.fr>
>>>> wrote:
>>>>
>>>>> Hello,
>>>>>
>>>>> I didn't find anyway to easy list all my vms thanks to the ansible
>>>>> modules...
>>>>> 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:
>>>>>
>>>>>   File "./ovirt4.py", line 262, in <module>
>>>>>     main()
>>>>>   File "./ovirt4.py", line 254, in main
>>>>>     vm_name=args.host,
>>>>>   File "./ovirt4.py", line 213, in get_data
>>>>>     vms[name] = get_dict_of_struct(connection, vm)
>>>>>   File "./ovirt4.py", line 185, in get_dict_of_struct
>>>>>     (device.name, [ip.address for ip in device.ips]) for device in
>>>>> devices
>>>>>   File "./ovirt4.py", line 185, in <genexpr>
>>>>>     (device.name, [ip.address for ip in device.ips]) for device in
>>>>> devices
>>>>> TypeError: 'NoneType' object is not iterable
>>>>>
>>>>
>>>> This error was fixed already, try to download latest ovirt4.py file.
>>>>
>>>> The other way is to use fatcs[1] module.
>>>>
>>>> [1] http://docs.ansible.com/ansible/ovirt_vms_facts_module.html
>>>>
>>>>
>>>>>
>>>>>
>>>>> What is the simpliest way to get this basic information with sdk4???
>>>>> (with sdk3 : ovirt-shell -E "list vms")
>>>>>
>>>>
>>>> Please take a look at the following example:
>>>>
>>>>  https://github.com/oVirt/ovirt-engine-sdk/blob/master/sdk/e
>>>> xamples/list_vms.py
>>>>
>>>>
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> 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
>>>>> blanchet at abes.fr
>>>>>
>>>>> _______________________________________________
>>>>> Users mailing list
>>>>> Users at ovirt.org
>>>>> http://lists.ovirt.org/mailman/listinfo/users
>>>>>
>>>>
>>>>
>>>> --
>>>> 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 14blanchet at abes.fr
>>>>
>>>>
>>>
>>> --
>>> 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 14blanchet at abes.fr
>>>
>>>
>>
>> --
>> 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 14blanchet at abes.fr
>>
>>
>
> --
> 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 14blanchet at abes.fr
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ovirt.org/pipermail/users/attachments/20170512/7a38aa05/attachment-0001.html>


More information about the Users mailing list