Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ansible multiple blockinfile tasks in a single file with when

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[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' 

>Solution :

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'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading