Ansible脚本模块不解释变量

时间:2017-08-10 05:31:00

标签: ansible ansible-2.x

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

vsa_deploy_config /任务/ 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 /瓦尔/ 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

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

任务[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-名称:在Center上部署VSA \ n ^ here \ n“}     要重试,请使用: - limit @ / opt / ansible / powershell.retry

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

2 个答案:

答案 0 :(得分: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

P.S。也没有必要将不必要的嵌套列表提供给with_items

答案 1 :(得分:0)

只需将行- debug: var=out移动到文件的末尾,它就会工作