首页 文章

Ansible脚本模块不解释变量

提问于
浏览
0

我遇到了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

当我运行剧本时,我收到以下错误:

TASK [vsa_deploy_config : Deploy VSAs on Center] ******************************************************************************* fatal: [auto-win1.lab.com]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'item' is undefined\n\nThe error appears to have been in '/opt/ansible/roles/vsa_deploy_config/tasks/main.yml': line 10, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Deploy VSAs on Center\n ^ here\n"} to retry, use: --limit @/opt/ansible/powershell.retry

使用模板模块的第一个任务正确解释了item.vsa_hostname变量,但脚本模块没有 . 脚本模块是否无法使用with_items?

1 回答

  • 0

    您的脚本任务没有 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 .

相关问题