【问题标题】:Why is Ansible using a previously created host?为什么 Ansible 使用之前创建的主机?
【发布时间】:2017-11-02 16:46:38
【问题描述】:

我正在编写一个简单的 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

我的剧本非常简单,应该只包含两个主机 - 我的 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 }

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


更新

这里是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

【问题讨论】:

  • 谁知道ec2_create角色里面有什么?
  • @KonstantinSuvorov 我用 ec2_create 角色更新了问题
  • 还有一个猜测:你使用像ec2.py这样的东西生成的动态库存?
  • 我正在使用动态库存并更新了我的问题。这可能是一个缓存问题吗?而且这似乎只有在我之后立即运行时才会发生。

标签: amazon-ec2 ansible


【解决方案1】:

使用meta: refresh_inventory,您可以强制更新库存,如果之前的服务器(在您的示例中为172.31.14.136)在您的AWS 账户中仍然存在,那么它会与您的搜索条件(tag_Product_tmp_instance_test)匹配,因此属于tmp_instance_test组。

因此,如果您不终止由ec2_create 角色创建的主机,那么所有这些主机都会在后续 playbook 运行时出现在您的清单中。

【讨论】:

  • 刚刚看了一下缓存就看到了,这正是正在发生的事情。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-08
  • 1970-01-01
  • 1970-01-01
  • 2022-12-21
  • 2016-04-04
相关资源
最近更新 更多