Ansible 安:我如何在一个CONF文件的中间插入一个文本字符串?

Ansible 安:我如何在一个CONF文件的中间插入一个文本字符串?,ansible,Ansible,我需要在“订单允许,拒绝”下方添加说明“允许所有”的文本 因此: 我想您正在查找lineinfle模块()的insertafter参数 大概是这样的: - lineinfile: path: /etc/cups/cupsd.conf insertafter: '\sOrder allow,deny' line: '\nAllow all' P>没有办法无条件地在一个CONF文件的中间插入一个文本字符串,使用一个可读的剧本。 Ansiblelineinfle模块允许您确

我需要在“订单允许,拒绝”下方添加说明“允许所有”的文本 因此:


我想您正在查找
lineinfle
模块()的
insertafter
参数

大概是这样的:

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

<> P>没有办法无条件地在一个CONF文件的中间插入一个文本字符串,使用一个可读的剧本。 Ansible
lineinfle
模块允许您确保文件中存在特定行,如果不存在,它将在另一行之前(
insertbefore
)或之后(
insertafter
)插入该行,但即使它在特定情况下工作,它也是特定情况(条件是:该行在文件中的其他位置不存在)

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


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

在极少数不可能的情况下,定义整个部分,例如:

- blockinfile:
    path: /etc/cups/cupsd.conf
    marker: # Ansible-managed block { mark }
    block: |
      <Location />
        Order allow,deny
        Allow all
     </Location>
-blockinfle:
路径:/etc/cups/cupsd.conf
marker:#Ansible托管块{mark}
区块:|
命令允许,拒绝
允许所有

我尝试了这个方法,它在文件的底部插入了块。唯一的区别是,因为我使用的是Ansible 2.2,所以我必须使用destfile而不是pathOf。当然,默认情况下,它会在底部插入块。这就是为什么我说你应该使用
模板
。建议使用
blockinfle
是为了某些特定的目的ic案例,但您需要知道,您正在做什么,并逐步构建文件。
  - name: Enable access to the server                                                                                                                                                                     
    lineinfile:
      destfile: /etc/cups/cupsd.conf
- lineinfile:
    path: /etc/cups/cupsd.conf
    insertafter: '\sOrder allow,deny'
    line: '\nAllow all'
- blockinfile:
    path: /etc/cups/cupsd.conf
    marker: # Ansible-managed block { mark }
    block: |
      <Location />
        Order allow,deny
        Allow all
     </Location>