当在Ansible播放中使用debug模块时,有时会遇到与with_items一起使用时不起作用的问题。这通常是由于with_items迭代的数据类型不正确所致。以下是解决此问题的几种方法:
方法1:使用循环变量
- name: Example playbook
hosts: localhost
gather_facts: false
tasks:
- name: Debug with_items
debug:
msg: "{{ item }}"
with_items:
- item1
- item2
方法2:使用loop关键字
- name: Example playbook
hosts: localhost
gather_facts: false
tasks:
- name: Debug with_items
debug:
msg: "{{ item }}"
loop:
- item1
- item2
方法3:使用debug模块的变量
- name: Example playbook
hosts: localhost
gather_facts: false
tasks:
- name: Debug with_items
debug:
var: item
with_items:
- item1
- item2
请注意,使用Ansible 2.5或更高版本时,建议使用loop关键字代替with_items。loop关键字提供了更好的可读性和更多的功能。
上一篇:Ansible播放无法使用变量。