78

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.

grep -Fxq "127.0.0.1" /tmp/my.conf

9 Answers 9

96

Use check_mode, register and failed_when in concert. This fails the task if the lineinfile module would make any changes to the file being checked. Check_mode ensures nothing will change even if it otherwise would.

- name: "Ensure /tmp/my.conf contains '127.0.0.1'"
  lineinfile:
    name: /tmp/my.conf
    line: "127.0.0.1"
    state: present
  check_mode: yes
  register: conf
  failed_when: (conf is changed) or (conf is failed)
4
  • 1
    For ansible 2.2 use name: instead of path:
    – kubanczyk
    May 23, 2017 at 13:39
  • 1
    This way you can use the same task for checking and later deploying the change, just with different values for check_mode and failed_when.
    – nlu
    Mar 22, 2018 at 16:43
  • 16
    I used this approach, but set state: absent and used the regexp parameter rather than line. That gives you pretty much all the flexibility of using grep, and the value of changed in the registered variable equates to whether the pattern was found or not.
    – robo
    Jun 14, 2018 at 15:53
  • 1
    This can also be nicely combined with a with_x ansible loop. Jan 8, 2019 at 17:51
57
- 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

Update 2017-08-28: Older Ansible versions need to use always_run: yes instead of check_mode: no.

6
  • 2
    I admit this isn't an optimal solution, because it's harder to port it to Windows. Let me see if there's anything else... Nope, I can't find any other way. Jun 12, 2015 at 8:08
  • 1
    I don't think check_mode is valid here, as it just skips performing the check. Nov 24, 2017 at 17:42
  • @AsfandQazi AFAIR check_mode: True means the command will be executed always; if we don't specify it, it will be skipped when Ansible runs in check mode. Nov 25, 2017 at 8:06
  • 1
    I think you have your check_mode options mixed. According to this, you need check_mode: false : docs.ansible.com/ansible/latest/… Nov 27, 2017 at 16:54
  • 3
    Instead of when: checkmyconf.rc == 0 you can also use when: not checkmyconf.failed which is a bit more readable.
    – assylias
    Apr 18, 2019 at 20:48
48

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
  ansible.builtin.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
8
  • 4
    It works but the downside is that the first task has a "changed" status when the line is present. This can be fixed by adding changed_when: false.
    – assylias
    Apr 18, 2019 at 20:50
  • 2
    For me the best solution is his answer, with assylias comment (changed_when: false), and then use out.found to check if a line was found.
    – Olivier
    Oct 3, 2019 at 12:34
  • @Olivier: I've updated the answer with changed_when: false and out.found. ty
    – Donn Lee
    Oct 4, 2019 at 17:53
  • 1
    I get 'dict object' has no attribute 'found'
    – Random Guy
    Jan 11, 2022 at 13:28
  • @DanielVazome the registered variable won't have the found key if it either wasn't able to find the file itself or didn't have access to it, e.g. perhaps the task required a become.
    – timss
    Feb 10, 2022 at 15:43
14

With the accepted solution, even though you ignore errors, you will still get ugly red error output on the first task if there is no match:

TASK: [Check whether /tmp/my.conf contains "127.0.0.1"] ***********************
failed: [localhost] => {"changed": false, "cmd": "grep -Fxq "127.0.0.1" /tmp/my.conf", "delta": "0:00:00.018709", "end": "2015-09-27 17:46:18.252024", "rc": 1, "start": "2015-09-27 17:46:18.233315", "stdout_lines": [], "warnings": []}
...ignoring

If you want less verbose output, you can use awk instead of grep. awk won't return an error on a non-match, which means the first check task below won't error regardless of a match or non-match:

- name: Check whether /tmp/my.conf contains "127.0.0.1"
  command: awk /^127.0.0.1$/ /tmp/my.conf
  register: checkmyconf
  changed_when: False

- name: Greet the world if /tmp/my.conf contains "127.0.0.1"
  debug: msg="Hello, world!"
  when: checkmyconf.stdout | match("127.0.0.1")

Notice that my second task uses the match filter as awk returns the matched string if it finds a match.

The alternative above will produce the following output regardless of whether the check task has a match or not:

TASK: [Check whether /tmp/my.conf contains "127.0.0.1"] ***********************
ok: [localhost]

IMHO this is a better approach as you won't ignore other errors in your first task (e.g. if the specified file did not exist).

1
  • 4
    You can use failed_when: false instead of ignore_errors to avoid the error message.
    – augurar
    Nov 30, 2015 at 20:13
6

Use ansible lineinfile command, but this command will update the file with the line if it does not exists.

- lineinfile: dest=/tmp/my.conf line='127.0.0.1' state=present
2
  • I agree to Antonis Christofides, I think you will have to use the command module for this purpose. Any alternative seems to be a work around than an ansible solution.
    – VSK
    Jun 12, 2015 at 8:44
  • 25
    It would be nice if a state=check was available in the lineinfile module
    – mixja
    Sep 27, 2015 at 5:04
5

Another way is to use the "replace module" then "lineinfile module".

The algo is closed to the one used when you want to change the values of two variables.

  • First, use "replace module" to detect if the line you are looking for is here and change it with the something else. (Like same line + something at the end).
  • Then if "replace" is true, It means your line is here then replace the new line with a particularity, with the new line looking.
  • Else the line you are looking for is not here.

Example:

# Vars
- name: Set parameters
  set_fact:
    newline      : "hello, i love ansible"
    lineSearched : "hello"
    lineModified : "hello you"

# Tasks
- name: Try to replace the line
  replace:
    dest    : /dir/file
    replace : '{{ lineModified }} '
    regexp  : '{{ lineSearched }}$'
    backup  : yes
  register  : checkIfLineIsHere

- name: Line is here, change it
  lineinfile:
    state   : present
    dest    : /dir/file
    line    : '{{ newline }}'
    regexp  : '{{ lineModified }}$'
  when: checkIfLineIsHere.changed
  • If the file contains "hello", it will become "hello you" then "hello, i love ansible" at the end.
  • If the file content doesn't contain "hello", the file is not modified.

With the same idea, you can do something if the lineSearched is here:

# Vars
- name: Set parameters
  set_fact:
    newline      : "hello, i love ansible"
    lineSearched : "hello"
    lineModified : "hello you"

# Tasks
- name: Try to replace the line
  replace:
    dest    : /dir/file
    replace : '{{ lineModified }} '
    regexp  : '{{ lineSearched }}$'
    backup  : yes
  register  : checkIfLineIsHere

# If the line is here, I want to add something.
- name: If line is here, do something
  lineinfile:
    state   : present
    dest    : /dir/file
    line    : '{{ newline }}'
    regexp  : ''
    insertafter: EOF
  when: checkIfLineIsHere.changed

# But I still want this line in the file, Then restore it
- name: Restore the searched line.
  lineinfile:
    state   : present
    dest    : /dir/file
    line    : '{{ lineSearched }}'
    regexp  : '{{ lineModified }}$'
  when: checkIfLineIsHere.changed
  • If the file contains "hello", the line will still contain "hello" and "hello, i love ansible" at the end.
  • If the file content doesn't contain "hello", the file is not modified.
1
  • Nice example ! As some might argue it is a lot complexer than using command module, but I prefer ansible solutions, even when there not always obvious at first. I'm trying to use the capture groups like in sed to get a specific string from a file. Any ideas ? for example sed -n 's:secret\ \"\(.*\)\".*:\1:p' tsig-key with line in file Jul 24, 2020 at 12:25
2

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')"
1
  • 4
    This will only work for local file on the ansible controller machine as lookups always run on the controller. To use this method for remote files, you would have to copy/slurp them locally before. Jan 8, 2020 at 16:56
1

Another solution, also useful for other purposes is to loop over the contents of the file, line by line

- name: get the file
  slurp:
    src: /etc/locale.gen
  register: slurped_file

- name: initialize the matches list
  set_fact:
    MATCHES: []

- name: collect matches in a list
  set_fact:
    MATCHES: "{{ MATCHES + [line2match] }}"
  loop: "{{ file_lines }}"
  loop_control:
    loop_var: line2match
  vars:
    - decode_content: "{{ slurped_file.content | b64decode }}"
    - file_lines: "{{ decode_content.split('\n') }}"
  when: '"BE" in line2match'
  
- name: report matches if any
  debug:
    msg: "Found {{ MATCHES | length }} matches\n{{ MATCHES }}"
  when: 'listlen | int > 0'
  vars:
    listlen: "{{ MATCHES | length }}"

This mechanism can be use to get a specific value from a matched line if you use it for example with the jinja regex_replace filter

0

The question shows grep with -Fx which means a whole line fixed string. But many people will land here looking for a way to do it with a regex.

Option 1 using shell (simpler):

- ansible.builtin.shell: grep '.* bar baz' /some/file
  changed_when: false

Option 2 using ansible itself (for the purists, and Windows users):

- ansible.builtin.lineinfile:
    path: /some/file
    regex: ".* bar baz"
    state: absent            # here's the trick
  changed_when: false
  check_mode: true
  register: result
  failed_when: result.found != 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.