On 12.10.2017 14:00, Abey Thomas wrote:
Hi,

I am running ansible on centos 7.4.1708 . Not sure what I am doing wrong but the dictionary variables are not completely written by the copy module.

Here is my complete YAML file :

---
- hosts: localhost
  gather_facts: False
  vars:
    var_dict1:
      127.0.0.1: localhost
      10.10.10.10: hostnameinternal1
      10.20.10.10: hostnameinternal2
  tasks:

  - name: Copy the contents of var_dict1 to output1
    copy:
      content: |
        {% for key in var_dict1 %}
        {{ item.key}}    {{item.value}}
        {% endfor %}
      dest: "/usershome/output1"
    run_once: yes
    with_dict: "{{ var_dict1 }}"


and here is the file /usershome/output1
127.0.0.1    localhost
127.0.0.1    localhost
127.0.0.1    localhost


Because of the with_dict the task will run 3 times, one for each item in var_dict1.
Only the last will stick since the file will be overwritten.

Do this instead
  - name: Copy the contents of var_dict1 to output1
    copy:
      content: |
        {% for key, value in var_dict1.iteritems() %}
        {{ key }}    {{ value }}
        {% endfor %}
      dest: "/usershome/output1"
    run_once: yes

--
Kai Stian Olstad

--
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/1a6785f1d7149e4338728ef0e5d8c7ae%40olstad.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to