How to copy directory based on a hostname in a group in Ansible? -


i have 2 hosts in group:

[group-1]  hostname1 hostname2 

i have 2 directories hostname1,hostname2 under /output/. want copy output/hostname1 host1 , output/hostname2 host 2 how can construct task copy hostname1 directory host hostname1 , homename2 play this:

- name: copy_play   hosts: group-1   gather_facts: true   roles:     - vlsconfig 

i tried writing task:

 - name: "copying"    copy: src="{{ base_dir }}/{{ item}} dest="/home/ec2-user" mode=0755    with_items: "{{ groups['cdh-all'] }}" 

but problem it's copying directories: hostname1 , hostname2 both hosts.

this should work you:

- name: "copying"   copy:     src: "{{ base_dir }}/{{ inventory_hostname }}"     dest: "/home/ec2-user"     mode: 0755 

you don't need loops here. task run once on each target , copy 1 directory. value of inventory_hostname fact different on each host.


Comments