Ansible – Read file in remote server and use the output to replace a line in configuration file

ansibleansible-playbook

Under /etc/osci we store a single line string for our monitoring name.
I created a ansible playbook that copy-s the default zabbix configuration to the remote server and now I want to use the ansible replace module to replace a string in the newly copied configuration file with the output of /etc/osci

In salt I can execute a cmd.shell that runs in the remote server and reads the file output to a variable(using cat), but I'm having trouble using the same method with Ansible.

What's the correct way to do this?

- shell: cat /etc/osci
  register: data

This does not work, calling out the data variable in system returns 0 output. Is there another way in doing this? I don't really want to use fetch to download the file to local host and read using lookup.

Best Answer

You are probably using the wrong return value of the register command. Adding .stdout should return the content of the file. Use it like this:

- name: Get osci content.
  shell: cat /etc/osci
  register: data

- debug:
    msg: "{{ data.stdout }}"
Related Topic