我在集群上运行 ansible playbook 以添加新机器。我希望它仅在添加新机器时运行,认为不存在旧机器。我可以使用“--limit”将剧本限制在一台机器上,但在这种情况下,我在创建之前不知道机器名称或 ip。
如何在通过 ansible 添加新机器时跳过集群上的现有机器?
谢谢
我在集群上运行 ansible playbook 以添加新机器。我希望它仅在添加新机器时运行,认为不存在旧机器。我可以使用“--limit”将剧本限制在一台机器上,但在这种情况下,我在创建之前不知道机器名称或 ip。
如何在通过 ansible 添加新机器时跳过集群上的现有机器?
谢谢
如果您的剧本创建一个需要捕获公共或私有 IP 并将其与新组相关联的新机器,则可以使用 add_host 模块,然后在您的下一个游戏中使用该组。
看下一个例子:
- name: Create a sandbox instance
hosts: localhost
gather_facts: False
vars:
key_name: my_keypair
instance_type: m1.small
security_group: my_securitygroup
image: my_ami_id
region: us-east-1
tasks:
- name: Launch instance
ec2:
key_name: "{{ keypair }}"
group: "{{ security_group }}"
instance_type: "{{ instance_type }}"
image: "{{ image }}"
wait: true
region: "{{ region }}"
vpc_subnet_id: subnet-29e63245
assign_public_ip: yes
register: ec2
- name: Add new instance to host group
add_host: hostname={{ item.public_ip }} groupname=launched
with_items: ec2.instances
- name: Wait for SSH to come up
wait_for: host={{ item.public_dns_name }} port=22 delay=60 timeout=320 state=started
with_items: ec2.instances
- name: Configure instance(s)
hosts: launched
become: True
gather_facts: True
roles:
- my_awesome_role
- my_awesome_test