【发布时间】:2021-03-24 11:07:14
【问题描述】:
我正在尝试执行以下 ansible 脚本,但出现错误。
文件 test.sh 有语句“echo $1”。
---
- name: Executing Ansible Playbook
hosts: localhost
become: yes
become_user: testuser
pre_tasks:
- include_vars: global_vars.yaml
- name: Print some debug information
set_fact:
all_vars: |
Content of vars
--------------------------------
{{ vars | to_nice_json }}
tasks:
- name: Iterate over an array
command: sh test.sh -i {{ item }}
register: sh
- debug:
msg: "{{ sh.stdout }}"
with_items: "{{ array }}"
数组包含以下值
array: ["array_item1", "array_item2","array_item3","array_item4","array_item5"]
错误:
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to be in '/mnt/c/ansible-test/first.yaml': line 16, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: Iterate over an array\n ^ here\n"}
但是,如果我删除该行
- debug:
msg: "{{ sh.stdout }}"
然后ansible脚本通过并给出输出
TASK [Iterate over an array] **************************************************************************************************************************************************************************************
changed: [localhost] => (item=array_item1)
changed: [localhost] => (item=array_item2)
changed: [localhost] => (item=array_item3)
changed: [localhost] => (item=array_item4)
changed: [localhost] => (item=array_item5)
如何使用调试或确保脚本以正确的项目值执行。
【问题讨论】:
-
如您所见,您的
command任务应该有with_items,而不是debug。 -
@seshadri_c 工作得非常好......但是你能回答我在你的回答中提出的疑问吗?