2017-01-31 33 views
0

我有playbook,其中包括。也var_prompt「name_VM」我需要在包括劇本「新vm.yml」傳遞變量,但我有錯誤:如何傳輸變量以包含劇本?

TASK [hostname] **************************************************************** fatal: [192.168.250.102]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: {{ name_VM }}: 'name_VM' is undefined\n\nThe error appears to have been in '/etc/ansible/playbooks/tasks/new-vm.yml': line 7, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n pre_tasks:\n - hostname:\n ^here\n"}

如何pre_tasks傳輸變量包括劇本?

主要劇本:

- hosts: localhost 
    gather_facts: false 
    connection: local 
    become: true 
    vars_files: 
    - ../roles/vm-create/vars/am-default.yml 
    vars_prompt: 
    - name: "name_VM" 
     prompt: "VM name:" 
     private: no 
     default: "vm001" 

    - name: "size_hard" 
     prompt: "Size hard disk (Gb)" 
     private: no 
     default: "16" 

    - name: "size_memory" 
     prompt: "Size memory (Mb)" 
     private: no 
     default: "2048" 

    - name: "count_CPU" 
     prompt: "Count CPU:" 
     private: no 
     default: "2" 

    roles: 
    - vm-create 

    tasks: 
    - include: tasks/check-ip.yml 

- include: tasks/new-vm.yml 

新虛擬劇本:

- hosts: temp 
    vars: 
     ldap_server: ldap://ldap.example.com 
     agent_server: zabbix.aexample.com 
    pre_tasks: 
    - hostname: 
     name: "{{ name_vm }}" 
    roles: 
    - { role: zabbix-agent, tags: [ 'zabbix' ] } 
    - { role: ldap-client, tags: [ 'ldap' ] } 
    - { role: motd, tags: [ 'motd' ] } 
    tasks: 
    - telegram: 
     token: 'bot12345:XXXXXX' 
     chat_id: XXXXX 
     msg: "New VM {{ ansible_hostname }} ({{ ansible_all_ipv4_addresses }}) is created and has been configured." 
     tags: 
     - telegram 

check_ip.yml我在其中添加主機:

- vsphere_guest: 
    vcenter_hostname: "{{ vcenter_hostname }}" 
    username: "{{ vcenter_user }}" 
    password: "{{ vcenter_pass }}" 
    guest: "{{ name_VM }}" 
    vmware_guest_facts: yes 
    validate_certs: no 
    register: vsphere_facts 
    until: vsphere_facts.ansible_facts.hw_eth0.ipaddresses[0] | match("192.168.250.") 
    retries: 6 
    delay: 10 
- name: Ensure virtual machine is in the dynamic inventory 
    add_host: 
    name: "{{ vsphere_facts.ansible_facts.hw_eth0.ipaddresses[0] }}" 
    ansible_user: root 
    ansible_ssh_pass: pass 
    groups: temp 

回答

2

在你的情況name_VM是發揮綁定並且在第二場比賽中不可見。

你需要一個事實分配給temp主機(我猜你使用add_hostvm-create作用的地方,所以只需添加name_vm: "{{ name_VM }}"主機其實有)。
然後在第二個玩你可以訪問{{ name_vm }}東道主的事實。

更新:示例,基於問題編輯。

- name: Ensure virtual machine is in the dynamic inventory 
    add_host: 
    name: "{{ vsphere_facts.ansible_facts.hw_eth0.ipaddresses[0] }}" 
    name_vm: "{{ name_VM }}" 
    ansible_user: root 
    ansible_ssh_pass: pass 
    groups: temp 
+0

SRY,我不知道如何在主機上添加的事實。我添加部分劇本,我添加主機,請舉例 – Nikita