Atualmente estou construindo um playbook para testar se alguns arquivos conf existem e, em seguida, verificar o conteúdo. Os arquivos são os seguintes
- /etc/resolv.conf - verifique se os servidores de nomes estão bem configurados
- /etc/systemd/timesyncd.conf - verifica se algo foi configurado
- /etc/ntp.conf - verifique também se algo foi configurado
O código .yml é o seguinte, como você pode ver, a tarefa é a mesma para todas as verificações, apenas reconfigurando o caminho do arquivo e a parte regex, se necessário.
tasks:
# RESOLV.CONF
- name: Check if resolv.conf exist
stat:
path: /etc/resolv.conf
register: resolv_conf
- name: Retrieve nameservers
debug:
msg: "{{ contents }}"
vars:
contents: "{{ lookup('file', '/etc/resolv.conf') | regex_findall('\\s*nameserver\\s*(.*)') }}"
when: resolv_conf.stat.exists == True
# NTP.CONF
- name: check if ntp.conf exists
stat:
path: /etc/ntp.conf
register: ntp_conf
- name: retrieve ntp conf server content
debug:
msg: "{{ contents }}"
vars:
contents: "{{ lookup('file', '/etc/ntp.conf') | regex_search('^server.*') }}"
when: ntp_conf.stat.exists == True
# TIMESYNC.CONF
- name: check if timesyncd
stat:
path: /etc/systemd/timesyncd.conf
register: timesyncd_conf
- name: Affiche le contenu de timesyncd.conf s'il est configure
debug:
msg: "{{ contents }}"
vars:
contents: "{{ lookup('file', '/etc/systemd/timesyncd.conf') | regex_search('^NTP=.*') }}"
when: timesyncd_conf.stat.exists == True
As tarefas estão funcionando bem, exceto aquela sobre a verificação de NTP.CONF que falha com o seguinte:
vendredi 07 octobre 2022 08:28:07 +0200 (0:00:00.509) 0:00:05.115 ******
[WARNING]: Unable to find '/etc/ntp.conf' in expected paths (use -vvvvv to see paths)
fatal: [my_server]: FAILED! => {"msg": "An unhandled exception occurred while templating '{{ lookup('file', '/etc/ntp.conf') | regex_search('^server.*') }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: An unhandled exception occurred while running the lookup plugin 'file'. Error was a <class 'ansible.errors.AnsibleError'>, original message: could not locate file in lookup: /etc/ntp.conf. could not locate file in lookup: /etc/ntp.conf"}
Eu não entendo por que ele falha, pois uso a mesma função, usuários e arquivos com os mesmos direitos são alguns outros em /etc/. Além disso, tentei rapidamente fazer o mesmo com "cat" e funciona:
- name: check ntp.conf content
command: "cat /etc/ntp.conf"
register: ntp_conf_contenu
- debug:
msg:
- " {{ ntp_conf_contenu | regex_findall ('server') }}"
Você tem alguma idéia de por que ele falha?
Muito obrigado !
As pesquisas não são executadas no host remoto, elas são executadas localmente.
Da documentação :
Então você verifica se o arquivo existe na máquina remota e então você o lê da sua máquina local onde o playbook é executado.
Para ler um arquivo da máquina remota, você pode usar o módulo slurp .