How do I create a variable shared by a number of tasks in a role in Ansible?

59 views
Skip to first unread message

Mark Butler

unread,
Feb 23, 2014, 1:25:46 PM2/23/14
to ansible...@googlegroups.com

(I created a SO question about this but maybe it would be better to ask here - 

http://stackoverflow.com/questions/21972208/how-do-i-create-a-variable-shared-by-a-number-of-tasks-in-a-role-in-ansible)

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?

Michael DeHaan

unread,
Feb 24, 2014, 2:31:24 PM2/24/14
to ansible...@googlegroups.com
Hi Mark,

Small introductory request -- Please don't post questions to StackOverflow if you are going to ask questions here.   It's kind of like texting us to let us know we have a voicemail, plus, there's a really strong community here that shouldn't be hassled with having to respond both here and on Stack Overflow...  You're going to get a faster answer here most of the time anyway and it saves us from having to surf to answer questions in both places :)

"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?"

There's no such thing, though I'm unclear why you can't define this at inventory or group level.   If it's different jars every time, perhaps this should be a parameter that you would like to pass into a role?






--
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/642dd7bb-aae8-45ae-b237-5154e497117c%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Adam Morris

unread,
Feb 25, 2014, 8:01:31 PM2/25/14
to ansible...@googlegroups.com


On Sunday, February 23, 2014 10:25:46 AM UTC-8, Mark Butler wrote:

(I created a SO question about this but maybe it would be better to ask here - 

Probably, I don't know your Significant Other... :-D

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


Well, it sounds like you need to set a variable and then use it in your task.  group_vars are associated with groups, but there are a whole host of places that you can create that particular variable.  

I would advise reading most of the documentation page on Variables...


Where you should put that variable in your case is pretty much up to you.  The variable precedence section will help you to decide where you should put it to ensure that you get the results that you want.

Adam

Adam Morris

unread,
Feb 25, 2014, 8:08:56 PM2/25/14
to ansible...@googlegroups.com
To be clear the answer is there in the precedence section...  Either you want to define it at the playbook level, at the role level, as a role default, or parameterise it and pass it into the roles...  

I really don't know which works best for you but that variable precedence section covers it.  I found the blog post less than helpful but that might be that the age of it is significantly older than the latest version of Ansible.

I hope that this helps,
      Adam
Reply all
Reply to author
Forward
0 new messages