AWS Ansible遍历多个区域并共享AMI

时间:2016-11-01 17:19:03

标签: amazon-web-services ansible

我创建了一个Ansible playbook来搜索不同地区的某个AMI名称:

---
- hosts: localhost
  tasks:
  - ec2_ami_find:
      region: "{{ item }}"
      owner: "self"
      name: "*ami-name*"
      sort: creationDate
      sort_order: descending
      sort_end: 1
      no_result_action: fail
    register: ami_result
    with_items: "{{ regions }}"

使用调试选项:

- debug: var=ami_result.results

我得到以下输出(摘录):

ok: [localhost] => {
    "ami_result.results": [
        {
            "item": "us-east-1", 
            "results": [
                {
                    "ami_id": "ami-6b0e727c", 
                }
            ]
        }, 
            "item": "eu-west-1", 
            "results": [
                {
                    "ami_id": "ami-c91e66ba"
                }
            ]
        }
    ]
}

如何使用ec2_ami Ansible模块遍历不同地区的项目和结果以与其他人共享AMI?

对于单个区域,我可以像这样访问ami_id

image: "{{ ami_result.results[0].ami_id }}

1 个答案:

答案 0 :(得分:0)

好的,找到了Ansible 2.1+和loop_control

的方法

首先,find_ami.yml

- ec2_ami_find:
    region: "{{ outer_item }}"
    owner: "self"
    name: "ami-name"
    sort: creationDate
    sort_order: descending
    sort_end: 1
    no_result_action: fail
  register: ami_result

- ec2_ami:
    region: "{{ outer_item }}"
    image_id: "{{ ami_result.results[0].ami_id }}"
    launch_permissions:
      user_ids: ['1234567890']

share_ami.yml

---
- hosts: localhost
  tasks:
  - include: find_ami.yml
    with_items: "{{regions}}"
    loop_control:
      loop_var: outer_item

区域存储在group_vars/all