使用lineinfile替换ansible行

时间:2016-10-14 07:48:23

标签: regex shell ansible

我正在使用ansible更改https主机的行

---
  - name: "change http to https"
    lineinfile:
    dest: "{{ install_dir }}/xx/abc-out.xml"
    state: present
    regexp: "<column name="PARAM_VALUE">http://host.com:42003</column>"
    line:  "<column name="PARAM_VALUE">https://host.com:42091</column>"

---
- name: Change line
  vars_files:
    - vars/conn.yml
  become_user: "{{ app_user }}"
  become: true
  roles:
      - { role: https-host }

它抱怨该线路有问题。 任何人都可以帮忙指出来吗?

2 个答案:

答案 0 :(得分:0)

尝试使用像这样的单引号 -

    regexp: '<column name="PARAM_VALUE">http://host.com:42003</column>'
    line:  '<column name="PARAM_VALUE">https://host.com:42091</column>'

此外,我发现您在问题中提到的示例中,deststateregexpline属性未正确缩进。它看起来应该是这样的 -

---
- name: "change http to https"
  lineinfile:
    dest: "{{ install_dir }}/xx/abc-out.xml"
    state: present
    regexp: '<column name="PARAM_VALUE">http://host.com:42003</column>'
    line:  '<column name="PARAM_VALUE">https://host.com:42091</column>'

答案 1 :(得分:0)

regexp: '         <column name="PARAM_VALUE">https://host.com:12360</column>'

正如我在评论中提到的那样。单引号的间距和更改有帮助。