I have found that using multiple blockinfile tasks in the single file with when condition not working , seems to the last blockinfile task will have priority over other block , even fist block met the correct condition output result does not generating
when i run in the sever , which have network address 10.45.122.0 , i did work when i have single block , but when i testing it with multiple block having issue . any recommendation
Also fond syntax warning when compiling
[WARNING]: While constructing a mapping from /tasks/main.yml, line 2, column 3, found a
duplicate
dict key (blockinfile). Using last defined value only.
[WARNING]: While constructing a mapping from /tasks/main.yml, line 2, column 3, found a
duplicate
dict key (when). Using last defined value only.
- name: get user output
vars:
username: testuser
envname1: user1
envname2: user2
blockinfile:
marker: "#--------------{mark}--------------------envname1"
path: /tmp/fileout
block: |
" ###########################################################"
You have been logging into {{ envname1 }} environment.
System Name - {{ username }}
IP address - {{ ansible_facts['default_ipv4']['address']}}
"############################################################"
when: ansible_facts['default_ipv4']['network'] == '10.45.122.0'
blockinfile:
marker: "#------------- {mark}----------------------- envname2"
path: /tmp/fileout
block: |
" ###########################################################"
You have been logging into {{ envname2 }} environment.
System Name - {{ username }}
IP address - {{ ansible_facts['default_ipv4']['address']}}
"############################################################"
when: ansible_facts['default_ipv4']['network'] == '10.46.122.0'
Following my comment, in a nutshell:
Option1:
- name: get user output1
vars:
# Those could be defined at a higher level
# to avoid repeating in each task
username: testuser
envname1: user1
envname2: user2
blockinfile:
marker: "#--------------{mark}--------------------envname1"
path: /tmp/fileout
block: |
" ###########################################################"
You have been logging into {{ envname1 }} environment.
System Name - {{ username }}
IP address - {{ ansible_facts['default_ipv4']['address']}}
"############################################################"
when: ansible_facts['default_ipv4']['network'] == '10.45.122.0'
- name: get user output2
vars:
username: testuser
envname1: user1
envname2: user2
blockinfile:
marker: "#------------- {mark}----------------------- envname2"
path: /tmp/fileout
block: |
" ###########################################################"
You have been logging into {{ envname2 }} environment.
System Name - {{ username }}
IP address - {{ ansible_facts['default_ipv4']['address']}}
"############################################################"
when: ansible_facts['default_ipv4']['network'] == '10.46.122.0'
Option2 (prefered):
- name: get user output
vars:
username: testuser
envname1: user1
envname2: user2
env_by_network:
10.45.122.0: envname1
10.46.122.0: envname2
current_network: "{{ ansible_facts['default_ipv4']['network'] }}"
current_env: "{{ env_by_network[current_network] | d('NoEnv') }}"
blockinfile:
marker: "#--------------{mark}--------------------{{ current_env }}"
path: /tmp/fileout
block: |
" ###########################################################"
You have been logging into {{ lookup('vars', current_env) }} environment.
System Name - {{ username }}
IP address - {{ current_network }}
"############################################################"
when: current_env != 'NoEnv'