我有这样的清单(inventory
)
[worker_nodes]
ubuntuw01
ubuntuw02
ubuntuw03
ubuntuw04
[worker_nodes:vars]
kube_role="worker"
[controlplane_nodes]
ubuntuc01
ubuntuc02
[controlplane_nodes:vars]
kube_role="controlplane"
[etcd_nodes]
ubuntue01
ubuntue02
ubuntue03
[etcd_nodes:vars]
kube_role="etcd"
我想为其应用这样的角色(myplay.yml
)
---
- name: tasks for kube cluster
hosts: all
serial: 1
roles:
- os_patching
我的roles/os_patching/tasks/main.yml
如下:
---
- name: "Update etc nodes"
import_tasks: etcd.yml
when: kube_role == "etcd"
- name: "Update controlplane nodes"
import_tasks: controlplane.yml
when: kube_role == "controlplane"
- name: "Update worker nodes"
import_tasks: worker.yml
when: kube_role == "worker"
当我像这样运行ansible时:
ansible-playbook -i inventory -b myplay.yml
我得到了
TASK [<various task names>] ***************************************************************************************
skipping: [ubuntu02]
适用于大多数任务,但琐碎的任务如Gathering Facts
。
我猜我的when
子句无法按预期工作,但是我对为什么这样做有点迷惑。我在做什么错了?
另一种方法是定义单独的角色,但是节点类型共享任务,所以我 认为这将是一个好主意。另外,它是一个群集,因此我想全面了解群集如何在一个角色中打补丁。也许这是个坏主意?
答案 0 :(得分:0)
结果非常简单,我什至不需要定义任何特殊变量。我的库存:
[worker_nodes]
ubuntuw01
ubuntuw02
ubuntuw03
ubuntuw04
[controlplane_nodes]
ubuntuc01
ubuntuc02
[etcd_nodes]
ubuntue01
ubuntue02
ubuntue03
还有我的main.yml
:
---
- name: "Update etcd nodes"
import_tasks: etcd.yml
when: "'etcd_nodes' in group_names"
- name: "Update controlplane nodes"
import_tasks: controlplane.yml
when: "'controlplane_nodes' in group_names"
- name: "Update worker nodes"
import_tasks: worker.yml
when: "'worker_nodes' in group_names"
因此,基本上,我正在when
子句中检查特定节点所在的组名。