gpt4 book ai didi

ansible - 如何为 CloudFormation 堆栈的 'collections' 运行 Ansible playbook?

转载 作者:行者123 更新时间:2023-12-03 07:13:44 24 4
gpt4 key购买 nike

我正在使用 Ansible 来管理大量的 CloudFormation 堆栈,所有这些堆栈都基于相同的 playbook 和 CloudFormation 模板。我想将堆栈视为库存中的节点,这可能吗?

例如,我有一个 CloudFormation 模板和一个 Ansible playbook,配置如下:

- name: CloudFormation
hosts: localhost
connection: local
gather_facts: false
vars_files:
- vars/global.yml
tasks:
- name: Network Stack
cloudformation:
stack_name: "{{ name }}-{{ env }}-network"

然后,我有一个在运行时传入的堆栈特定的 var 文件 ansible-playbook -e @one-prod-network.yml play.yml

我需要一种方法来针对组中的所有堆栈运行剧本,就好像它们是库存项目一样,例如:

[test]
one-test-network
two-test-network

[prod]
three-prod-network
four-prod-network

我认为角色可能是答案,但经过调查后我不太确定 - 显然我不想为每个角色重复模板。目前,我使用 Makefile 来运行每个堆栈的命令,但更喜欢在 Ansible 中执行。

最佳答案

I would like to treat the stacks as if they were nodes in an inventory, is this possible?

据我了解,是的,您可以通过指定ansible_connection在组cloud_formation的所有主机上本地然后嵌套来做到这一点其中的所有主机(或主机组)。

这是一个库存示例:

[cloud_formation:children]
test
prod

[cloud_formation:vars]
ansible_connection=local

[test]
one-test-network name=one
two-test-network name=two

[test:vars]
env=test

[prod]
three-prod-network name=three
four-prod-network name=four

[prod:vars]
env=prod

针对它运行此剧本:

- hosts: cloud_formation
gather_facts: false

tasks:
- debug:
msg:
cloudformation:
stack_name: "{{ name }}-{{ env }}-network"

会产生:

ok: [one-test-network] => 
msg:
cloudformation:
stack_name: one-test-network
ok: [two-test-network] =>
msg:
cloudformation:
stack_name: two-test-network
ok: [three-prod-network] =>
msg:
cloudformation:
stack_name: three-prod-network
ok: [four-prod-network] =>
msg:
cloudformation:
stack_name: four-prod-network

然后,如果我对您想要实现的目标的猜测是正确的,您甚至可以通过库存进一步简化:

[cloud_formation:children]
test
prod

[cloud_formation:vars]
ansible_connection=local

[test]
one-test-network
two-test-network

[prod]
three-prod-network
four-prod-network

根据剧本运行:

- hosts: cloud_formation
gather_facts: false

tasks:
- debug:
msg:
cloudformation:
stack_name: "{{ inventory_hostname }}"

会产生:

ok: [one-test-network] => 
msg:
cloudformation:
stack_name: one-test-network
ok: [two-test-network] =>
msg:
cloudformation:
stack_name: two-test-network
ok: [three-prod-network] =>
msg:
cloudformation:
stack_name: three-prod-network
ok: [four-prod-network] =>
msg:
cloudformation:
stack_name: four-prod-network

或者,如果没有父组,则库存:

[test]
one
two

[test:vars]
ansible_connection=local

[prod]
three
four

[prod:vars]
ansible_connection=local

根据剧本运行:

- hosts: test, prod
gather_facts: false

tasks:
- debug:
msg:
cloudformation:
stack_name: >-
{{ inventory_hostname -}}
-{{ group_names[0] -}}
-network
## /!\ Mind that this only works accuratly
## if the hosts are present in a single group

会产生:

ok: [one] => 
msg:
cloudformation:
stack_name: one-test-network
ok: [two] =>
msg:
cloudformation:
stack_name: two-test-network
ok: [three] =>
msg:
cloudformation:
stack_name: three-prod-network
ok: [four] =>
msg:
cloudformation:
stack_name: four-prod-network

关于ansible - 如何为 CloudFormation 堆栈的 'collections' 运行 Ansible playbook?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76234459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com