Only check whether a line present in a file (ansible)

后端 未结 8 2007
粉色の甜心
粉色の甜心 2020-12-01 02:36

In ansible, I need to check whether a particular line present in a file or not. Basically, I need to convert the following command to an ansible task. My goal is to only che

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 03:11

    User robo's regexp & absent method is quite clean, so I've fleshed it out here for easy use and added improvements from comments by @assylias and @Olivier:

    - name: Ensure /tmp/my.conf contains 127.0.0.1
      lineinfile:
        path: /tmp/my.conf
        regexp: '^127\.0\.0\.1.*whatever'
        state: absent
      check_mode: yes
      changed_when: false
      register: out
    
    - debug:
        msg: "Yes, line exists."
      when: out.found
    
    - debug:
        msg: "Line does NOT exist."
      when: not out.found
    

提交回复
热议问题