I'm trying to use blockinfile module of ansible to write block text into file.
test purpose to /tmp/hosts, here is the content of blockinfile-test.yaml
---
- hosts: web
gather_facts: false
tasks:
- name: Add mappings to /tmp/hosts
blockinfile:
path: /tmp/hosts
create: yes
block: |
{{ item.ip }} {{ item.name }}
loop:
- { name: host1, ip: 10.180.104.100 }
- { name: host2, ip: 10.180.104.101 }
This is inventory file
[myself]
ansible-main.local
[web]
10.180.104.101
Then ran command to apply
ansible-playbook -i hosts blockinfile-test.yaml
TASK [Add mappings to /tmp/hosts] *****************************
changed: [10.180.104.101] => (item={'name': 'host1', 'ip': '10.180.104.100'})
changed: [10.180.104.101] => (item={'name': 'host2', 'ip': '10.180.104.101'})
PLAY RECAP ********************
10.180.104.101 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Then, I checked that effort but got only one line was appended in file /tmp/hosts
# BEGIN ANSIBLE MANAGED BLOCK
10.180.104.101 host2
# END ANSIBLE MANAGED BLOCK
Why is only one line written to the file?
Is there missing something to blockinfile?