Ansible loop multiple tasks [closed]


Ansible loop multiple tasks [closed]



How to loop in ansible block which has multiple tasks? I mean can we loop items to multiple tasks without creating or using include_tasks, include module? Basically, I don't want to create multiple YAML files to do this.



Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.





Please rephrase your question, remove useless tags and provide accurate information stackoverflow.com/help/how-to-ask. Currently the question is not clear.
– baptistemm
Jul 3 at 6:42





github.com/ansible/ansible/issues/13262
– techraf
Jul 3 at 6:57




1 Answer
1



'with_items' and 'loop' is not a valid attribute for a Block. but you can achieve this task using include.



here is the example.



I have one print.yml play which prints the message:


$ cat print.yml
- name: print name
debug: msg={{ item }}

$cat test.yml
---
- hosts: 127.0.0.1
connection: local
tasks:
- include: print.yml
with_items:
- 1
- 2
- 3
- include: print.yml
with_items:
- a
- b
- c
$ansible-playbook test.yml

PLAY [127.0.0.1] **********************************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************************
ok: [localhost]

TASK [include] ************************************************************************************************************************************************************************************************
included: /home/ankur/stackoverflow/print.yml for localhost
included: /home/ankur/stackoverflow/print.yml for localhost
included: /home/ankur/stackoverflow/print.yml for localhost

TASK [print name] *********************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": 1
}

TASK [print name] *********************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": 2
}

TASK [print name] *********************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": 3
}

TASK [include] ************************************************************************************************************************************************************************************************
included: /home/ankur/stackoverflow/print.yml for localhost
included: /home/ankur/stackoverflow/print.yml for localhost
included: /home/ankur/stackoverflow/print.yml for localhost

TASK [print name] *********************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "a"
}

TASK [print name] *********************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "b"
}

TASK [print name] *********************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "c"
}

PLAY RECAP ****************************************************************************************************************************************************************************************************
localhost : ok=13 changed=0 unreachable=0 failed=0





How does this meet the following condition specified in the question without creating or using include_tasks, include module"?
– techraf
Jul 3 at 8:42






@techraf it meets the "I don't want to create multiple YAML files to do this." condition. and as per his requirement, he wants to perform set of a task in a loop and block does not support the loop and with_item attribute.
– Ankur Gupta
Jul 3 at 9:01






in this way he can fullfill the his requiremnt.
– Ankur Gupta
Jul 3 at 9:04





OP doesn't want "multiple YAML files" and you listed three in your answer: print.yml, test.yml, and loop_over_block.yml.
– techraf
Jul 3 at 9:07



print.yml


test.yml


loop_over_block.yml





apology my mistake. he needs to maintain only two files. one is set of task play (print.yml) and second is main playbook (test.yml)
– Ankur Gupta
Jul 3 at 9:14

Popular posts from this blog

JMeter fails on beanshell imports

Why in node-red my HTTP POST no receive payload from inject?

PHP contact form sending but not receiving emails