Discussion:
various questions about using ansible - inventory, sharing handlers, lists and maps
Mark Butler
2014-02-24 19:45:19 UTC
Permalink
Hello team,

I am getting started with ansible but I have a number of questions.
Apologies in advance for the rather noob questions:


1. In my inventory, is there anyway I can specify a single domain for all
the hosts e.g.

[myhosts]
hostA
hostB

rather than

[myhosts]
hostA.example.com
hostB.example.com

Given the requirement that I might be run tests from outside the domain?


2. Many of my roles need to call supervisor when they have finished so they
all use the same handler:

---
- name: restart supervisor
service: name=supervisor state=restarted

However at the moment I have the same duplicated handler file for each role
- how can I avoid this and have a single handler file?


3. Is it possible to create strings from list?

I need to create a classpath variable - this is how I do it currently:

classpath: "{{ dest }}jarA.jar:{{ dest }}jarB.jar:{{ dest }}jarC.jar:{{
dest }}jarD.jar"

In Python I could use a loop to do this. Is there any way to do this in
Ansible?


4. Do map style structures exist?

Similarly, when I am getting these jars I use a list like this - ideally it
would be better to use a map, then generate the list from the map to avoid
configuration duplication. Is there any way to achieve this?

- name: my service | Get jars
action: get_url url={{ build_url }}lastSuccessfulBuild/artifact/{{ item
}} dest={{ dest }} mode=0440
with_items:
- pathA/jarA
- pathB/jarB
- pathC/jarC
- pathD/jarD


Thanks in advance,

Mark
--
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-project+***@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/58e4fa83-8ce3-41fc-a6f6-48e8c2f52f3a%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Michael DeHaan
2014-02-24 19:55:21 UTC
Permalink
Post by Mark Butler
Hello team,
I am getting started with ansible but I have a number of questions.
1. In my inventory, is there anyway I can specify a single domain for all
the hosts e.g.
[myhosts]
hostA
hostB
rather than
[myhosts]
hostA.example.com
hostB.example.com
Given the requirement that I might be run tests from outside the domain?
There is not a way to set the domain name suffix on a group all together.

OS mechanisms on short filenames (i.e. resolv.conf) will apply
Post by Mark Butler
2. Many of my roles need to call supervisor when they have finished so
---
- name: restart supervisor
service: name=supervisor state=restarted
However at the moment I have the same duplicated handler file for each
role - how can I avoid this and have a single handler file?
Yes, you can define the handler in a common role and just use it once.

It's ok if the role doesn't have a tasks file too.
Post by Mark Butler
3. Is it possible to create strings from list?
classpath: "{{ dest }}jarA.jar:{{ dest }}jarB.jar:{{ dest }}jarC.jar:{{
dest }}jarD.jar"
Yes! See the "set_fact" module in the module docs, or just define a
variable like so anywhere else in Ansible.

Variables are lazy-evaluated at the time of use.
Post by Mark Butler
In Python I could use a loop to do this. Is there any way to do this in
Ansible?
4. Do map style structures exist?
Similarly, when I am getting these jars I use a list like this - ideally
it would be better to use a map, then generate the list from the map to
avoid configuration duplication. Is there any way to achieve this?
- name: my service | Get jars
action: get_url url={{ build_url }}lastSuccessfulBuild/artifact/{{ item
}} dest={{ dest }} mode=0440
- pathA/jarA
- pathB/jarB
- pathC/jarC
- pathD/jarD
So what you have above works.

What would be in your map/dictionary? You can definitely iterate across a
list of dictionaries and hopefully I can help explain further -- just need
a bit more context.

Thanks!
Post by Mark Butler
Thanks in advance,
Mark
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/58e4fa83-8ce3-41fc-a6f6-48e8c2f52f3a%40googlegroups.com
.
For more options, visit https://groups.google.com/groups/opt_out.
--
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-project+***@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/CAEVJ8QMT3390_okm6JZmN3tqZ5gejG7n5ufM9qXwXsnBYBauZg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.
Mark Butler
2014-02-24 20:09:56 UTC
Permalink
Hi Michael,

Sorry about the SO mix-up.

The reason I don't want to define it at the inventory or group level is it
isn't associated with them - it's associated with the role - because the
service wherever it runs will use the same jars - and because the jars
often have version numbers in that will change slowly with release - so it
doesn't make sense to pass them in every time but also it makes sense to
have them in a config so they can be changed without breaking the template.

Actually, it turns out it's easy and I was being stupid because the "best
practice" document does say that I can create vars at the role level (as
Leucos pointed out, thanks!). My next question was going to be if roles can
share vars but I am guessing by your reply above they can via the common
task just like handlers.

For the list and map, this is how I would do it Python - I'm just keen to
avoid duplicate config values.

build_server_path = 'http://builderserver/path'
dest_path = '/dest/path/'
jars = {'pathA':'jarA', 'pathB': 'jarB', 'pathC': 'jarC'}

# jars have path on build server because they come from different modules
urls = [build_server_path + k + '/' + jars[k] for k in jars]

# but for service execution put them one directory and generate classpath
to simplify things
dest_jars = ['/dest/path/' + jars[k] for k in jars]
classpath = ':'.join(dest_jars)

download(urls, dest_path)
set_classpath(classpath)

Thanks,

Mark
Post by Michael DeHaan
Post by Mark Butler
Hello team,
I am getting started with ansible but I have a number of questions.
1. In my inventory, is there anyway I can specify a single domain for all
the hosts e.g.
[myhosts]
hostA
hostB
rather than
[myhosts]
hostA.example.com
hostB.example.com
Given the requirement that I might be run tests from outside the domain?
There is not a way to set the domain name suffix on a group all together.
OS mechanisms on short filenames (i.e. resolv.conf) will apply
Post by Mark Butler
2. Many of my roles need to call supervisor when they have finished so
---
- name: restart supervisor
service: name=supervisor state=restarted
However at the moment I have the same duplicated handler file for each
role - how can I avoid this and have a single handler file?
Yes, you can define the handler in a common role and just use it once.
It's ok if the role doesn't have a tasks file too.
Post by Mark Butler
3. Is it possible to create strings from list?
classpath: "{{ dest }}jarA.jar:{{ dest }}jarB.jar:{{ dest }}jarC.jar:{{
dest }}jarD.jar"
Yes! See the "set_fact" module in the module docs, or just define a
variable like so anywhere else in Ansible.
Variables are lazy-evaluated at the time of use.
Post by Mark Butler
In Python I could use a loop to do this. Is there any way to do this in
Ansible?
4. Do map style structures exist?
Similarly, when I am getting these jars I use a list like this - ideally
it would be better to use a map, then generate the list from the map to
avoid configuration duplication. Is there any way to achieve this?
- name: my service | Get jars
action: get_url url={{ build_url }}lastSuccessfulBuild/artifact/{{ item
}} dest={{ dest }} mode=0440
- pathA/jarA
- pathB/jarB
- pathC/jarC
- pathD/jarD
So what you have above works.
What would be in your map/dictionary? You can definitely iterate across
a list of dictionaries and hopefully I can help explain further -- just
need a bit more context.
Thanks!
Post by Mark Butler
Thanks in advance,
Mark
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/58e4fa83-8ce3-41fc-a6f6-48e8c2f52f3a%40googlegroups.com
.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to a topic in the
Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/ansible-project/7N9qqACa3lI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CAEVJ8QMT3390_okm6JZmN3tqZ5gejG7n5ufM9qXwXsnBYBauZg%40mail.gmail.com
.
For more options, visit https://groups.google.com/groups/opt_out.
--
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-project+***@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/CAC_tV581HSSALTmM-7f2JVVAW%2B3yV96udyeer2rRp%3D6-HDMwZQ%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.
Mark Butler
2014-02-24 23:44:37 UTC
Permalink
Hi Michael, Team,

One more question I am afraid - is there any way I can share templates
between roles?

I tried putting my template in common/templates but it didn't work?

Thanks!

Mark
Post by Mark Butler
Hi Michael,
Sorry about the SO mix-up.
The reason I don't want to define it at the inventory or group level is it
isn't associated with them - it's associated with the role - because the
service wherever it runs will use the same jars - and because the jars
often have version numbers in that will change slowly with release - so it
doesn't make sense to pass them in every time but also it makes sense to
have them in a config so they can be changed without breaking the template.
Actually, it turns out it's easy and I was being stupid because the "best
practice" document does say that I can create vars at the role level (as
Leucos pointed out, thanks!). My next question was going to be if roles can
share vars but I am guessing by your reply above they can via the common
task just like handlers.
For the list and map, this is how I would do it Python - I'm just keen to
avoid duplicate config values.
build_server_path = 'http://builderserver/path'
dest_path = '/dest/path/'
jars = {'pathA':'jarA', 'pathB': 'jarB', 'pathC': 'jarC'}
# jars have path on build server because they come from different modules
urls = [build_server_path + k + '/' + jars[k] for k in jars]
# but for service execution put them one directory and generate classpath
to simplify things
dest_jars = ['/dest/path/' + jars[k] for k in jars]
classpath = ':'.join(dest_jars)
download(urls, dest_path)
set_classpath(classpath)
Thanks,
Mark
Post by Michael DeHaan
Post by Mark Butler
Hello team,
I am getting started with ansible but I have a number of questions.
1. In my inventory, is there anyway I can specify a single domain for
all the hosts e.g.
[myhosts]
hostA
hostB
rather than
[myhosts]
hostA.example.com
hostB.example.com
Given the requirement that I might be run tests from outside the domain?
There is not a way to set the domain name suffix on a group all together.
OS mechanisms on short filenames (i.e. resolv.conf) will apply
Post by Mark Butler
2. Many of my roles need to call supervisor when they have finished so
---
- name: restart supervisor
service: name=supervisor state=restarted
However at the moment I have the same duplicated handler file for each
role - how can I avoid this and have a single handler file?
Yes, you can define the handler in a common role and just use it once.
It's ok if the role doesn't have a tasks file too.
Post by Mark Butler
3. Is it possible to create strings from list?
classpath: "{{ dest }}jarA.jar:{{ dest }}jarB.jar:{{ dest }}jarC.jar:{{
dest }}jarD.jar"
Yes! See the "set_fact" module in the module docs, or just define a
variable like so anywhere else in Ansible.
Variables are lazy-evaluated at the time of use.
Post by Mark Butler
In Python I could use a loop to do this. Is there any way to do this in
Ansible?
4. Do map style structures exist?
Similarly, when I am getting these jars I use a list like this - ideally
it would be better to use a map, then generate the list from the map to
avoid configuration duplication. Is there any way to achieve this?
- name: my service | Get jars
action: get_url url={{ build_url }}lastSuccessfulBuild/artifact/{{
item }} dest={{ dest }} mode=0440
- pathA/jarA
- pathB/jarB
- pathC/jarC
- pathD/jarD
So what you have above works.
What would be in your map/dictionary? You can definitely iterate across
a list of dictionaries and hopefully I can help explain further -- just
need a bit more context.
Thanks!
Post by Mark Butler
Thanks in advance,
Mark
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/58e4fa83-8ce3-41fc-a6f6-48e8c2f52f3a%40googlegroups.com
.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to a topic in the
Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/ansible-project/7N9qqACa3lI/unsubscribe
.
To unsubscribe from this group and all its topics, send an email to
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CAEVJ8QMT3390_okm6JZmN3tqZ5gejG7n5ufM9qXwXsnBYBauZg%40mail.gmail.com
.
For more options, visit https://groups.google.com/groups/opt_out.
--
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-project+***@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/CAC_tV58e8%2B2ww_vcm_VEQPe-UwXE_PMZt4pMzOFnmRmhxDROoA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.
Michael DeHaan
2014-02-25 00:07:39 UTC
Permalink
You can ../../common/templates/foo.j2 in the src=

-- Michael

On Feb 24, 2014, at 6:44 PM, Mark Butler <***@gmail.com> wrote:

Hi Michael, Team,

One more question I am afraid - is there any way I can share templates
between roles?

I tried putting my template in common/templates but it didn't work?

Thanks!

Mark
Post by Mark Butler
Hi Michael,
Sorry about the SO mix-up.
The reason I don't want to define it at the inventory or group level is it
isn't associated with them - it's associated with the role - because the
service wherever it runs will use the same jars - and because the jars
often have version numbers in that will change slowly with release - so it
doesn't make sense to pass them in every time but also it makes sense to
have them in a config so they can be changed without breaking the template.
Actually, it turns out it's easy and I was being stupid because the "best
practice" document does say that I can create vars at the role level (as
Leucos pointed out, thanks!). My next question was going to be if roles can
share vars but I am guessing by your reply above they can via the common
task just like handlers.
For the list and map, this is how I would do it Python - I'm just keen to
avoid duplicate config values.
build_server_path = 'http://builderserver/path'
dest_path = '/dest/path/'
jars = {'pathA':'jarA', 'pathB': 'jarB', 'pathC': 'jarC'}
# jars have path on build server because they come from different modules
urls = [build_server_path + k + '/' + jars[k] for k in jars]
# but for service execution put them one directory and generate classpath
to simplify things
dest_jars = ['/dest/path/' + jars[k] for k in jars]
classpath = ':'.join(dest_jars)
download(urls, dest_path)
set_classpath(classpath)
Thanks,
Mark
Post by Michael DeHaan
Post by Mark Butler
Hello team,
I am getting started with ansible but I have a number of questions.
1. In my inventory, is there anyway I can specify a single domain for
all the hosts e.g.
[myhosts]
hostA
hostB
rather than
[myhosts]
hostA.example.com
hostB.example.com
Given the requirement that I might be run tests from outside the domain?
There is not a way to set the domain name suffix on a group all together.
OS mechanisms on short filenames (i.e. resolv.conf) will apply
Post by Mark Butler
2. Many of my roles need to call supervisor when they have finished so
---
- name: restart supervisor
service: name=supervisor state=restarted
However at the moment I have the same duplicated handler file for each
role - how can I avoid this and have a single handler file?
Yes, you can define the handler in a common role and just use it once.
It's ok if the role doesn't have a tasks file too.
Post by Mark Butler
3. Is it possible to create strings from list?
classpath: "{{ dest }}jarA.jar:{{ dest }}jarB.jar:{{ dest }}jarC.jar:{{
dest }}jarD.jar"
Yes! See the "set_fact" module in the module docs, or just define a
variable like so anywhere else in Ansible.
Variables are lazy-evaluated at the time of use.
Post by Mark Butler
In Python I could use a loop to do this. Is there any way to do this in
Ansible?
4. Do map style structures exist?
Similarly, when I am getting these jars I use a list like this - ideally
it would be better to use a map, then generate the list from the map to
avoid configuration duplication. Is there any way to achieve this?
- name: my service | Get jars
action: get_url url={{ build_url }}lastSuccessfulBuild/artifact/{{
item }} dest={{ dest }} mode=0440
- pathA/jarA
- pathB/jarB
- pathC/jarC
- pathD/jarD
So what you have above works.
What would be in your map/dictionary? You can definitely iterate across
a list of dictionaries and hopefully I can help explain further -- just
need a bit more context.
Thanks!
Post by Mark Butler
Thanks in advance,
Mark
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/58e4fa83-8ce3-41fc-a6f6-48e8c2f52f3a%40googlegroups.com
.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to a topic in the
Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/ansible-project/7N9qqACa3lI/unsubscribe
.
To unsubscribe from this group and all its topics, send an email to
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/CAEVJ8QMT3390_okm6JZmN3tqZ5gejG7n5ufM9qXwXsnBYBauZg%40mail.gmail.com
.
For more options, visit https://groups.google.com/groups/opt_out.
--
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-project+***@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/CAC_tV58e8%2B2ww_vcm_VEQPe-UwXE_PMZt4pMzOFnmRmhxDROoA%40mail.gmail.com
.
For more options, visit https://groups.google.com/groups/opt_out.
--
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-project+***@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/2691053359068513881%40unknownmsgid.
For more options, visit https://groups.google.com/groups/opt_out.
Alexander Litnitskiy
2015-07-15 11:53:51 UTC
Permalink
Regarding first question I do it myself like:

[myhosts]
hostA
hostB

group_vars/all
ansible_ssh_host: "{{inventory_hostname}}.{{domain}}"

group_vars/myhosts:
domain: example.com

In my case each group/host must define "domain" variable.
Post by Mark Butler
Hello team,
I am getting started with ansible but I have a number of questions.
1. In my inventory, is there anyway I can specify a single domain for all
the hosts e.g.
[myhosts]
hostA
hostB
rather than
[myhosts]
hostA.example.com
hostB.example.com
Given the requirement that I might be run tests from outside the domain?
2. Many of my roles need to call supervisor when they have finished so
---
- name: restart supervisor
service: name=supervisor state=restarted
However at the moment I have the same duplicated handler file for each
role - how can I avoid this and have a single handler file?
3. Is it possible to create strings from list?
classpath: "{{ dest }}jarA.jar:{{ dest }}jarB.jar:{{ dest }}jarC.jar:{{
dest }}jarD.jar"
In Python I could use a loop to do this. Is there any way to do this in
Ansible?
4. Do map style structures exist?
Similarly, when I am getting these jars I use a list like this - ideally
it would be better to use a map, then generate the list from the map to
avoid configuration duplication. Is there any way to achieve this?
- name: my service | Get jars
action: get_url url={{ build_url }}lastSuccessfulBuild/artifact/{{ item
}} dest={{ dest }} mode=0440
- pathA/jarA
- pathB/jarB
- pathC/jarC
- pathD/jarD
Thanks in advance,
Mark
--
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-project+***@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/0d296fa5-3dc9-4910-9bd3-deb0719d53d9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...