Ansible nested loops
I have a variable that looks like this:
device:
- sdb: 2
- sdc: 3
- sdd: 4
How can I produce the result with ansible loops:
sdb 1
sdb 2
sdc 1
sdc 2
sdc 3
sdd 1
sdd 2
sdd 3
sdd 4
I tried with_sequence
and loop_control
but it didn't work.
loops ansible
add a comment |
I have a variable that looks like this:
device:
- sdb: 2
- sdc: 3
- sdd: 4
How can I produce the result with ansible loops:
sdb 1
sdb 2
sdc 1
sdc 2
sdc 3
sdd 1
sdd 2
sdd 3
sdd 4
I tried with_sequence
and loop_control
but it didn't work.
loops ansible
add a comment |
I have a variable that looks like this:
device:
- sdb: 2
- sdc: 3
- sdd: 4
How can I produce the result with ansible loops:
sdb 1
sdb 2
sdc 1
sdc 2
sdc 3
sdd 1
sdd 2
sdd 3
sdd 4
I tried with_sequence
and loop_control
but it didn't work.
loops ansible
I have a variable that looks like this:
device:
- sdb: 2
- sdc: 3
- sdd: 4
How can I produce the result with ansible loops:
sdb 1
sdb 2
sdc 1
sdc 2
sdc 3
sdd 1
sdd 2
sdd 3
sdd 4
I tried with_sequence
and loop_control
but it didn't work.
loops ansible
loops ansible
asked Nov 21 '18 at 2:35
Cheng ShuaiCheng Shuai
33
33
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Wrote a debug task with ansible loop and jinja which should give you the desired result. Refactor as required.
- name: Debug device var
debug:
msg: "{% for key, value in item.iteritems() %}{% for i in range(value) %} {{ key }} {{ loop.index }} {% endfor %}{% endfor %}"
loop: "{{ device }}"
Actually, I need use the result in the command. So how could I get the result without jinjia?
– Cheng Shuai
Nov 21 '18 at 5:29
Yeah that's the correct output, isn't it ?
– Anant Naugai
Nov 21 '18 at 5:32
The output should be "sdc 1" "sdc 2" "sdc 3" instead of " sdc 1 sdc 2 sdc 3 " in each command line.
– Cheng Shuai
Nov 21 '18 at 5:40
If you just need quotes around the values then use this (quotes need to be escaped for debug msg): "{% for key, value in item.iteritems() %}{% for i in range(value) %}"{{ key }} {{ loop.index }}" {% endfor %}{% endfor %}"
– Anant Naugai
Nov 21 '18 at 5:47
I mean I need separated lines in each command, not just need the quotes. Maybe jinja isn't suitable for the case.
– Cheng Shuai
Nov 21 '18 at 6:13
add a comment |
Finally, I got the solution but I changed the format of the variable, however. The variable is changed to:
device:
sdb: 2
sdc: 3
sdd: 4
The result is:
ok: [ceph-host-2] => (item=1) => {
"msg": "sdd 1"
}
ok: [ceph-host-2] => (item=2) => {
"msg": "sdd 2"
}
ok: [ceph-host-2] => (item=3) => {
"msg": "sdd 3"
}
ok: [ceph-host-2] => (item=4) => {
"msg": "sdd 4"
}
TASK [osd : debug]
ok: [ceph-host-2] => (item=1) => {
"msg": "sdb 1"
}
ok: [ceph-host-2] => (item=2) => {
"msg": "sdb 2"
}
TASK [osd : debug]
ok: [ceph-host-2] => (item=1) => {
"msg": "sdc 1"
}
ok: [ceph-host-2] => (item=2) => {
"msg": "sdc 2"
}
ok: [ceph-host-2] => (item=3) => {
"msg": "sdc 3"
}
main.yml
- include_tasks: inner.yml
loop: "{{ device.keys() }}"
loop_control:
loop_var: outer_item
inner.yml
- debug:
msg: "{{ outer_item }} {{ item }}"
with_sequence: count={{ device[outer_item] }}
add a comment |
The variable is:
device:
- sdb: 2
- sdc: 3
- sdd: 4
main.yml
- include_tasks: inner.yml
loop: "{{ device }}"
loop_control:
loop_var: outer_item
inner.yml
- debug:
msg: "{{ outer_item.keys()[0] }} {{ item }}"
with_sequence: count={{ outer_item.values()[0] }}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53404545%2fansible-nested-loops%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Wrote a debug task with ansible loop and jinja which should give you the desired result. Refactor as required.
- name: Debug device var
debug:
msg: "{% for key, value in item.iteritems() %}{% for i in range(value) %} {{ key }} {{ loop.index }} {% endfor %}{% endfor %}"
loop: "{{ device }}"
Actually, I need use the result in the command. So how could I get the result without jinjia?
– Cheng Shuai
Nov 21 '18 at 5:29
Yeah that's the correct output, isn't it ?
– Anant Naugai
Nov 21 '18 at 5:32
The output should be "sdc 1" "sdc 2" "sdc 3" instead of " sdc 1 sdc 2 sdc 3 " in each command line.
– Cheng Shuai
Nov 21 '18 at 5:40
If you just need quotes around the values then use this (quotes need to be escaped for debug msg): "{% for key, value in item.iteritems() %}{% for i in range(value) %}"{{ key }} {{ loop.index }}" {% endfor %}{% endfor %}"
– Anant Naugai
Nov 21 '18 at 5:47
I mean I need separated lines in each command, not just need the quotes. Maybe jinja isn't suitable for the case.
– Cheng Shuai
Nov 21 '18 at 6:13
add a comment |
Wrote a debug task with ansible loop and jinja which should give you the desired result. Refactor as required.
- name: Debug device var
debug:
msg: "{% for key, value in item.iteritems() %}{% for i in range(value) %} {{ key }} {{ loop.index }} {% endfor %}{% endfor %}"
loop: "{{ device }}"
Actually, I need use the result in the command. So how could I get the result without jinjia?
– Cheng Shuai
Nov 21 '18 at 5:29
Yeah that's the correct output, isn't it ?
– Anant Naugai
Nov 21 '18 at 5:32
The output should be "sdc 1" "sdc 2" "sdc 3" instead of " sdc 1 sdc 2 sdc 3 " in each command line.
– Cheng Shuai
Nov 21 '18 at 5:40
If you just need quotes around the values then use this (quotes need to be escaped for debug msg): "{% for key, value in item.iteritems() %}{% for i in range(value) %}"{{ key }} {{ loop.index }}" {% endfor %}{% endfor %}"
– Anant Naugai
Nov 21 '18 at 5:47
I mean I need separated lines in each command, not just need the quotes. Maybe jinja isn't suitable for the case.
– Cheng Shuai
Nov 21 '18 at 6:13
add a comment |
Wrote a debug task with ansible loop and jinja which should give you the desired result. Refactor as required.
- name: Debug device var
debug:
msg: "{% for key, value in item.iteritems() %}{% for i in range(value) %} {{ key }} {{ loop.index }} {% endfor %}{% endfor %}"
loop: "{{ device }}"
Wrote a debug task with ansible loop and jinja which should give you the desired result. Refactor as required.
- name: Debug device var
debug:
msg: "{% for key, value in item.iteritems() %}{% for i in range(value) %} {{ key }} {{ loop.index }} {% endfor %}{% endfor %}"
loop: "{{ device }}"
answered Nov 21 '18 at 3:21
Anant NaugaiAnant Naugai
19029
19029
Actually, I need use the result in the command. So how could I get the result without jinjia?
– Cheng Shuai
Nov 21 '18 at 5:29
Yeah that's the correct output, isn't it ?
– Anant Naugai
Nov 21 '18 at 5:32
The output should be "sdc 1" "sdc 2" "sdc 3" instead of " sdc 1 sdc 2 sdc 3 " in each command line.
– Cheng Shuai
Nov 21 '18 at 5:40
If you just need quotes around the values then use this (quotes need to be escaped for debug msg): "{% for key, value in item.iteritems() %}{% for i in range(value) %}"{{ key }} {{ loop.index }}" {% endfor %}{% endfor %}"
– Anant Naugai
Nov 21 '18 at 5:47
I mean I need separated lines in each command, not just need the quotes. Maybe jinja isn't suitable for the case.
– Cheng Shuai
Nov 21 '18 at 6:13
add a comment |
Actually, I need use the result in the command. So how could I get the result without jinjia?
– Cheng Shuai
Nov 21 '18 at 5:29
Yeah that's the correct output, isn't it ?
– Anant Naugai
Nov 21 '18 at 5:32
The output should be "sdc 1" "sdc 2" "sdc 3" instead of " sdc 1 sdc 2 sdc 3 " in each command line.
– Cheng Shuai
Nov 21 '18 at 5:40
If you just need quotes around the values then use this (quotes need to be escaped for debug msg): "{% for key, value in item.iteritems() %}{% for i in range(value) %}"{{ key }} {{ loop.index }}" {% endfor %}{% endfor %}"
– Anant Naugai
Nov 21 '18 at 5:47
I mean I need separated lines in each command, not just need the quotes. Maybe jinja isn't suitable for the case.
– Cheng Shuai
Nov 21 '18 at 6:13
Actually, I need use the result in the command. So how could I get the result without jinjia?
– Cheng Shuai
Nov 21 '18 at 5:29
Actually, I need use the result in the command. So how could I get the result without jinjia?
– Cheng Shuai
Nov 21 '18 at 5:29
Yeah that's the correct output, isn't it ?
– Anant Naugai
Nov 21 '18 at 5:32
Yeah that's the correct output, isn't it ?
– Anant Naugai
Nov 21 '18 at 5:32
The output should be "sdc 1" "sdc 2" "sdc 3" instead of " sdc 1 sdc 2 sdc 3 " in each command line.
– Cheng Shuai
Nov 21 '18 at 5:40
The output should be "sdc 1" "sdc 2" "sdc 3" instead of " sdc 1 sdc 2 sdc 3 " in each command line.
– Cheng Shuai
Nov 21 '18 at 5:40
If you just need quotes around the values then use this (quotes need to be escaped for debug msg): "{% for key, value in item.iteritems() %}{% for i in range(value) %}"{{ key }} {{ loop.index }}" {% endfor %}{% endfor %}"
– Anant Naugai
Nov 21 '18 at 5:47
If you just need quotes around the values then use this (quotes need to be escaped for debug msg): "{% for key, value in item.iteritems() %}{% for i in range(value) %}"{{ key }} {{ loop.index }}" {% endfor %}{% endfor %}"
– Anant Naugai
Nov 21 '18 at 5:47
I mean I need separated lines in each command, not just need the quotes. Maybe jinja isn't suitable for the case.
– Cheng Shuai
Nov 21 '18 at 6:13
I mean I need separated lines in each command, not just need the quotes. Maybe jinja isn't suitable for the case.
– Cheng Shuai
Nov 21 '18 at 6:13
add a comment |
Finally, I got the solution but I changed the format of the variable, however. The variable is changed to:
device:
sdb: 2
sdc: 3
sdd: 4
The result is:
ok: [ceph-host-2] => (item=1) => {
"msg": "sdd 1"
}
ok: [ceph-host-2] => (item=2) => {
"msg": "sdd 2"
}
ok: [ceph-host-2] => (item=3) => {
"msg": "sdd 3"
}
ok: [ceph-host-2] => (item=4) => {
"msg": "sdd 4"
}
TASK [osd : debug]
ok: [ceph-host-2] => (item=1) => {
"msg": "sdb 1"
}
ok: [ceph-host-2] => (item=2) => {
"msg": "sdb 2"
}
TASK [osd : debug]
ok: [ceph-host-2] => (item=1) => {
"msg": "sdc 1"
}
ok: [ceph-host-2] => (item=2) => {
"msg": "sdc 2"
}
ok: [ceph-host-2] => (item=3) => {
"msg": "sdc 3"
}
main.yml
- include_tasks: inner.yml
loop: "{{ device.keys() }}"
loop_control:
loop_var: outer_item
inner.yml
- debug:
msg: "{{ outer_item }} {{ item }}"
with_sequence: count={{ device[outer_item] }}
add a comment |
Finally, I got the solution but I changed the format of the variable, however. The variable is changed to:
device:
sdb: 2
sdc: 3
sdd: 4
The result is:
ok: [ceph-host-2] => (item=1) => {
"msg": "sdd 1"
}
ok: [ceph-host-2] => (item=2) => {
"msg": "sdd 2"
}
ok: [ceph-host-2] => (item=3) => {
"msg": "sdd 3"
}
ok: [ceph-host-2] => (item=4) => {
"msg": "sdd 4"
}
TASK [osd : debug]
ok: [ceph-host-2] => (item=1) => {
"msg": "sdb 1"
}
ok: [ceph-host-2] => (item=2) => {
"msg": "sdb 2"
}
TASK [osd : debug]
ok: [ceph-host-2] => (item=1) => {
"msg": "sdc 1"
}
ok: [ceph-host-2] => (item=2) => {
"msg": "sdc 2"
}
ok: [ceph-host-2] => (item=3) => {
"msg": "sdc 3"
}
main.yml
- include_tasks: inner.yml
loop: "{{ device.keys() }}"
loop_control:
loop_var: outer_item
inner.yml
- debug:
msg: "{{ outer_item }} {{ item }}"
with_sequence: count={{ device[outer_item] }}
add a comment |
Finally, I got the solution but I changed the format of the variable, however. The variable is changed to:
device:
sdb: 2
sdc: 3
sdd: 4
The result is:
ok: [ceph-host-2] => (item=1) => {
"msg": "sdd 1"
}
ok: [ceph-host-2] => (item=2) => {
"msg": "sdd 2"
}
ok: [ceph-host-2] => (item=3) => {
"msg": "sdd 3"
}
ok: [ceph-host-2] => (item=4) => {
"msg": "sdd 4"
}
TASK [osd : debug]
ok: [ceph-host-2] => (item=1) => {
"msg": "sdb 1"
}
ok: [ceph-host-2] => (item=2) => {
"msg": "sdb 2"
}
TASK [osd : debug]
ok: [ceph-host-2] => (item=1) => {
"msg": "sdc 1"
}
ok: [ceph-host-2] => (item=2) => {
"msg": "sdc 2"
}
ok: [ceph-host-2] => (item=3) => {
"msg": "sdc 3"
}
main.yml
- include_tasks: inner.yml
loop: "{{ device.keys() }}"
loop_control:
loop_var: outer_item
inner.yml
- debug:
msg: "{{ outer_item }} {{ item }}"
with_sequence: count={{ device[outer_item] }}
Finally, I got the solution but I changed the format of the variable, however. The variable is changed to:
device:
sdb: 2
sdc: 3
sdd: 4
The result is:
ok: [ceph-host-2] => (item=1) => {
"msg": "sdd 1"
}
ok: [ceph-host-2] => (item=2) => {
"msg": "sdd 2"
}
ok: [ceph-host-2] => (item=3) => {
"msg": "sdd 3"
}
ok: [ceph-host-2] => (item=4) => {
"msg": "sdd 4"
}
TASK [osd : debug]
ok: [ceph-host-2] => (item=1) => {
"msg": "sdb 1"
}
ok: [ceph-host-2] => (item=2) => {
"msg": "sdb 2"
}
TASK [osd : debug]
ok: [ceph-host-2] => (item=1) => {
"msg": "sdc 1"
}
ok: [ceph-host-2] => (item=2) => {
"msg": "sdc 2"
}
ok: [ceph-host-2] => (item=3) => {
"msg": "sdc 3"
}
main.yml
- include_tasks: inner.yml
loop: "{{ device.keys() }}"
loop_control:
loop_var: outer_item
inner.yml
- debug:
msg: "{{ outer_item }} {{ item }}"
with_sequence: count={{ device[outer_item] }}
answered Nov 21 '18 at 8:49
Cheng ShuaiCheng Shuai
33
33
add a comment |
add a comment |
The variable is:
device:
- sdb: 2
- sdc: 3
- sdd: 4
main.yml
- include_tasks: inner.yml
loop: "{{ device }}"
loop_control:
loop_var: outer_item
inner.yml
- debug:
msg: "{{ outer_item.keys()[0] }} {{ item }}"
with_sequence: count={{ outer_item.values()[0] }}
add a comment |
The variable is:
device:
- sdb: 2
- sdc: 3
- sdd: 4
main.yml
- include_tasks: inner.yml
loop: "{{ device }}"
loop_control:
loop_var: outer_item
inner.yml
- debug:
msg: "{{ outer_item.keys()[0] }} {{ item }}"
with_sequence: count={{ outer_item.values()[0] }}
add a comment |
The variable is:
device:
- sdb: 2
- sdc: 3
- sdd: 4
main.yml
- include_tasks: inner.yml
loop: "{{ device }}"
loop_control:
loop_var: outer_item
inner.yml
- debug:
msg: "{{ outer_item.keys()[0] }} {{ item }}"
with_sequence: count={{ outer_item.values()[0] }}
The variable is:
device:
- sdb: 2
- sdc: 3
- sdd: 4
main.yml
- include_tasks: inner.yml
loop: "{{ device }}"
loop_control:
loop_var: outer_item
inner.yml
- debug:
msg: "{{ outer_item.keys()[0] }} {{ item }}"
with_sequence: count={{ outer_item.values()[0] }}
answered Nov 21 '18 at 11:52
Cheng ShuaiCheng Shuai
33
33
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.
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%2f53404545%2fansible-nested-loops%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