How can I change the path for specific host

31 views
Skip to first unread message

Guilherme Ueno

unread,
Dec 12, 2016, 10:11:36 AM12/12/16
to Ansible Project
Hi all,

I have to copy a file into a directory to specific hosts, but the path changes for specific hosts.

Ex.: 

Inventory:

[webserver] 
hosta
hostb



task: Copy a file into:

hosta: /usr/app/abc/dir
hostb: /usr/app/def/dir


But, I don't know how to use those variables in the "same task" using "webserver" as target.

thank you,
Guilherme.

Dick Davies

unread,
Dec 12, 2016, 11:22:23 AM12/12/16
to ansible list
If they're in different groups, make the path a group var.
If they're in the same groups, make the path a host var.
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/a4ec4ad0-6432-40a1-a45b-6e908b506e77%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Guilherme Ueno

unread,
Dec 12, 2016, 1:10:20 PM12/12/16
to Ansible Project, di...@hellooperator.net
Hi Dick,

Thank you for your attention, but what you mean when you says "host var"?

INVENTORY:
[webserver] 
hosta
hostb


TASK1:
- name: copy script to server A
  copy: src=jboss.sh dest=/usr/app/abc/dir mode=u+rwx

TASK2:
- name: copy script to server B
  copy: src=jboss.sh dest=/usr/app/def/dir mode=u+rwx


COMMAND:
ansible-playbook -i inventory environment --limit webserver


The destination is different on both hosts and I can't reduce those tasks to only one, because of subdirectories "abc" and "def".

Thank you again,
Guilherme.

Greg Langford

unread,
Dec 13, 2016, 3:31:27 AM12/13/16
to Ansible Project, di...@hellooperator.net
You can use host vars or group vars like the following.

In the root of your ansible repo where your inventory file sits create the following yaml files for host vars

host_vars/hosta
host_vars/hostb

Within these files you can now specify variables e.g within host_vars/hosta

file_path: /file/path/for/hosta

And then create file host_vars/hostb

file_path: /file/path/for/hostb

Within your playbook you can now specify

TASK1:
- name: copy script to server A
  copy: src=jboss.sh dest="{{ file_path }}" mode=u+rwx

This will use the variable from within your host_vars file, its the same for group vars only the folder is called group_vars and you would instead use the group names instead of the host names.

Kind Regards

Guilherme Ueno

unread,
Dec 13, 2016, 9:14:26 AM12/13/16
to Ansible Project, di...@hellooperator.net
Hi Greg, I've just created the vars as you said and it worked, thank you very much!

Greg Langford

unread,
Dec 15, 2016, 6:12:37 AM12/15/16
to Ansible Project, di...@hellooperator.net
No problem, you are most welcome.

Kind Regards
Reply all
Reply to author
Forward
0 new messages