-1

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' 
chamara
  • 15
  • 4
  • To start with, you can't have several actions in the same task. If it was a different action than `blockinfile` you would have a `conficting action error`. And next, this is just how yaml works. If you define the same keys (i.e. `blockinfile` and `when`) in the same dictionnary (i.e. your task), only the last definition is retained. You need to separate those blockinfile actions with their conditions in separate tasks or better, apply them in a loop. – Zeitounator Feb 28 '23 at 11:09
  • @Zeitounator **when i gave separate name for each blockinfile generate issue , hence i have pass like this .** ` ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each: JSON: No JSON object could be decoded Syntax Error while loading YAML. mapping values are not allowed in this context The error appears to be in /tasks/main.yml': line 3, column 8, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: - name: set motd vars: ^ here ` ' – chamara Feb 28 '23 at 11:20

1 Answers1

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'
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
  • **d('NoEnv')** i assume this mean if no any network match – chamara Feb 28 '23 at 11:30
  • `d` is an alias to `default`: https://jinja.palletsprojects.com/en/3.0.x/templates/#jinja-filters.default. So yes, if you don't have a matchin network in the `env_by_network` dict, `current_env` will contain the string `"NoEnv"` and the task will be skipped according to the condition. – Zeitounator Feb 28 '23 at 11:36