简体   繁体   中英

How to override high precedence variables in ansible?

I am trying to override inventory group_vars/all variable with play vars_prompt variable. Consider code below:

inventory/group_vars/all.yml

variable_1: test1
variable_2: test2

test.yml

- hosts: localhost
  vars_prompt:
    - name: "variable_1"
      prompt: "Enter variable_1"
      private: no

    - name: "variable_2"
      prompt: "Enter variable_2"
      private: no

- hosts: group_1
  roles:
    - { role: role_1, tags: role_1 }

- hosts: group_2
  roles:
    - { role: role_2, tags: role_2 }

roles/role_1/tasks/main.yml

- name: role_1 task
  shell: echo "{{ variable_1 }}"
  register: out

- debug:
    msg: "{{ out.stdout }}"

roles/role_2/tasks/main.yml

- name: role_2 task
  shell: echo "{{ variable_2 }}"
  register: out

- debug:
    msg: "{{ out.stdout }}"

output ansible-playbook -i inventory/hosts.test test.yml Enter variable_1: var1 Enter variable_2: var2

TASK [role_1 : role_1 task] *******************************************************************************************************************************
Monday 15 January 2018  03:42:12 -0800 (0:00:02.915)       0:00:15.048 *****
changed: [xxx.xxx.com]

TASK [role_1 : debug] *************************************************************************************************************************************
Monday 15 January 2018  03:42:13 -0800 (0:00:00.525)       0:00:15.574 *****
ok: [xxx.xxx.com] => {
    "msg": "test1"
}

PLAY [group_2] ********************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************
Monday 15 January 2018  03:42:13 -0800 (0:00:00.051)       0:00:15.625 *****
ok: [xxx.xxx.com]

TASK [role_2 : role_2 task] *******************************************************************************************************************************
Monday 15 January 2018  03:42:15 -0800 (0:00:02.178)       0:00:17.804 *****
changed: [xxx.xxx.com]

TASK [role_2 : debug] *************************************************************************************************************************************
Monday 15 January 2018  03:42:15 -0800 (0:00:00.397)       0:00:18.202 *****
ok: [xxx.xxx.com] => {
    "msg": "test2"
}

PLAY RECAP ************************************************************************************************************************************************
localhost   : ok=1    changed=0    unreachable=0    failed=0
xxx.xxx.com : ok=3    changed=1    unreachable=0    failed=0
xxx.xxx.com : ok=3    changed=1    unreachable=0    failed=0
xxx.xxx.com : ok=3    changed=1    unreachable=0    failed=0

I want to override variable_1 & variable_2 values with the var1 & var2 (Values accepted with vars_prompt) instead of test1 & test2 (Values set at inventory/group_vars/all.yml). Is there any way to override the values? or any other method to share the variables across the different playbooks.

Variables prompts are Play-bound, so your prompted variable_1=var and variable_2=var are registered only for the first (empty in your example) play, second and third plays know nothing about this.

Either apply prompts to respective plays, or make run first play against all hosts with set_fact , like this:

- hosts: all
  gather_facts: no
  vars_prompt:
    - name: "variable_1"
      prompt: "Enter variable_1"
      private: no
    - name: "variable_2"
      prompt: "Enter variable_2"
      private: no
  tasks:
    - set_fact:
        variable_1: "{{ variable_1 }}"
        variable_2: "{{ variable_2 }}"

- hosts: group_1
  roles:
    - { role: role_1, tags: role_1 }

- hosts: group_2
  roles:
    - { role: role_2, tags: role_2 }

This will define host facts variable_1 and variable_2 which are host-bound (and survive through several plays) and have higher priority than group vars from inventory.

PS if you use tags to execute subset of tasks, make sure that you have those tags for set_fact set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM