Ansible with_items vs loop

倾然丶 夕夏残阳落幕 提交于 2020-01-03 11:32:21

问题


What is the difference between using with_items vs loops in ansilbe?


回答1:


Update: The most recent Documentation lists down the differences as below

  • The with_ keywords rely on Lookup Plugins - even items is a lookup.
  • The loop keyword is equivalent to with_list, and is the best choice for simple loops.
  • The loop keyword will not accept a string as input, see Ensuring list input for loop: query vs. lookup.
  • Generally speaking, any use of with_* covered in Migrating from with_X to loop can be updated to use loop.
  • Be careful when changing with_items to loop, as with_items performed implicit single-level flattening. You may need to use flatten(1) with loop to match the exact outcome.

Old answer

As per the docs,

Before 2.5 Ansible mainly used the with_ keywords to create loops, the loop keyword is basically analogous to with_list.

So basically they are pretty much the same, only the newer version uses loop in its syntax. And as of version 2.7.12 both work as expected but using the loop keyword is encouraged for future compatibility.




回答2:


They are different. Be careful when changing with_items to loop, as with_items performed implicit single-level flattening. You may need to use flatten with loop to match the exact outcome. look at the official doc examples.

Ansible Loops



来源:https://stackoverflow.com/questions/50456997/ansible-with-items-vs-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!