可售库存
# hosts
[groupA]
192.168.1.1
[groupB]
192.168.1.1
192.168.1.3
可行任务:
# site.yml
---
- name: test
hosts: all
tasks:
- name: taskA
command: echo testA
when: "'groupA' in group_names "
- name: taskB
command: echo testB
when: "'groupB' in group_names "
我要执行taskA
到groupA
的任务,如何执行?我使用以下命令执行
ansible-playbook site.yml -l groupA -i hosts
但是,获得以下结果不是我想要的,因为它也执行taskB
,但是我只想使用groupA
来执行taskA
。
我不想使用ansible tag
,因为我有很多roles
,将每个tag
添加到每个role
会有很多工作。 / p>
PLAY [test] *******************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************
ok: [192.168.1.1]
TASK [taskA] ******************************************************************************************************************************************
changed: [192.168.1.1]
TASK [taskB] ******************************************************************************************************************************************
changed: [192.168.1.1]
PLAY RECAP ********************************************************************************************************************************************
192.168.1.1 : ok=3 changed=2 unreachable=0 failed=0
答案 0 :(得分:0)
Q“ I just want the machine inside group_B to execute Task of B...For example, the machine inside group_B only executes Task of B.
”
A:测试主机在组中的成员资格。请参见下面的代码。
- hosts: all
tasks:
- name: Task of group_A
command: bash groupA.sh
when: inventory_hostname in groups['group_A']
- name: Task of group_B
command: bash groupB.sh
when: inventory_hostname in groups['group_B']
Q:“ I just want to run Task of group_B. What should my order be? ansible-playbook site.yml -l group_B -i ansible_inventory If group_A and group_B have a common ip, Task of group_A will be executed, but I only want to execute Task of group_B.
”
“ I don't want to use ansible tag because I have a lot. roles, there will be a lot of work to add each tag to each role.
”
A:使用include_role并应用标签(在2.7中添加)。此类应用的标签由包含角色中的所有任务继承。请参见下面的代码。
- name: Task of group_B
include_role:
name: role123
apply:
tags: group_B
when: inventory_hostname in groups['group_B']
tags: group_B
运行剧本
ansible-playbook -t group_B -i ansible_inventory site.yml