首页 > 解决方案 > import_ansible plabook 并使用主剧本中的变量

问题描述

我想在我导入的剧本中使用我在主剧本中定义的变量。不幸的是,我收到以下错误消息:

fatal: [localhost]: FAILED! => 
{
    "msg": "The task includes an option with an 
            undefined variable. The error was: 'vcenter_username' is undefined\n\nThe 
            error appears to have been in '/home/ansible/ansible/vcenter/vm-
            provisioning/vcenter_vm_creation.yml': line 4, 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
}

我的剧本看起来像这样:

---
- hosts: localhost
  vars_prompt:
    - name: prompt_vcenter_domain
      prompt: "Enter the Vcenter Domain Name for example vcenter"
      private: no
    - name: prompt_vcenter_username
      prompt: "Enter your Vcenter User Name"
      private: no
    - name: prompt_vcenter_password
      prompt: "Enter your Vcenter user Password"
    - name: prompt_environment_to_deploy
      prompt: "Enter the right Environment, Type Produktiv or Test VM"
      private: no
    - name: prompt_template_to_deploy
      prompt: "Enter the right Template, Template Ubuntu 18.04 LTS x64 or Template Ubuntu 18.04 LTS x64 SMALL"
      private: no
    - name: prompt_vm_hostname
      prompt: "Enter the VM Hostname"

- import_playbook: vcenter_vm_creation.yml
  vars:
    vcenter_domain: "{{ prompt_vcenter_domain }}"
    vcenter_username: "{{ prompt_vcenter_username }}"
    vcenter_password: "{{ prompt_vcenter_password }}"
    vm_hostname: "{{ prompt_vm_hostname }}"
    template_to_deploy: "{{ prompt_template_to_deploy }}"
    environment_to_deploy: "{{ prompt_environment_to_deploy }}"

子剧本看起来像这样:

---
- hosts: localhost
  tasks:
    - name: Clone the template
      vmware_guest:
        hostname: '{{ vcenter_domain }}.muc.lv1871.de'
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        validate_certs: False
        name: "{{ vm_hostname }}"
        template: "{{ template_to_deploy }}"
        folder: /{{ environment_to_deploy }}/Linux
        state: poweredon
        wait_for_ip_address: no

谁能帮助如何获取子剧本中的变量?

标签: ansible

解决方案


为此,您可以使用set_vact。在 ansible-playbook 执行期间,定义的变量set_fact将可用于后续播放。

可以通过以下剧本确认:

- hosts: localhost
  gather_facts: false
  vars_prompt:
    - name: prompt_vcenter_username
      prompt: "Enter your Vcenter User Name"
  tasks:
    - name: Set vcenter_username variable with set_fact
      set_fact:
        vcenter_username: "{{ prompt_vcenter_username }}"

- hosts: localhost
  gather_facts: false
  tasks:
    - name: Display vcenter_username
      debug:
        msg: "{{ vcenter_username }}"

结果:

Enter your Vcenter User Name: 

PLAY [localhost] *******************************************************************************

TASK [Set vcenter_username variable with set_fact] *********************************************
ok: [localhost]

PLAY [localhost] *******************************************************************************

TASK [Display vcenter_username] ****************************************************************
ok: [localhost] => {
    "msg": "test_user"
}

PLAY RECAP *************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0

推荐阅读