Question:
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 check.
1 2 |
grep -Fxq "127.0.0.1" /tmp/my.conf |
Answer:
1 2 3 4 5 6 7 8 9 10 11 |
- name: Check whether /tmp/my.conf contains "127.0.0.1" command: grep -Fxq "127.0.0.1" /tmp/my.conf register: checkmyconf check_mode: no ignore_errors: yes changed_when: no - name: Greet the world if /tmp/my.conf contains "127.0.0.1" debug: msg="Hello, world!" when: checkmyconf.rc == 0 |
Note: Older Ansible versions need to use
always_run: yes
instead of check_mode: no
.