如何传输变量以包含剧本?
How to transfer variables to include playbook?
我有剧本,里面有。还有 var_prompt "name_VM" 并且我需要在 include 剧本 "new-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 include playbook 中传递变量?
主要剧本:
- 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
在你的情况下 name_VM
是 play-bound 并且在第二次播放时将不可见。
您需要为 temp
主机分配一个事实(我猜您在 vm-create
角色中的某处使用了 add_host
;因此只需在其中添加 name_vm: "{{ name_VM }}"
主机事实)。
然后在第二个游戏中你可以访问 {{ name_vm }}
host fact.
更新:示例,基于问题编辑。
- 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
我有剧本,里面有。还有 var_prompt "name_VM" 并且我需要在 include 剧本 "new-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 include playbook 中传递变量?
主要剧本:
- 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
在你的情况下 name_VM
是 play-bound 并且在第二次播放时将不可见。
您需要为 temp
主机分配一个事实(我猜您在 vm-create
角色中的某处使用了 add_host
;因此只需在其中添加 name_vm: "{{ name_VM }}"
主机事实)。
然后在第二个游戏中你可以访问 {{ name_vm }}
host fact.
更新:示例,基于问题编辑。
- 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