Home > Software engineering >  Overwrite vars_prompt variable in playbook with host variable from inventory in Ansible
Overwrite vars_prompt variable in playbook with host variable from inventory in Ansible

Time:11-16

I want to overwrite some variables in my playbook file from the inventory file for a host that are defined as "vars_prompt". If I understand it correctly, Ansible shouldn't prompt for the variables if they were already set before, however, it still prompts for the variables when I try to execute the playbook.

How can I overwrite the "vars_prompt" variables from the inventory or is this not possible because of the variable precedence definition of Ansible?

Example:
playbook.yml

---
- name: Install Gateway
  hosts: all
  become: yes

  vars_prompt:
    - name: "hostname"
      prompt: "Hostname"
      private: no

...

inventory.yml

---
all:
  children:
    gateways:
      hosts:
        gateway:
          ansible_host: 192.168.1.10
          ansible_user: user
          hostname: "gateway-name"
...

CodePudding user response:

Q: "If I understand it correctly, Ansible shouldn't prompt for the variables if they were already set before, however, it still prompts for the variables when I try to execute the playbook."

A: You're wrong. Ansible won't prompt for variables defined by the command line --extra-vars. Quoting from Interactive input: prompts:

Prompts for individual vars_prompt variables will be skipped for any variable that is already defined through the command line --extra-vars option, ...

You can't overwrite vars_prompt variables from the inventory. See Understanding variable precedence. Inventory variables (3.-9.) is lower precedence compared to play vars_prompt (13.). The precedence of extra vars is 22.

Use the module pause to ask for the hostname if any variable is not defined. For example, the inventory

shell> cat hosts
host_1
host_2

and the playbook

 hosts: all
  gather_facts: false

  vars:

    hostnames: "{{ ansible_play_hosts_all|
                   map('extract', hostvars, 'hostname')|
                   list }}"
    hostnames_undef: "{{ hostnames|from_yaml|
                         select('eq', 'AnsibleUndefined')|
                         length > 0 }}"

  tasks:

    - debug:
        msg: |
          hostnames: {{ hostnames }}
          hostnames_undef: {{ hostnames_undef }}
      run_once: true

    - pause:
        prompt: "Hostname"
      register: out
      when: hostnames_undef
      run_once: true

    - set_fact:
        hostname: "{{ out.user_input }}"
      when: hostname is not defined

    - debug:
        var: hostname

gives

shell> ansible-playbook pb.yml 

PLAY [all] ************************************************************************************

TASK [debug] **********************************************************************************
ok: [host_1] => 
  msg: |-
    hostnames: [AnsibleUndefined, AnsibleUndefined]
    hostnames_undef: True

TASK [pause] **********************************************************************************
[pause]
Hostname:
gw.example.com^Mok: [host_1]

TASK [set_fact] *******************************************************************************
ok: [host_1]
ok: [host_2]

TASK [debug] **********************************************************************************
ok: [host_1] => 
  hostname: gw.example.com
ok: [host_2] => 
  hostname: gw.example.com

PLAY RECAP ************************************************************************************
host_1: ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host_2: ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

The playbook won't ovewrite variables defined in the inventory. For example

shell> cat hosts
host_1
host_2 hostname=gw2.example.com

gives

TASK [debug] **********************************************************************************
ok: [host_1] => 
  hostname: gw.example.com
ok: [host_2] => 
  hostname: gw2.example.com

CodePudding user response:

I don't know if you can stop the prompts but you can se a default value directly in vars_prompts. In this way you do not need to type "gateway-name" every time.

  vars_prompt:
    - name: "hostname"
      prompt: "Hostname"
      private: no
      default: "gateway-name"

Source: https://docs.ansible.com/ansible/latest/user_guide/playbooks_prompts.html

  • Related