引用刚在Ansible中创建的EC2资源的实例ID

Bac*_*n25 1 amazon-ec2 ansible

我在引用刚刚创建的EC2资源的实例ID时遇到了困难。创建它之后,我想立即终止它。我的代码如下:

谢谢你,比尔

---
- name: Example of provisioning servers
  hosts: 127.0.0.1

  connection: local
  tasks:
   - name: set_fact1
     set_fact: foo = 1

   - name: Create security group
     local_action:
       module: ec2_group
       name: ep2
       description: Access to the Episode2 servers
       region: us-east-1
       rules:
         - proto: tcp
           from_port: 22
           to_port: 22
           cidr_ip: 0.0.0.0/0

   - name: Launch instances
     local_action:
       module: ec2
       instance_tags:
        Name: server1
        Env: myenv
       region: us-east-1
       keypair: ansiblekeypair
       group: ep2
       instance_type: m1.small
       image: ami-1aae3a0c
       count: 1
       wait: yes
     register: ec2

   - name: Terminate instances that were previously launched
     ec2:
       state: absent
       region: us-east-1
       instance_ids: "{{ ec2.instance_id[0] }}"
     with_items: ec2
Run Code Online (Sandbox Code Playgroud)

Kon*_*rov 6

您需要参考ec2.instances[0].id

使用- debug: var=ec2任务或带-vv开关运行剧本很有用,以查看已注册变量的详细值并检查哪些属性可供使用。