如何使用Ansible将EC2实例添加到ALB的目标组?

时间:2018-11-21 18:58:54

标签: amazon-web-services ansible

我正在编写Ansible剧本,在其中创建AMI,并从该AMI中创建一个新实例,该实例应自动加入AWS中已经存在的ALB中的多个目标组。

但是不知何故,我的剧本给了我一个错误,说,

致命:[localhost]:失败! => {“更改”:false,“ msg”:“ ELB”测试“不存在”}

我的剧本如下,

---
- name: "AMI"
hosts: local
connection: local
gather_facts: False

vars:
  instance_id1: i-0s434345554611b9
  state: present
  ami_id: testami
  instance_type: t2.micro
  count: 1
  region: us-east-2
  alb: test

tasks:
- name: "aws ami creation"
  ec2_ami:
    instance_id: "{{ instance_id1 }}"
    description: testami
    wait: yes
    state:  "{{ state }}"
    delete_snapshot: yes
    no_reboot: yes
    region: "{{ region }}"
    name: "{{ ami_id  }}"
  register: ami


- name: Launch the new EC2 Instance
  ec2:
    instance_type: "{{ instance_type }}"
    image: "{{ ami.image_id }}"
    vpc_subnet_id: subnet-f1234563
    assign_public_ip: yes
 #   termination_protection: yes
    group: ['test-100', 'ec2-test', 'pUB_IP', 'launch-wizard-1']
    key_name: ec2-test
    state:  "{{ state }}"
    instance_tags:
      name: ansible-test-instance
    wait: true
    region: "{{ region }}"
    count: "{{ count }}"
  register: ec2

- name: Add instance to ALB
  ec2_elb:
     state: present
     ec2_elbs: "{{ alb }}"
     region: "{{ region }}"
     instance_id: "{{ ec2.instances[0].id }}"

如果您有任何想法如何将新创建的实例附加到ALB的目标组,请告诉我。

谢谢

0 个答案:

没有答案