[英]Ansible EC2 with a static inventory
I'm using ansible to provision servers on EC2, using the dynamic inventory and exact_count. 我正在使用ansible通过动态库存和exact_count在EC2上配置服务器。 This lets me scale up/down when I need to, which is nice. 这使我可以在需要时按比例放大/缩小,这很好。
Now I need to add a unique variable to the environment on each server when I provision them. 现在,在设置服务器时,需要在每个服务器的环境中添加一个唯一变量。 One way I thought of doing this is using the inventory file like this: 我想到的一种方式是使用清单文件,如下所示:
[ec2-servers]
host1 myvar=abc
host2 myvar=def
...
where host1(2) somehow refer to the relevant EC2 instance, via tag_SomeName_host1 or similar. 其中主机1(2)以某种方式参考相关EC2实例,经由tag_SomeName_host1或相似。
But this doesn't tie in with how I'm currently provisioning servers. 但这与我当前配置服务器的方式不相关。 The dynamic inventory with exact_count gives me a set of identical clone servers. 具有精确计数的动态清单为我提供了一组相同的克隆服务器。
Is there a way I can define servers in my inventory file, and have ansible provision it in ec2 if it doesn't exist, and remove it if a server exists in ec2 but not the inventory? 有没有一种方法,我可以定义在我的库存文件服务器,并有ansible规定它在EC2上,如果它不存在,如果一台服务器在EC2存在,但不清单中移除?
eg 例如
I run my playbook for the first time with the inventory: 我第一次使用清单运行我的剧本:
[ec2-servers]
host1 myvar=1
Then later I need to scale up so edit the inventory: 然后,稍后我需要按比例放大,因此请编辑库存:
[ec2-servers]
host1 myvar=1
host2 myvar=2
and ansible ignores host1 as it already exists, then provisions an instance for host2. ansible会忽略host1(因为它已经存在),然后为host2设置一个实例。
Then later I no longer need the extra server so modify the inventory: 然后,以后我不再需要额外的服务器,因此可以修改清单:
[ec2-servers]
host1 myvar=1
and ansible removes host2 from ec2. 并且ansible从ec2中删除host2。
No. Ansible doesn't [care to]: 不。Ansible不会[关心]:
You need to build the logic yourself in the playbook. 您需要自己在剧本中构建逻辑。
Something like, my_playbook.yml
: 类似于my_playbook.yml
:
- hosts: to_be_provisioned
tasks:
- include: provision_ec2_host.yml
- hosts: to_be_unprovisioned
tasks:
- include: unprovision_ec2_host.yml
both unprovision_ec2_host.yml
& provision_ec2_host.yml
should be idempotent of course. 当然, unprovision_ec2_host.yml
和provision_ec2_host.yml
都应是幂等的。
Now you need to ensure that your inventory has correct set of hosts under the host-groups to_be_provisioned
& to_be_unprovisioned
and run my_playbook.yml. 现在,您需要确保清单在to_be_provisioned
和to_be_unprovisioned
主机组下具有正确的主机集,并运行my_playbook.yml。
$ cat inventory.ini
[to_be_provisioned]
host1 myvar=1
[to_be_unprovisioned]
host2 myvar=2
$ ansible-playbook -i inventory.ini my_playbook.yml
$ # modify inventory
$ cat inventory.ini
[to_be_provisioned]
host1 myvar=1
host2 myvar=2
# [to_be_unprovisioned] -- no hosts
$ ansible-playbook -i inventory.ini my_playbook.yml
$ # modify inventory
$ cat inventory.ini
[to_be_provisioned]
host1 myvar=1
[to_be_unprovisioned]
host2 myvar=2
$ ansible-playbook -i inventory.ini my_playbook.yml
Finally, to do this whole thing automatically, you can use your dynamic inventory . 最后,要自动完成全部操作,您可以使用动态清单 。 I recommend just make a copy of existing ec2.py
and modify it so it returns the groups as you want. 我建议仅复制现有ec2.py
并对其进行修改,以便它根据需要返回组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.