working with file

47 views
Skip to first unread message

nicolas stevenin

unread,
Jul 21, 2020, 10:08:03 AM7/21/20
to Ansible Project
Hi,

I have a file like : [1,2,3,4,5] 
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). 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}}"


Thanks for your help

Dick Visser

unread,
Jul 21, 2020, 12:57:22 PM7/21/20
to ansible...@googlegroups.com
On Tue, 21 Jul 2020 at 16:08, nicolas stevenin <steveni...@hotmail.fr> 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:





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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/c4322a92-5792-48e6-9973-59cdfd03b9e7o%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

steveni...@hotmail.fr

unread,
Jul 22, 2020, 4:18:22 AM7/22/20
to Ansible Project
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



Stefan Hornburg (Racke)

unread,
Jul 22, 2020, 5:01:06 AM7/22/20
to ansible...@googlegroups.com
On 7/22/20 10:18 AM, steveni...@hotmail.fr 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
> <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
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> 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.

steveni...@hotmail.fr

unread,
Jul 22, 2020, 8:56:27 AM7/22/20
to Ansible Project
OK ! It works, thanks :)
Reply all
Reply to author
Forward
0 new messages