我想我终于理解了问题。下面是我的原始答案,如果跳过一个角色仍然可以使用。
这是我基本上理解你要做的事情:
- 根据一组条件确定需要使用的主机
- 根据上述选择的主机来运行一个或另一个play
- 最后无条件执行其他任务
以下是一个非常基本但完全功能的示例,以满足您的要求。我将其简化为最小任务集,使其适合于提问。但是,您可以使用相同的概念添加回您的角色,并注入真实任务以选择目标。
剧本中的关键是使用add_host
模块将所需的主机推送到内存库存中的所需组中。对于此示例,任何主机都将使用“local”连接,以便它伪造一个主机,但仍然使用localhost运行。请注意,您可以很好地将几个主机添加到几个组中。
我还添加了一个基本检查,以确保我们通过虚拟主机选择传递的值在预期值的集合内。默认情况下,playbook使用主机B,但是您可以通过在命令行上传递-e host_we_need=C
来更改它。
足够的交谈:看示例吧!为了避免一些警告,让我们创建一个包含我们需要的两个组但为空的虚拟inventory.yml
:
---
group_B:
group_C:
然后是playbook:
---
- name: Determine the hosts we need
hosts: localhost
gather_facts: false
tasks:
- name: dummy task to fake you choice
command: "echo {{ host_we_need | default('B') }}"
register: host_choice
- name: check condition to go on
vars:
allowed_hosts:
- B
- C
assert:
that:
- host_choice.stdout in allowed_hosts
fail_msg:
- "Found host value is: {{ host_choice.stdout }}"
- "It should be one of: {{ allowed_hosts | join(', ') }}"
- name: add the needed host to in-memory inventory
add_host:
name: "{{ host_choice.stdout }}"
groups:
- "group_{{ host_choice.stdout }}"
ansible_connection: local
- name: play tasks for group_B
hosts: group_B
gather_facts: false
tasks:
- debug:
msg: This a task for group_B
- name: play tasks for group_C
hosts: group_C
gather_facts: false
tasks:
- debug:
msg: This is a task for group_C
- name: whatever leftover tasks on localhost
hosts: localhost
gather_facts: false
tasks:
- debug:
msg: leftover localhost tasks
提供(每种情况的示例运行)
$ ansible-playbook -i add inventory.yml playbook.yml
PLAY [Determine the hosts we need] **********************************
TASK [dummy task to fake you choice] ********************************
changed: [localhost]
TASK [check condition to go on] *************************************
ok: [localhost]
TASK [add the needed host to in-memory inventory] *******************
changed: [localhost]
PLAY [play tasks for group_B] ***************************************
TASK [debug] ********************************************************
ok: [B] => {
"msg": "This a task for group_B"
}
PLAY [play tasks for group_C] ***************************************
skipping: no hosts matched
PLAY [whatever leftover tasks on localhost] *************************
TASK [debug] ********************************************************
ok: [localhost] => {
"msg": "leftover localhost tasks"
}
PLAY RECAP **********************************************************
B : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
localhost : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$ ansible-playbook -i inventory.yml playbook.yml -e host_we_need=C
PLAY [Determine the hosts we need] **********************************
TASK [dummy task to fake you choice] ********************************
changed: [localhost]
TASK [check condition to go on] *************************************
ok: [localhost]
TASK [add the needed host to in-memory inventory] *******************
changed: [localhost]
PLAY [play tasks for group_B] ***************************************
skipping: no hosts matched
PLAY [play tasks for group_C] ***************************************
TASK [debug] ********************************************************
ok: [C] => {
"msg": "This is a task for group_C"
}
PLAY [whatever leftover tasks on localhost] *************************
TASK [debug] ********************************************************
ok: [localhost] => {
"msg": "leftover localhost tasks"
}
PLAY RECAP **********************************************************
C : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
localhost : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
您可以在您的角色中添加
when
条件。
- name: Play B
hosts: Host B
roles:
- role: my_b_role
when: my_condition | bool
缺点是您需要将这个条件添加到您的播放中的任何角色或任务中。