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

后端 未结 8 2077
粉色の甜心
粉色の甜心 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

    You can use the file plugin for this scenario.

    To set a fact you can use in other tasks ... this works.

    - name: Check whether /tmp/my.conf contains "127.0.0.1"
      set_fact:
        myconf: "{{ lookup('file', '/tmp/my.conf') }}"  
      ignore_errors: yes
    
    - name: Greet the world if /tmp/my.conf contains "127.0.0.1"
      debug: msg="Hello, world!"
      when: "'127.0.0.1' in myconf"
    

    To check the file content as a condition of a task ... this should work.

    - name: Greet the world if /tmp/my.conf contains "127.0.0.1"
      debug: msg="Hello, world!"
      when: "'127.0.0.1' in lookup('file', '/tmp/my.conf')"
    

提交回复
热议问题