Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ansible

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.

like image 475
Asier Gomez Avatar asked Jan 12 '18 10:01

Asier Gomez


1 Answers

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 }}/
like image 52
Asier Gomez Avatar answered Nov 15 '22 11:11

Asier Gomez