Ansible dictionary key as variable
up vote
1
down vote
favorite
Let's have something like this in role defaults/main.yml:
num: 0
config:
0:
a: true
b: 'x'
1:
a: false
b: 'y'
2:
a: false
b: 'z'
Now I send -e num=1 in playbook call, and I want to use values a and b based on this value somewhere else in the role, something like:
aValue: '{{config[num].a}}'
bValue: '{{config[num].b}}'
How do I do that? I tried
aValue: '{{config[num].a}}'
but got an error: 'dict object' has no attribute u'1'
aValue: '{{config["num"].a}}'
but got an error: 'dict object' has no attribute 'num'
automation continuous-integration ansible jinja2
add a comment |
up vote
1
down vote
favorite
Let's have something like this in role defaults/main.yml:
num: 0
config:
0:
a: true
b: 'x'
1:
a: false
b: 'y'
2:
a: false
b: 'z'
Now I send -e num=1 in playbook call, and I want to use values a and b based on this value somewhere else in the role, something like:
aValue: '{{config[num].a}}'
bValue: '{{config[num].b}}'
How do I do that? I tried
aValue: '{{config[num].a}}'
but got an error: 'dict object' has no attribute u'1'
aValue: '{{config["num"].a}}'
but got an error: 'dict object' has no attribute 'num'
automation continuous-integration ansible jinja2
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Let's have something like this in role defaults/main.yml:
num: 0
config:
0:
a: true
b: 'x'
1:
a: false
b: 'y'
2:
a: false
b: 'z'
Now I send -e num=1 in playbook call, and I want to use values a and b based on this value somewhere else in the role, something like:
aValue: '{{config[num].a}}'
bValue: '{{config[num].b}}'
How do I do that? I tried
aValue: '{{config[num].a}}'
but got an error: 'dict object' has no attribute u'1'
aValue: '{{config["num"].a}}'
but got an error: 'dict object' has no attribute 'num'
automation continuous-integration ansible jinja2
Let's have something like this in role defaults/main.yml:
num: 0
config:
0:
a: true
b: 'x'
1:
a: false
b: 'y'
2:
a: false
b: 'z'
Now I send -e num=1 in playbook call, and I want to use values a and b based on this value somewhere else in the role, something like:
aValue: '{{config[num].a}}'
bValue: '{{config[num].b}}'
How do I do that? I tried
aValue: '{{config[num].a}}'
but got an error: 'dict object' has no attribute u'1'
aValue: '{{config["num"].a}}'
but got an error: 'dict object' has no attribute 'num'
automation continuous-integration ansible jinja2
automation continuous-integration ansible jinja2
edited Nov 14 at 7:53
asked Nov 14 at 6:38
lubik
83
83
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
If you quote those config keys, they will become strings:
config:
"0":
a: true
Or, if you have the rest of your playbook that really does want them to be numbers, you can make num actually be a number in two ways:
ansible -e '{"num": 1}' to cause ansible to parse --extra-vars as JSON, where "num" really will be a Number (in the JSON sense)
or coerce num in the jinja2 expression:
aValue: '{{ config[ (num|int) ].a }}'
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
If you quote those config keys, they will become strings:
config:
"0":
a: true
Or, if you have the rest of your playbook that really does want them to be numbers, you can make num actually be a number in two ways:
ansible -e '{"num": 1}' to cause ansible to parse --extra-vars as JSON, where "num" really will be a Number (in the JSON sense)
or coerce num in the jinja2 expression:
aValue: '{{ config[ (num|int) ].a }}'
add a comment |
up vote
2
down vote
accepted
If you quote those config keys, they will become strings:
config:
"0":
a: true
Or, if you have the rest of your playbook that really does want them to be numbers, you can make num actually be a number in two ways:
ansible -e '{"num": 1}' to cause ansible to parse --extra-vars as JSON, where "num" really will be a Number (in the JSON sense)
or coerce num in the jinja2 expression:
aValue: '{{ config[ (num|int) ].a }}'
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
If you quote those config keys, they will become strings:
config:
"0":
a: true
Or, if you have the rest of your playbook that really does want them to be numbers, you can make num actually be a number in two ways:
ansible -e '{"num": 1}' to cause ansible to parse --extra-vars as JSON, where "num" really will be a Number (in the JSON sense)
or coerce num in the jinja2 expression:
aValue: '{{ config[ (num|int) ].a }}'
If you quote those config keys, they will become strings:
config:
"0":
a: true
Or, if you have the rest of your playbook that really does want them to be numbers, you can make num actually be a number in two ways:
ansible -e '{"num": 1}' to cause ansible to parse --extra-vars as JSON, where "num" really will be a Number (in the JSON sense)
or coerce num in the jinja2 expression:
aValue: '{{ config[ (num|int) ].a }}'
answered Nov 14 at 7:59
Matthew L Daniel
6,82112326
6,82112326
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53294414%2fansible-dictionary-key-as-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown