Ansible:如何使用Ansible playbook在conf文件中间插入文本字符串?

时间:2017-06-28 23:26:21

标签: ansible

我需要添加说明“允许所有人”的文字。正下方'订单允许,拒绝' 所以这个:

# Restrict access to the server...
<Location />
  Order allow,deny
</Location>

应该是这样的:

# Restrict access to the server...
<Location />
  Order allow,deny
  Allow all
</Location>

到目前为止,我的Ansible playbook块看起来像这样:

  - name: Enable access to the server                                                                                                                                                                     
    lineinfile:
      destfile: /etc/cups/cupsd.conf

2 个答案:

答案 0 :(得分:0)

我认为您正在寻找insertafter模块的lineinfile参数(duplicate

类似的东西:

- lineinfile:
    path: /etc/cups/cupsd.conf
    insertafter: '\sOrder allow,deny'
    line: '\nAllow all'

答案 1 :(得分:0)

没有办法无条件地#34; 使用Ansible playbook &#34;在conf文件的中间插入一个文本字符串。

Ansible lineinfile模块可让您确保文件中存在特定行,如果不存在,则会在(insertbefore)之前或之后(insertafter插入)另一行,但即使它在特定情况下有效,它也是一个特例(条件是:该行在文件的其他地方不存在)。

对于平面配置文件,这可能是可以接受的,但如果文件是结构化的(包含可能包含相同行的部分),则会很麻烦。

底线是:您应该避免使代码依赖于未指定的初始状态,因此请改用template module

在极少数情况下,如果不可能,请使用blockinfile module定义整个部分,例如:

- blockinfile:
    path: /etc/cups/cupsd.conf
    marker: # Ansible-managed block { mark }
    block: |
      <Location />
        Order allow,deny
        Allow all
     </Location>