On 7/22/20 10:18 AM, [email protected] wrote:
> Hi,
> 
> I have a json file-> cluster_name.json :
> ["cluster1","cluster2","cluster3","cluster4"]
> 
> And I want to iterate on every cluster name to obtain some informations so I 
> use the module vmware_host_service_info.
> My playbook is -> vmware.yml :
> ---
> - name: test
>   hosts: localhost
>   vars_files:
>     - cluster_name.json
>   tasks:
>     - name: Gather info about all ESXi Host in given Cluster
>       vmware_host_service_info:
>         validate_certs: no
>         hostname: hostname
>         username: user
>         password: password
>         cluster_name: "{{cluster}}"      <-- HERE I WANT TO ITERATE ON EVERY 
> CLUSTER_NAME IN cluster_name.json
>       delegate_to: localhost
> 

Replace vars_files with a lookup:

 vars:
    cluster_names: "{{ lookup( 'file', 'cluster_name.json' ) }}"

This automatically converts the file contents from JSON to a list.

You can now loop over that:

 tasks:
   - name: Gather info about all ESXi Host in given Cluster
     vmware_host_service_info:
       validate_certs: no
       hostname: hostname
       username: user
       password: password
       cluster_name: "{{ item }}"
     with_items: "{{ cluster_names }}"
     delegate_to: localhost

Regards
        Racke

> 
> 
> Le mardi 21 juillet 2020 à 18:57:22 UTC+2, [email protected] a écrit :
> 
>     On Tue, 21 Jul 2020 at 16:08, nicolas stevenin <[email protected]> 
> wrote:
> 
>         Hi,
> 
>         I have a file like : [1,2,3,4,5] 
> 
> 
> 
>     'Like' is ambiguous. What is like what?
>     Is the filename like 12345? Is the content a json list of integers? 
> Please try to be precise.
> 
> 
>         and I want to use loop for each number in this file on the playbook 
> (I want to use the file, not write keys in
>         the playbook).
> 
> 
> 
>     No idea again what 'writing keys in the playbook' means. So again please 
> be precise. 
> 
> 
>         Could you help me please ?
> 
>         The playbook:  (and i need to loop on "variable_file")
>             - name: Gather info about all ESXi Host in given Cluster
>               vmware_host_service_info:
>                 validate_certs: no
>                 hostname: xxxxx
>                 username: user
>                 password: password
>                 cluster_name: "{{variable_file}}"
> 
> 
> 
>     A wild guess, if you want to read the contents of a file ? Maybe this is 
> what you want:
> 
>     https://docs.ansible.com/ansible/latest/plugins/lookup/lines.html
> 
> 
> 
> 
> 
>         Thanks for your help
> 
>         -- 
>         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
>         [email protected].
>         To view this discussion on the web visit
>         
> https://groups.google.com/d/msgid/ansible-project/c4322a92-5792-48e6-9973-59cdfd03b9e7o%40googlegroups.com
>         
> <https://groups.google.com/d/msgid/ansible-project/c4322a92-5792-48e6-9973-59cdfd03b9e7o%40googlegroups.com?utm_medium=email&utm_source=footer>.
> 
>     -- 
>     Sent from a mobile device - please excuse the brevity, spelling and 
> punctuation.
> 
> -- 
> 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
> [email protected] 
> <mailto:[email protected]>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/1ee51582-de55-4e92-bc1e-928f926f9874n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/1ee51582-de55-4e92-bc1e-928f926f9874n%40googlegroups.com?utm_medium=email&utm_source=footer>.


-- 
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/26aef76c-9614-d391-85d7-e252509117e6%40linuxia.de.

Reply via email to