【问题标题】:Ansible Script Module Not Interpreting VariableAnsible 脚本模块不解释变量
【发布时间】:2018-01-18 04:45:34
【问题描述】:

我遇到了 Ansible 脚本模块解释 with_items 变量的问题。

vsa_deploy_config/tasks/main.yml:

- name: Create VSA scripts for center
  template:
   src: vsa_deploy.ps1.j2
   dest: "/opt/ansible/roles/vsa_deploy_config/files/{{ item.vsa_hostname }}.ps1"
  when: target == "local"
  with_items:
  - "{{ vsa_center }}"

- name: Deploy VSAs on Center
  script: "files/{{ item.vsa_hostname }}.ps1"
  register: out
  when: target == "win_center"
- debug: var=out
  with_items:
  - "{{ vsa_center }}"

vsa_deploy_config/vars/main.yml:

---
vsa_center:
    - vcsa_hostname: 10.10.10.74
      vcsa_username: administrator@vsphere.local
      vcsa_password: password
      vcsa_datacenter: DataCenter1
      vsa_rdm_lun: 02000000006006bf1d58d25a1020d292f8fcfb22b3554353432d4d
      vsa_hostname: sm01-ct01
      vsa_mgmt_ip: 10.10.10.75
      vsa_mgmt_netmask: 255.255.255.192
      vsa_mgmt_gw: 10.10.10.65
      vsa_mgmt_ns: 10.10.10.92
      vsa_mgmt_pg: SC-MGMT
      vsa_mgmt_moref: Network:network-13
      vsa_iscsi_ip: 192.168.2.1
      vsa_iscsi_netmask: 255.255.255.0
      vsa_iscsi_pg: ISCSI
      vsa_iscsi_moref: Network:network-22
      vsa_mirror_ip: 192.168.5.1
      vsa_mirror_netmask: 255.255.255.0
      vsa_mirror_pg: Mirror
      vsa_mirror_moref: Network:network-23
      esxi_hostname: 10.10.10.72
      esxi_datastore: DS-01
    - vcsa_hostname: 10.10.10.74
      vcsa_username: administrator@vsphere.local
      vcsa_password: password
      vcsa_datacenter: DataCenter1
      vsa_rdm_lun: 02000000006006bf1d58d25dd0210bb356a78344e5554353432d4d 
      vsa_hostname: sm02-ct01
      vsa_mgmt_ip: 10.10.10.76
      vsa_mgmt_netmask: 255.255.255.192
      vsa_mgmt_gw: 10.10.10.65
      vsa_mgmt_ns: 10.10.10.92
      vsa_mgmt_pg: SC-MGMT
      vsa_mgmt_moref: Network:network-13
      vsa_iscsi_ip: 192.168.2.2
      vsa_iscsi_netmask: 255.255.255.0
      vsa_iscsi_pg: ISCSI
      vsa_iscsi_moref: Network:network-22
      vsa_mirror_ip: 192.168.5.2
      vsa_mirror_netmask: 255.255.255.0
      vsa_mirror_pg: Mirror
      vsa_mirror_moref: Network:network-23
      esxi_hostname: 10.2.120.73
      esxi_datastore: DS-02

当我运行 playbook 时出现以下错误:

任务 [vsa_deploy_config : 在中心部署 VSA] *********************************** ************************************************ 致命:[auto-win1.lab.com]:失败! => {"failed": true, "msg": "字段 'args' 的值无效,似乎包含未定义的变量。错误是:'item' 未定义\n\n错误似乎是已在“/opt/ansible/roles/vsa_deploy_config/tasks/main.yml”中:第 10 行,第 3 列,但可能\n位于文件中的其他位置,具体取决于确切的语法问题。\n\n违规行似乎是:\n\n\n- 名称:在中心部署 VSA\n ^ 此处\n"} 要重试,请使用:--limit @/opt/ansible/powershell.retry

使用模板模块的第一个任务正确解释了 item.vsa_hostname 变量,但脚本模块没有。脚本模块不能使用 with_items 吗?

【问题讨论】:

    标签: ansible ansible-2.x


    【解决方案1】:

    您的脚本任务没有with_items

    - name: Deploy VSAs on Center                    # -\
      script: "files/{{ item.vsa_hostname }}.ps1"    #   \
      register: out                                  #   / This is task1
      when: target == "win_center"                   # -/
    - debug: var=out                                 # -\
      with_items:                                    #   > This is task2
      - "{{ vsa_center }}"                           # -/
    

    我猜你想把调试移到最底层:

    - name: Deploy VSAs on Center
      script: "files/{{ item.vsa_hostname }}.ps1"
      register: out
      when: target == "win_center"
      with_items: "{{ vsa_center }}"
    
    - debug: var=out
    

    附:也不需要将不必要的嵌套列表提供给with_items

    【讨论】:

      【解决方案2】:

      只需将行 - debug: var=out 移动到文件末尾即可

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-28
        • 1970-01-01
        • 2020-01-24
        • 2020-12-21
        • 1970-01-01
        • 1970-01-01
        • 2022-11-30
        • 2022-11-07
        相关资源
        最近更新 更多