60

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 ?

Kiva
  • 9,193
  • 17
  • 62
  • 94

5 Answers5

144

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 }}"
AndiDog
  • 68,631
  • 21
  • 159
  • 205
danday74
  • 52,471
  • 49
  • 232
  • 283
13

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:
DaveyDaveDave
  • 9,821
  • 11
  • 64
  • 77
helloV
  • 50,176
  • 7
  • 137
  • 145
  • I tried to do that, but I can't because I need to loop on another object. – Kiva Jan 12 '16 at 08:54
  • 10
    bad answer, avoid shell and command modules wherever possible (they are not naturally idempotent), you should be using the slurp module .. arguable that because this is a read only op a shell command is acceptable but best to get into the habit of avoiding shell commands where a natural ansible solution exists .. see my answer below for best approach – danday74 Jan 14 '17 at 23:13
8

You can try the 'fetch' 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}}" 
mattp
  • 66
  • 8
vernonm
  • 141
  • 2
  • 7
  • 1
    good answer but slurp is better which avoids creating a tmp file on localhost .. the docs for fetch say that under the hood it uses slurp – danday74 Jan 14 '17 at 23:07
  • 1
    @danday74 You are correct in that using slurp will avoid the temp file. However, if you reread the note for fetch, it states "*When running fetch with `become`, the slurp module will also be used [...]*". In this example, he is not using become – Oberst Apr 05 '17 at 14:43
3

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 .

Luv33preet
  • 1,686
  • 7
  • 33
  • 66
-7

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