1

我正在尝试使用lineinfile通过 ansible playbook 在 yaml 配置文件中添加新行。我到目前为止所尝试的。

#attempt-1
- name: "Configure Node River"
 lineinfile: "dest=/path/to/config.conf line='node.river: river_name'"
#attempt-2
- name: "Configure Node River"
 lineinfile: "dest=/path/to/config.conf state=present regexp='^' line='node.river: river_name'"

我也按照参考文件进行了尝试。

# Add a line to a file if it does not exist, without passing regexp
- lineinfile: dest=/tmp/testfile line="192.168.1.99 foo.lab.net foo"

谁能指导我如何添加文件中不存在的新行。我知道我可以通过添加regexp='' insertafter=EOF我的#attempt-1. 但我想在文件中间添加上面的行。

4

1 回答 1

2

As I understand you can use insertafter=<regexp>:

--- play.yml
- hosts: localhost                                                              
  gather_facts: False                                                           
  tasks:                                                                        
    - name: "Configure Node River"                                            
      local_action: 'lineinfile dest=/home/ig/test.conf
                     line="node.river: river_name" insertafter="first line"'

test.conf before:

first line
second line

test conf after:

first line
node.river: river_name
second line
于 2013-12-27T08:24:55.337 回答