1

I need to have my ansible playbook to pull a file via scp to each host in inventory from a host not in inventory.

In my example I am running my playbook from 10.3.3.10

my inventory:

[linux]
10.2.2.10
10.2.2.11
10.2.2.12

Here is an example command of what I want to run on each host.

scp admin@10.10.10.100:/backup/scripts/special_script /root

I cant seem to find a way to accomplish this with fetch outside of just running shell and using the scp command as is.

Is there a way to accomplish this with fetch, or a built in idempotent method?

Dave
  • 727
  • 1
  • 9
  • 20
  • For sure having an initial play in your playbook that targets the 10.10.100 host to pull that file back to the controller to make it available for the next play(s) to use. The trick is going to be that your `scp` command likely uses _your_ ssh to authenticate `admin` and in order to make the play useful you'll need to teach _ansible_ how to do that same – mdaniel Mar 24 '23 at 02:54
  • 1
    You can [sync](https://stackoverflow.com/questions/61720113/sync-files-between-2-hosts-using-ansible/61722159#61722159) whatever you want If the hosts can *ssh* each other and you can install *rsync* on them. The next limitation might by Python. The source must by an Ansible client. – Vladimir Botka Mar 24 '23 at 03:05

1 Answers1

2

Q: Is there a way to accomplish this ... in idempotent method?"

A: Yes. You can make the command idempotent by using the parameter creates. Quoting:

If a matching file already exists, this step will not be run.

- command:
    cmd: scp admin@10.10.10.100:script /root
    creates: /root/script

There are many other options depending on the bandwidth, file size, and environment. See Ansible - fetch files from one remote node to another

  • If you can install Ansible on 10.10.10.100 the trivial option would be to run a play there.

  • It's also possible to create a script to rsync the files from the command line on 10.10.10.100

  • Get the file on the controller first.

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63