For inventory purpose due to some business limitation, i wrote a playbook that retrieve content of resolv.conf + ntp.conf + timesyncd.conf and then, write the retrieved content within a CSV file.
Globally, maybe it's not the best practice but i set a fact at every task for retrieving file content, then i format the datas as i would and finally, write them in a csv like data1;data2;data3;
I got some issues/questions there :
- if the file doesn't exists,Ansible task fails and doesn't go to the next task, i know this is by design. To prevent that, should it be a good solution to use failed_when/changed_when conditionals near retrieving tasks ?
- in the last task ("Write results to ...") how to handle non-existing fact ? at the moment if 3 facts are existing, file is written. If 1/3 facts doesn't exists but others are , nothing is written. Thanks a lot for your advices. Playbook is below
---
- name: sys-check_conf_ntp_dns_net
hosts: my_servers
remote_user: my_user
tasks:
# RESOLV.CONF
- name: Retrieve remote /etc/resolv.conf
ansible.builtin.slurp:
src: /etc/resolv.conf
register: resolv_conf
- name: Format resolv_conf_fact data
set_fact:
resolv_conf_fact: "{{ (resolv_conf['content'] | b64decode) | regex_findall('\\s*nameserver\\s*(.*)') }}"
# NTP.CONF
- name: Retrieve remote /etc/ntp.conf
ansible.builtin.slurp:
src: /etc/ntp.conf
register: ntp_conf
- name: Format ntp_conf_fact data
set_fact:
ntp_conf_fact: "{{ (ntp_conf['content'] | b64decode) | regex_findall('(\\nserver.*?)(\\n)') }}"
# TIMESYNCD.CONF
- name: Retrieve /etc/systemd/timesyncd.conf
ansible.builtin.slurp:
src: /etc/systemd/timesyncd.conf
register: timesyncd_conf
- name: Format timesyncd_conf_fact data
set_fact:
timesyncd_conf_fact: "{{ (timesyncd_conf['content'] | b64decode) | regex_search('(NTP=f.*)') }}"
- name: Write results to /tmp/sys-check_conf_ntp_dns_net.csv
lineinfile:
path: /tmp/sys-check_conf_ntp_dns_net.csv
line: "Hostname:{{inventory_hostname}};resolv.conf:{{ resolv_conf_fact }};ntp.conf:{{ ntp_conf_fact }};timesyncd.conf:{{ timesyncd_conf_fact }};"
create: yes
delegate_to: localhost
EDIT
I finally found a workaround, not sure if it's legal or not :D For every fact i set, i add a default value, then as it's filtered through the regex, it behaves like the fact is not empty and so seems to work at the end.
For example :
Setting a fact before :
ntp_conf_fact: "{{ (ntp_conf['content'] | b64decode) | regex_findall('(\\nserver.*?)(\\n)') }}"
Setting a fact after :
ntp_conf_fact: "{{ ((ntp_conf['content']|default([blabla])) | b64decode) | regex_findall('(\\nserver.*?)(\\n)') }}"
Can someone confirm if it sounds ok ? or if anyone got a different solution ?
0 Answers