Rajesh Devan
Rajesh Devan

Reputation: 9

hostname of the target in ansible

I have to copy files, which are target specific files. I have stored these files in my machine as their target hostnames.

Example:

/tmp/Server1.cfg /tmp/Server2.cfg

host file has 

Server1
Server2

When my playbook runs for Server1 it should copy Server1.cfg. When my playbook runs for Server2 it should copy Server2.cfg.

How can I achieve this ?

Thanks. PS: Please be explicit as I am still a toddler in ansible

Upvotes: 0

Views: 3835

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68319

You may want to read some chapters at docs.ansible.com:

Additionally, inventory_hostname is the name of the hostname as configured in Ansible’s inventory host file. This can be useful for when you don’t want to rely on the discovered hostname ansible_hostname or for other mysterious reasons.

So, in your case:

- copy:
    src: "{{ inventory_hostname }}"
    dest: "/tmp/{{ inventory_hostname }}.cfg"

Upvotes: 2

Related Questions