André Deseive
André Deseive

Reputation: 23

import_ansible plabook and use variables from a master playbook

I want to use in my imported playbook the varaibles which I have defined in the master playbook. Unfortunately I get the following error message:

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
}

My Playbook looks like this:

---
- 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 }}"

and the child playbook looks like this:

---
- 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

Can anyone help how to get the variables in the child playbook?

Upvotes: 2

Views: 284

Answers (2)

clockworknet
clockworknet

Reputation: 3056

I am not sure you can pass vars into the import_playbook directive like that, hence you are seeing them as undefined. Moreover, unless you have a specific reason to, using import_playbook at all, is not really a common pattern.

You can simply place the 'Clone the template' task in the same play as the one containing the vars_prompt, or if you want to split it out into a separate file, use the include statement, making the included file part of the same play:

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

Upvotes: 1

Akif
Akif

Reputation: 6786

You can use set_vact for this purpose. Variable that defined in set_fact will be available to subsequent plays during ansible-playbook execution.

Can be confirmed with following 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 }}"

Results:

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

Upvotes: 2

Related Questions