为什么Ansible使用以前创建的主机?

时间:2017-11-02 16:46:38

标签: amazon-ec2 ansible

我正在制作一个简单的ansible playbook来创建一个临时的EC2实例。我第一次运行剧本时收到了这个回顾 -

PLAY RECAP *********************************************************************
172.31.14.136              : ok=1    changed=0    unreachable=0    failed=0
localhost                  : ok=3    changed=1    unreachable=0    failed=0

运行之后,我意识到我想调整一些东西,所以我做了一些小改动(与主机无关)并再次运行并得到了这个回顾 -

PLAY RECAP *********************************************************************
172.31.13.74               : ok=1    changed=0    unreachable=0    failed=0
172.31.14.136              : ok=1    changed=0    unreachable=0    failed=0
localhost                  : ok=3    changed=1    unreachable=0    failed=0

我的playbook非常简单,只能包含两个主机 - 我的localhost和新创建的EC2实例的ip。这是剧本 -

- name: Provision instance
  hosts: localhost
  vars_files:
    - group_vars/test_ec2.yml

  roles:
    - { role: ec2_create, product: "tmp_instance_test" }

- name: Configure SSH Authorized Keys
  hosts: tmp_instance_test
  vars_files:
    - group_vars/test_ec2.yml

  roles:
    - { role: yarn }

我的问题是为什么第一次运行172.31.14.136的ip参与第二次运行?

更新

这是角色ec2_create -

- name: Create instance
  ec2:
    image: "{{ image }}"
    instance_type: "{{ instance_type }}"
    aws_access_key: "{{ aws_access_key_id }}"
    aws_secret_key: "{{ aws_secret_access_key }}"
    key_name: "{{ key_name }}"
    instance_tags:
      Name: "{{ name }}"
      Environment: "{{ env }}"
      Product: "{{ product }}"
      Service: "{{ service }}"
    region: "{{ region }}"
    volumes:
      - device_name: "{{ disk_name }}"
        volume_type: "{{ disk_type }}"
        volume_size: "{{ disk_size }}"
        delete_on_termination: "{{ delete_on_termination }}"
    group: "{{ security_group_name }}"
    wait: true
    vpc_subnet_id: "{{ vpc_subnet_id }}"
    count: "{{ instance_count }}"
    monitoring: "{{ detailed_monitoring }}"
    instance_profile_name: "{{ iam_role }}"
    assign_public_ip: "{{ assign_public_ip }}"
  register: ec2

- name: Wait for SSH to come up
  wait_for: host={{ item.private_ip }} port=22 timeout=600 state=started
  with_items: "{{ ec2.instances }}"

- name: refresh inventory
  meta: refresh_inventory

更新2

库存 -

[localhost]
localhost ansible_ssh_user=deployer ansible_connection=local ansible_python_interpreter=/usr/bin/python

[tag_Product_tmp_instance_test]
[tmp_instance_test:children]
tag_Product_tmp_instance_test
[tmp_instance_test:vars]
ansible_ssh_user=ubuntu 
ansible_ssh_private_key_file=~/.ssh/BaseDev

1 个答案:

答案 0 :(得分:1)

使用for (int i = list.size() - 2; i >= 0; i--) { if (list.get(i).length() > 1 && list.get(i + 1).length() > 1) { list.remove(i+1); } } 强制进行广告资源更新,如果您的AWS账户中的上一个服务器(示例中为meta: refresh_inventory)仍然存在,则会根据您的搜索条件(172.31.14.136)进行匹配因此属于tag_Product_tmp_instance_test组。

因此,如果您不终止由tmp_instance_test角色创建的主机,则所有这些主机都将在后续的Playbook运行中显示在您的广告资源中。