我想在导入的剧本中使用我在主剧本中定义的变量。不幸的是,我收到以下错误消息:
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
任何人都可以帮助获得儿童剧本中的变量吗?
答案 0 :(得分:1)
您可以为此使用set_vact。在set_fact
中定义的变量将在ansible-playbook执行期间供后续播放使用。
可以通过以下剧本进行确认:
- 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
答案 1 :(得分:1)
我不确定是否可以像这样将vars传递给import_playbook指令,因此您将它们视为未定义。而且,除非有特殊原因,否则根本不使用import_playbook并不是一种常见的模式。
您可以简单地将“克隆模板”任务与包含vars_prompt
的任务放在同一剧本中,或者,如果您想将其拆分成单独的文件,请使用include
语句,使包含的文件成为同一播放的一部分:
playbook.yml
---
- hosts: localhost
vars_prompt:
- name: vcenter_domain
prompt: "Enter the Vcenter Domain Name for example vcenter"
private: no
- name: vcenter_username
prompt: "Enter your Vcenter User Name"
private: no
- name: vcenter_password
prompt: "Enter your Vcenter user Password"
- name: environment_to_deploy
prompt: "Enter the right Environment, Type Produktiv or Test VM"
private: no
- name: 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: vm_hostname
prompt: "Enter the VM Hostname"
tasks:
- include: template_clone.yml
template_clone.yml
---
- 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