Skip to content

Instantly share code, notes, and snippets.

@supix
Last active March 21, 2022 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save supix/70ca30e5f5c4eccbdd87854498d34dec to your computer and use it in GitHub Desktop.
Save supix/70ca30e5f5c4eccbdd87854498d34dec to your computer and use it in GitHub Desktop.
Check whether a string is found through grep
---
- name: Check if a string is found through grep
hosts: all
vars:
filename: /tmp/rc.txt
dst_folder: /tmp/hosts
tasks:
- name: delete the centralized file
file:
path: "{{ filename }}"
state: absent
delegate_to: localhost
run_once: true
- name: ensure directory exists
file:
path: "{{ dst_folder }}"
state: directory
delegate_to: localhost
run_once: true
- name: grep the string
shell: grep -ir stringToFind /the/path
register: result
failed_when: false # "result.rc not in [ 0, 1 ]"
- name: print output to screen
debug:
msg: "{{ result.rc }}"
- name: print output to file per host
copy:
content: "{{ result.rc }}"
dest: "{{ dst_folder }}/{{ ansible_hostname }}.txt"
delegate_to: localhost
- name: print output to a centralized file
lineinfile:
path: "{{ filename }}"
line: "{{ ansible_hostname }} {{ result.rc }}"
create: true
delegate_to: localhost
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment