Ansible: read remote file

AnsibleAnsible Playbook

Ansible Problem Overview


I generate files with ansible on remote host and after this generation, I would like to read theses files in another task.

I don't find any module to read remote file with ansible (lookup seems only on local host).

Do you know a module like this ?

Thanks

EDIT:

Here is my use case:

I generate ssh keys and I add it to github. These keys are setting by an object in var files so I loop like this to generate it:

    tasks:
  - name: Create ssh key
    user:
      name: "{{sshConfigFile.user}}"
      generate_ssh_key: yes
      ssh_key_file: ".ssh/{{item.value.file}}"
      state: present
    with_dict: "{{sshConfiguration}}"

It works very fine but how read these keys to send it to github via the API ?

Ansible Solutions


Solution 1 - Ansible

Either run with the --diff flag (outputs a diff when the destination file changes) ..

ansible-playbook --diff server.yaml

or slurp it up ..

- name: Slurp hosts file
  slurp:
    src: /etc/hosts
  register: slurpfile

- debug: msg="{{ slurpfile['content'] | b64decode }}"

Solution 2 - Ansible

Note that when this question was asked, the following solution was acceptable. Later versions of Ansible may provide a better solution to solve this problem.

As you said, all lookups are on localhost. But all of them can be done on remote by using shell and register. Can you tell what exactly you are trying to do? just an example.

  - shell: cat "{{remote_file}}"
    register: data

  - shell: ......
    with_xxxx:

Solution 3 - Ansible

You can try the ['fetch'][1] module, which will retrieve the key file to a destination path on localhost:

fetch: 
  src: ".ssh/{{item.value.file}}" 
  dest:"/tmp/ssh_keys/{{item.value.file}}"
  flat: yes
with_dict: "{{sshConfiguration}}" 

[1]: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/fetch_module.html "fetch module"

Solution 4 - Ansible

You can register the contents of the file in a variable using the register command. Here is what I would suggest,

- name: get contents of file
  command: cat /path/to/file
  register: filename
  become: true # use case specific option

- name: viewing the contents
  debug:
    msg: "{{filename.stdout}}"

This will display the contents of the file .

Solution 5 - Ansible

By using command module you can read or use that file in another task in remote node.

like

-command: cp /tmp/xxx/example.sh /usr/local/yyy

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKivaView Question on Stackoverflow
Solution 1 - Ansibledanday74View Answer on Stackoverflow
Solution 2 - AnsiblehelloVView Answer on Stackoverflow
Solution 3 - AnsiblevernonmView Answer on Stackoverflow
Solution 4 - AnsibleLuv33preetView Answer on Stackoverflow
Solution 5 - Ansibledevika raniView Answer on Stackoverflow