如何在ansible剧本中添加新行

时间:2013-12-27 06:02:25

标签: ansible ansible-playbook

我正在尝试使用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来添加文件结尾。但我想在文件中间添加上面的行。

1 个答案:

答案 0 :(得分:2)

据我了解,您可以使用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:

first line
second line

测试conf后:

first line
node.river: river_name
second line
相关问题