Re: [ansible-project] json_query & regular expression
Thanks Abhijeet & Valdo for your time to check. This works ! On Sunday, April 12, 2020 at 9:05:20 AM UTC-7, Abhijeet Kasurde wrote: > > Even this works for me - notice ``vm_facts.virtual_machines`` > > - debug: > msg: "{{ item.ip_address }}" > loop: "{{ vm_facts.virtual_machines | selectattr('guest_name', 'match', > '^vm(.*)$')| list }}" > > > > On Sun, Apr 12, 2020 at 9:31 PM Abhijeet Kasurde > wrote: > >> I would do this in a simpler way - >> >> - name: Gather all VMs information >> vmware_vm_info: >> hostname: '{{ vcenter_hostname }}' >> username: '{{ vcenter_username }}' >> password: '{{ vcenter_password }}' >> validate_certs: no >> register: vm_facts >> delegate_to: localhost >> >> - debug: >> msg: "{{ item.ip_address }}" >> loop: "{{ vm_facts.virtual_machines }}" >> when: >> - item.guest_name | regex_search('^vm') >> >> >> On Sun, Apr 12, 2020 at 5:26 PM Vladimir Botka > > wrote: >> >>> On Sun, 12 Apr 2020 00:26:39 -0700 (PDT) >>> Kiran Kumar > wrote: >>> >>> > { >>> > "attributes": {}, >>> > "cluster": "xx", >>> > "esxi_hostname": "xx", >>> > "guest_fullname": "Microsoftxx", >>> > "guest_name": "xx", >>> > "ip_address": "xx", >>> > "mac_address": [ >>> > "xx" >>> > ], >>> > "power_state": "poweredOn", >>> > "tags": [], >>> > "uuid": "xx", >>> > "vm_network": { >>> > "xx": { >>> > "ipv4": [ >>> > "xx" >>> > ], >>> > "ipv6": [] >>> > } >>> > >>> > > fatal: [localhost]: FAILED! => {"msg": "'str object' has no >>> attribute >>> > > > 'guest_name'"} >>> > > > > >>> > > > > - debug: >>> > > > > msg: "{{ item.ip_address }}" >>> > > > > loop: "{{ vm_facts| >>> > > > > selectattr('guest_name', 'match', '^vm(.*)$')| >>> > > > > list }}" >>> >>> There shouldn't be any problem with the dictionary "vm_facts" you >>> posted. The >>> attribute "guest_name" is in place. I can't explain the error. It's up >>> to you >>> to fix the JSON paths. >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Ansible Project" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to ansible...@googlegroups.com . >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/ansible-project/20200412135631.0fd34f5a%40gmail.com >>> . >>> >> >> >> -- >> Thanks, >> Abhijeet Kasurde >> > > > -- > Thanks, > Abhijeet Kasurde > -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ef6a592e-0928-4747-9539-47f5342991ef%40googlegroups.com.
Re: [ansible-project] json_query & regular expression
Even this works for me - notice ``vm_facts.virtual_machines`` - debug: msg: "{{ item.ip_address }}" loop: "{{ vm_facts.virtual_machines | selectattr('guest_name', 'match', '^vm(.*)$')| list }}" On Sun, Apr 12, 2020 at 9:31 PM Abhijeet Kasurde wrote: > I would do this in a simpler way - > > - name: Gather all VMs information > vmware_vm_info: > hostname: '{{ vcenter_hostname }}' > username: '{{ vcenter_username }}' > password: '{{ vcenter_password }}' > validate_certs: no > register: vm_facts > delegate_to: localhost > > - debug: > msg: "{{ item.ip_address }}" > loop: "{{ vm_facts.virtual_machines }}" > when: > - item.guest_name | regex_search('^vm') > > > On Sun, Apr 12, 2020 at 5:26 PM Vladimir Botka wrote: > >> On Sun, 12 Apr 2020 00:26:39 -0700 (PDT) >> Kiran Kumar wrote: >> >> > { >> > "attributes": {}, >> > "cluster": "xx", >> > "esxi_hostname": "xx", >> > "guest_fullname": "Microsoftxx", >> > "guest_name": "xx", >> > "ip_address": "xx", >> > "mac_address": [ >> > "xx" >> > ], >> > "power_state": "poweredOn", >> > "tags": [], >> > "uuid": "xx", >> > "vm_network": { >> > "xx": { >> > "ipv4": [ >> > "xx" >> > ], >> > "ipv6": [] >> > } >> > >> > > fatal: [localhost]: FAILED! => {"msg": "'str object' has no >> attribute >> > > > 'guest_name'"} >> > > > > >> > > > > - debug: >> > > > > msg: "{{ item.ip_address }}" >> > > > > loop: "{{ vm_facts| >> > > > > selectattr('guest_name', 'match', '^vm(.*)$')| >> > > > > list }}" >> >> There shouldn't be any problem with the dictionary "vm_facts" you posted. >> The >> attribute "guest_name" is in place. I can't explain the error. It's up to >> you >> to fix the JSON paths. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Ansible Project" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to ansible-project+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/ansible-project/20200412135631.0fd34f5a%40gmail.com >> . >> > > > -- > Thanks, > Abhijeet Kasurde > -- Thanks, Abhijeet Kasurde -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAFwWkHquH4mo7GhF2G05yB01Gv%3DZ%3DQCSgM90UHL-z%2BsmCoKd5Q%40mail.gmail.com.
Re: [ansible-project] json_query & regular expression
I would do this in a simpler way - - name: Gather all VMs information vmware_vm_info: hostname: '{{ vcenter_hostname }}' username: '{{ vcenter_username }}' password: '{{ vcenter_password }}' validate_certs: no register: vm_facts delegate_to: localhost - debug: msg: "{{ item.ip_address }}" loop: "{{ vm_facts.virtual_machines }}" when: - item.guest_name | regex_search('^vm') On Sun, Apr 12, 2020 at 5:26 PM Vladimir Botka wrote: > On Sun, 12 Apr 2020 00:26:39 -0700 (PDT) > Kiran Kumar wrote: > > > { > > "attributes": {}, > > "cluster": "xx", > > "esxi_hostname": "xx", > > "guest_fullname": "Microsoftxx", > > "guest_name": "xx", > > "ip_address": "xx", > > "mac_address": [ > > "xx" > > ], > > "power_state": "poweredOn", > > "tags": [], > > "uuid": "xx", > > "vm_network": { > > "xx": { > > "ipv4": [ > > "xx" > > ], > > "ipv6": [] > > } > > > > > fatal: [localhost]: FAILED! => {"msg": "'str object' has no attribute > > > > > 'guest_name'"} > > > > > > > > > > - debug: > > > > > msg: "{{ item.ip_address }}" > > > > > loop: "{{ vm_facts| > > > > > selectattr('guest_name', 'match', '^vm(.*)$')| > > > > > list }}" > > There shouldn't be any problem with the dictionary "vm_facts" you posted. > The > attribute "guest_name" is in place. I can't explain the error. It's up to > you > to fix the JSON paths. > > -- > You received this message because you are subscribed to the Google Groups > "Ansible Project" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to ansible-project+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/ansible-project/20200412135631.0fd34f5a%40gmail.com > . > -- Thanks, Abhijeet Kasurde -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAFwWkHpcMQjZMaioZ6v%3Dc-8t%3D%3DwsmvuiUZRmriAt-HEiVYFhGw%40mail.gmail.com.
Re: [ansible-project] json_query & regular expression
On Sun, 12 Apr 2020 00:26:39 -0700 (PDT) Kiran Kumar wrote: > { > "attributes": {}, > "cluster": "xx", > "esxi_hostname": "xx", > "guest_fullname": "Microsoftxx", > "guest_name": "xx", > "ip_address": "xx", > "mac_address": [ > "xx" > ], > "power_state": "poweredOn", > "tags": [], > "uuid": "xx", > "vm_network": { > "xx": { > "ipv4": [ > "xx" > ], > "ipv6": [] > } > > > fatal: [localhost]: FAILED! => {"msg": "'str object' has no attribute > > > 'guest_name'"} > > > > > > > > - debug: > > > > msg: "{{ item.ip_address }}" > > > > loop: "{{ vm_facts| > > > > selectattr('guest_name', 'match', '^vm(.*)$')| > > > > list }}" There shouldn't be any problem with the dictionary "vm_facts" you posted. The attribute "guest_name" is in place. I can't explain the error. It's up to you to fix the JSON paths. -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20200412135631.0fd34f5a%40gmail.com. pgprDoCcxWYcW.pgp Description: OpenPGP digital signature
Re: [ansible-project] json_query & regular expression
Thanks for your reply Valdo. This is a JSON output { "attributes": {}, "cluster": "xx", "esxi_hostname": "xx", "guest_fullname": "Microsoftxx", "guest_name": "xx", "ip_address": "xx", "mac_address": [ "xx" ], "power_state": "poweredOn", "tags": [], "uuid": "xx", "vm_network": { "xx": { "ipv4": [ "xx" ], "ipv6": [] } I am reading about - selectattr - as i am new to this. Thanks On Sunday, April 12, 2020 at 12:01:30 AM UTC-7, Vladimir Botka wrote: > > On Sat, 11 Apr 2020 23:35:16 -0700 (PDT) > Kiran Kumar > wrote: > > > I get error > > TASK [debug] > ***> > > > fatal: [localhost]: FAILED! => {"msg": "'str object' has no attribute > > 'guest_name'"} > > Take a look at the structure of "vm_fact" and fix it. If you still have > issues > post: > > - debug: > var: vm_fact > > > On Saturday, April 11, 2020 at 10:02:06 PM UTC-7, Vladimir Botka wrote: > > > > > - debug: > > > > > msg: "{{ item.ip_address }}" > > > > > with_items: > > > > > - "{{ vm_facts.virtual_machines | json_query(query) }}" > > > > > vars: > > > > > query: "[?guest_name == 'vm1' ]" > > > > > > Filter "selectattr" should do the job. For example > > > > > > - debug: > > > msg: "{{ item.ip_address }}" > > > loop: "{{ vm_facts| > > > selectattr('guest_name', 'match', '^vm1$')| > > > list }}" > > > - debug: > > > msg: "{{ item.ip_address }}" > > > loop: "{{ vm_facts| > > > selectattr('guest_name', 'match', '^vm(.*)$')| > > > list }}" > -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/999e1b69-c255-473f-8cdc-d9ae33748195%40googlegroups.com.
Re: [ansible-project] json_query & regular expression
On Sat, 11 Apr 2020 23:35:16 -0700 (PDT) Kiran Kumar wrote: > I get error > TASK [debug] ***> fatal: [localhost]: FAILED! => {"msg": "'str object' has no attribute > 'guest_name'"} Take a look at the structure of "vm_fact" and fix it. If you still have issues post: - debug: var: vm_fact > On Saturday, April 11, 2020 at 10:02:06 PM UTC-7, Vladimir Botka wrote: > > > > - debug: > > > > msg: "{{ item.ip_address }}" > > > > with_items: > > > > - "{{ vm_facts.virtual_machines | json_query(query) }}" > > > > vars: > > > > query: "[?guest_name == 'vm1' ]" > > > > Filter "selectattr" should do the job. For example > > > > - debug: > > msg: "{{ item.ip_address }}" > > loop: "{{ vm_facts| > > selectattr('guest_name', 'match', '^vm1$')| > > list }}" > > - debug: > > msg: "{{ item.ip_address }}" > > loop: "{{ vm_facts| > > selectattr('guest_name', 'match', '^vm(.*)$')| > > list }}" -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20200412090107.72140fe8%40gmail.com. pgpA8ymxxqQSC.pgp Description: OpenPGP digital signature
Re: [ansible-project] json_query & regular expression
Thanks for the Reply Valdo I get error TASK [debug] * fatal: [localhost]: FAILED! => {"msg": "'str object' has no attribute 'guest_name'"} On Saturday, April 11, 2020 at 10:02:06 PM UTC-7, Vladimir Botka wrote: > > On Sat, 11 Apr 2020 21:26:28 -0700 (PDT) > Kiran Kumar > wrote: > > > In jason query how can i add regular expression ? eg if have vm1, > vm2,vm3 > > below query can be adjusted ? eg query: "[?guest_name == 'vm*' ]" > , > > seems does not work in below case > > > > > tasks: > > > - name: Gather all VMs information > > > vmware_vm_info: > > > hostname: '{{ vcenter_hostname }}' > > > username: '{{ vcenter_username }}' > > > password: '{{ vcenter_password }}' > > > validate_certs: no > > > register: vm_facts > > > delegate_to: localhost > > > - debug: > > > msg: "{{ item.ip_address }}" > > > with_items: > > > - "{{ vm_facts.virtual_machines | json_query(query) }}" > > > vars: > > > query: "[?guest_name == 'vm1' ]" > > Filter "selectattr" should do the job. For example > > - debug: > msg: "{{ item.ip_address }}" > loop: "{{ vm_facts| > selectattr('guest_name', 'match', '^vm1$')| > list }}" > - debug: > msg: "{{ item.ip_address }}" > loop: "{{ vm_facts| > selectattr('guest_name', 'match', '^vm(.*)$')| > list }}" > > I'm not aware of regex in JMESPath. > https://jmespath.readthedocs.io/en/latest/ > > HTH, > > -vlado > -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f8d929f8-116c-4574-a782-29fbe6639240%40googlegroups.com.
Re: [ansible-project] json_query & regular expression
On Sat, 11 Apr 2020 21:26:28 -0700 (PDT) Kiran Kumar wrote: > In jason query how can i add regular expression ? eg if have vm1, vm2,vm3 > below query can be adjusted ? eg query: "[?guest_name == 'vm*' ]" , > seems does not work in below case > > > tasks: > > - name: Gather all VMs information > > vmware_vm_info: > > hostname: '{{ vcenter_hostname }}' > > username: '{{ vcenter_username }}' > > password: '{{ vcenter_password }}' > > validate_certs: no > > register: vm_facts > > delegate_to: localhost > > - debug: > > msg: "{{ item.ip_address }}" > > with_items: > > - "{{ vm_facts.virtual_machines | json_query(query) }}" > > vars: > > query: "[?guest_name == 'vm1' ]" Filter "selectattr" should do the job. For example - debug: msg: "{{ item.ip_address }}" loop: "{{ vm_facts| selectattr('guest_name', 'match', '^vm1$')| list }}" - debug: msg: "{{ item.ip_address }}" loop: "{{ vm_facts| selectattr('guest_name', 'match', '^vm(.*)$')| list }}" I'm not aware of regex in JMESPath. https://jmespath.readthedocs.io/en/latest/ HTH, -vlado -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/20200412070147.05c65041%40gmail.com. pgpGeHxUxSg7J.pgp Description: OpenPGP digital signature