如何在运行ansible playbook时跳过现有机器

时间:2016-03-03 00:33:32

标签: ansible ansible-playbook ansible-2.x

我在集群上运行ansible playbook来添加新机器。我希望这个只运行,以添加新机器,认为没有旧机器存在。我可以使用" - 限制"将剧本限制为一台机器。但在这种情况下,我在创建之前不知道机器名称或IP。

如何在通过ansible添加新计算机时跳过群集上的现有计算机?

由于

1 个答案:

答案 0 :(得分:2)

你可以使用add_host模块,如果你在playbook中创建一台新机器,你需要抓住公共或私人IP并将其与一个新组联系起来,然后在下一场比赛中使用该组。

看看下一个例子:

- 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