我正在创建一个Ansible角色来更新集群中配置的主机。在这个PSA架构中定义了3种角色。
host-1 # primary
host-2 # secondary
host-3 # secondary
host-4 # secondary
host-5 # arbiter如果主机是次要的,我想配置Ansible来执行一个任务块,如果主机是次要的,则每个主机一个一个地执行。但是在同一块中,执行单个任务作为主任务。
例如:
- name: remove host from cluster
shell: command_to_remove
- name: update
yum:
state: latest
name: application
- name: add host to cluster
shell: command_to_add
- name: verify cluster
shell: verify cluster
when: primary在这里,serial的使用是完美的,但block不支持这一点。
Ansible的结果应该是:
TASK [remove host from cluster]
changed: [host-2]
TASK [update]
changed: [host-2]
TASK [add host from cluster]
changed: [host-2]
TASK [verify cluster]
changed: [host-1]
# rinse and repeat for host-3
TASK [remove host from cluster]
changed: [host-3]
TASK [update]
changed: [host-3]
TASK [add host from cluster]
changed: [host-3]
TASK [verify cluster]
changed: [host-1]
# and so on, until host-4发布于 2022-01-17 14:40:10
虽然在某些情况下它可能是“不正确的”,但您可以将hostvar放置为每个主机声明某种“配置文件”,然后在此基础上使用一个带有include_tasks的profile.yaml执行“profile.yaml”,然后在profile.yaml中放置任务。
我用我用来配置机器的游戏手册做类似的事情,它基于在ansible调用上声明的配置文件。
此剧本是在正在配置的膝上型计算机上调用的,因此它使用localhost,但是您可以通过一些调整远程使用它。
hosts.yaml
all:
hosts:
localhost:
ansible_connection: local
vars:
profile: allplaybook.yaml
- hosts: all
tasks:
...
- name: Run install tasks
include_role:
name: appinstalls
...roles/appinstalls/tasks/main.yaml
- name: Include tasks for profile "all"
include_tasks: profile_all.yml
when: 'profile == "all"'
- name: Include tasks for profile "office"
include_tasks: profile_office.yml
when: 'profile == "office"'roles/appinstalls/tasks/profile_all.yml
- name: Run tasks for profile "all"
include_tasks: "{{ item }}.yml"
with_items:
- app1
- app2
- app3
- app4roles/appinstalls/tasks/profile_office.yml
- name: Run tasks for profile "all"
include_tasks: "{{ item }}.yml"
with_items:
- app1
- app2
- app3
- app4
- office_app1
- office_app2
- office_app3
- office_app4当使用以下方法调用时:
ansible-playbook playbook.yml -i hosts.yaml -K它将使用默认配置文件"all“运行。如果使用
ansible-playbook playbook.yml -i hosts.yaml -K -e profile=office它将使用"office“配置文件代替运行。
关于这个方法,我喜欢的是,所有的包含都是一次性发生的,而不是当你走的时候:
TASK [appinstalls : Run tasks for profile "office"] ******************************************************************************************************************************************
included: /home/blenderfox/machine-setup/files/roles/appinstalls/tasks/app1.yml for localhost => (item=app1)
included: /home/blenderfox/machine-setup/files/roles/appinstalls/tasks/app2.yml for localhost => (item=app2)
included: /home/blenderfox/machine-setup/files/roles/appinstalls/tasks/app3.yml for localhost => (item=app3)
included: /home/blenderfox/machine-setup/files/roles/appinstalls/tasks/app4.yml for localhost => (item=app4)
included: /home/blenderfox/machine-setup/files/roles/appinstalls/tasks/office_app1.yml for localhost => (item=office_app1)
included: /home/blenderfox/machine-setup/files/roles/appinstalls/tasks/office_app2.yml for localhost => (item=office_app2)
included: /home/blenderfox/machine-setup/files/roles/appinstalls/tasks/office_app3.yml for localhost => (item=office_app3)
included: /home/blenderfox/machine-setup/files/roles/appinstalls/tasks/office_app4.yml for localhost => (item=office_app4)因此,在您的情况下,您可以将您的任务放在相当于profile_all或profile_office的位置。
这种方法没有任何花哨的东西,它使它更容易解决未来的变化。
它完全允许每个配置文件独立于另一个配置文件--只需更改每个Run tasks for profile块中的列表即可。
https://stackoverflow.com/questions/70741946
复制相似问题