如何在 Ansible 中添加多行

sol*_*ris 3 ansible

我试图在比赛后使用 lineinfile 将多行添加到配置文件中,但我发现生成的行是相反的。这是我的剧本:

  - name: Line test
    lineinfile:
      path: /home/vagrant/abcd
      insertafter: '### AFTER THIS LINE'
      line: "{{ item }}"
      state: present
    with_items:
      - '# This is line 1'
      - '# This is line 2'
      - '# This is line 3' 
Run Code Online (Sandbox Code Playgroud)

这是结果:

### AFTER THIS LINE
# This is line 3
# This is line 2
# This is line 1
Run Code Online (Sandbox Code Playgroud)

我想要的结果应该是:

### AFTER THIS LINE
# This is line 1
# This is line 2
# This is line 3
Run Code Online (Sandbox Code Playgroud)

我知道反转是由于循环造成的,但是如何在不反转输入顺序的情况下克服这一问题呢?我知道有一个 blockinfile 按原样放置文本块,但添加了我不想要的“ANSIBLE MANAGED BLOCK”标记。

谢谢。

sol*_*ris 6

经过更多的尝试后,我发现我可以这样做:

 - name: Line test2
   blockinfile:
     path: /home/vagrant/abcd
     marker: "------"
     insertafter: '### AFTER THIS LINE PART 2'
     state: present
     block: |
       # This is line 1
       # This is line 2
       # This is line 3
Run Code Online (Sandbox Code Playgroud)

产生这个:

 ### AFTER THIS LINE PART 2
 ------
 # This is line 1
 # This is line 2
 # This is line 3
 ------
Run Code Online (Sandbox Code Playgroud)

我认为这对于我们的要求是可以接受的。

谢谢。