Question

I am creating a task file in an Ansible role using the best practice directory layout e.g.

myscripts/roles
myscripts/roles/the_role
myscripts/roles/the_role/tasks
myscripts/roles/the_role/tasks/main.yml

In main.yml, I need to call a number of Java tasks so I have to specify the classpath each time e.g.

- name: java | Do something with Bar Java class
  action: command java -cp A.jar:B.jar:C.jar com.example.Bar myargs

- name: java | Do something with Foo Java class
  action: command java -cp A.jar:B.jar:C.jar com.example.Foo myOtherargs

so I would to use a variable to replace A.jar:B.jar:C.jar

I have been looking at the documentation and this blog post. It seems possible to make a variable that is associated with a host or a group of hosts. However this variable should be associated with a task - how do I do that? You can do it in a Playbook but I can't get it to work because I have split the Playbook into roles?

Était-ce utile?

La solution

Mark,

You can create vars putting them in main.yml under vars.

Following your directory structure, it would be there :

myscripts/roles/the_role/vars/main.yml

See the directory layout section in the Best Practices page.

For instance :

class_path: "A.jar:B.jar:C.jar"

Then in your tasks :

- name: java | Do something with Bar Java class
  action: command java -cp {{ classpath }} com.example.Bar myargs

- name: java | Do something with Foo Java class
  action: command java -cp {{ classpath }} com.example.Foo myOtherargs

Good luck.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top