如何在ansible中覆盖高优先级变量?

时间:2018-02-15 11:58:29

标签: ansible global-variables ansible-2.x

我正在尝试使用 play vars_prompt 变量覆盖库存 group_vars / all 变量。请考虑以下代码:

库存/ 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 }

角色/ role_1 /任务/ main.yml

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

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

角色/ role_2 /任务/ main.yml

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

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

输出     ansible-playbook -i inventory / hosts.test test.yml     输入variable_1:var1     输入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

我想覆盖variable_1&变量_值与var1& var2(用vars_prompt接受的值)而不是test1& test2(在inventory / group_vars / all.yml设置的值)。有没有办法覆盖这些值?或者在不同的剧本中共享变量的任何其他方法。

1 个答案:

答案 0 :(得分:1)

变量提示是Play限制的,因此提示variable_1=varvariable_2=var仅针对第一个(示例中为空)播放,第二个和第三个播放只对此有所了解。

将提示应用于相应的游戏,或者首先使用set_fact对所有主机进行游戏,如下所示:

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

这将定义主机绑定的主机事实 variable_1variable_2(并通过多次播放生存)并且优先于广告资源中的组变量。

P.S。如果您使用标记来执行任务子集,请确保您拥有set_fact设置的标记。