Asier Gomez
Asier Gomez

Reputation: 6578

Ansible blockinfile replace if there is another block instead of inserting the new

I try to insert 2 ansible blocks in the same file, but Ansible replace the first block with the second.

If I insert the next 2 blocks:

- name: Setup java environment
  blockinfile:
    dest: /home/{{ user }}/.bashrc
    block: |
      #Java path# 
      JAVA_HOME={{ java_home }}/

- name: Setup hadoop environment
  blockinfile:
    dest: /home/{{ user }}/.bashrc
    block: |
      #Hadooppath# 
      HADOOP_HOME={{ hadoop_home }}/

Only the second block will be in the file, becouse it replace the first.

Upvotes: 7

Views: 7021

Answers (1)

Asier Gomez
Asier Gomez

Reputation: 6578

To insert 2 blocks with Ansible in the same file and don't replace the first with the second:

Change the Ansible blockinfile marker:

blockinfile_task_1:
marker: "# {mark} ANSIBLE MANAGED BLOCK insertion 1"
blockinfile_task_2:
marker: "# {mark} ANSIBLE MANAGED BLOCK insertion 2"

For the previous example, the playbook will be:

- name: Setup java environment
  blockinfile:
    dest: /home/{{ user }}/.bashrc
    marker: "# {mark} ANSIBLE MANAGED BLOCK Java"
    block: |
      #Java path# 
      JAVA_HOME={{ java_home }}/

- name: Setup hadoop environment
  blockinfile:
    dest: /home/{{ user }}/.bashrc
    marker: "# {mark} ANSIBLE MANAGED BLOCK Hadoop"
    block: |
      #Hadooppath# 
      HADOOP_HOME={{ hadoop_home }}/

Upvotes: 13

Related Questions