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

How to override high precedence variables in ansible?

我正在尝试用 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 }

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

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

我想用 var1 和 var2(vars_prompt 接受的值)而不是 test1 和 test2(库存设置的值/[=33)覆盖 variable_1 和 variable_2 值=].yml).有什么方法可以覆盖这些值吗?或任何其他在不同剧本之间共享变量的方法。

变量提示受播放约束,因此您的提示 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 }

这将定义 host facts variable_1variable_2 它们是主机绑定的(并通过多次播放生存)并且比组变量具有更高的优先级来自库存。

P.S。如果您使用标签来执行任务的子集,请确保您设置了 set_fact 的那些标签。