我正在创建一个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
虽然它可能不是"正确的";在某些情况下,您可以放置一个hostvar来声明一种";配置文件";对于每个主机;include_tasks"使用基于此的profile.yaml,然后在profile.yaml中放入您的任务。
我使用一个剧本做了类似的事情,该剧本用于根据ansible调用中声明的概要文件来配置机器。
这个剧本是在正在配置的笔记本电脑上调用的,所以它使用localhost,但您可以通过一些调整远程使用它
hosts.yaml
all:
hosts:
localhost:
ansible_connection: local
vars:
profile: all
playbook.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
- app4
roles/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
它将使用默认配置文件";所有";。如果使用调用
ansible-playbook playbook.yml -i hosts.yaml -K -e profile=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
块中的列表