我努力让这项工作没有成功。这是我的剧本。
首先按标签搜索我的机器,然后尝试终止它们。
- name: EC2 Facts
ec2_instance_facts:
region: us-east-1
filters:
"tag:Type": "staging"
aws_access_key: "{{ lookup('env', 'AWS_ACCESS_KEY_ID') }}"
aws_secret_key: "{{ lookup('env', 'AWS_SECRET_ACCESS_KEY') }}"
register: ec2
- name: Kill EC2 Instance
ec2:
instance_ids: "{{ item.instance_id }}"
state: "{{ state }}"
region: "{{ lookup('env', 'AWS_REGION') }}"
aws_access_key: "{{ lookup('env', 'AWS_ACCESS_KEY_ID') }}"
aws_secret_key: "{{ lookup('env', 'AWS_SECRET_ACCESS_KEY') }}"
with_items: "{{ ec2.instances }}"
像这样跑:
ansible-playbook ec2_id_kill.yml --extra-vars "state=absent"
看起来ansible无法从事实中找到 instance_id
答案 0 :(得分:1)
我用它来终止单个实例,你可以调整它来终止多个实例:
- name: EC2 Facts
ec2_instance_facts:
region: us-east-1
filters:
"tag:Type": "staging"
aws_access_key: "{{ lookup('env', 'AWS_ACCESS_KEY_ID') }}"
aws_secret_key: "{{ lookup('env', 'AWS_SECRET_ACCESS_KEY') }}"
register: ec2
- name: Kill EC2 Instance
ec2:
instance_ids: "{{ ec2.instances[0].id }}"
state: "{{ state }}"
region: "{{ lookup('env', 'AWS_REGION') }}"
aws_access_key: "{{ lookup('env', 'AWS_ACCESS_KEY_ID') }}"
aws_secret_key: "{{ lookup('env', 'AWS_SECRET_ACCESS_KEY') }}"
希望能帮到你