Используйте ansible для создания экземпляров ec2 в разных зонах доступности. - PullRequest
0 голосов
/ 05 августа 2020

Я создал сценарий для развертывания экземпляров ec2 в разных зонах доступности. Это не удается, когда я добавляю with_items для зоны подсчета и su bnet. Как это преодолеть.

Я следил за этим Ansible: Создавал экземпляры в разных подсетях , но это не сработало, поэтому создал новый объект, так как я не мог его обновить

playbook здесь

- name: Deploy EC2 Master node
  hosts: localhost
  gather_facts: False
  vars_files:
    - ~/aws-common/automation/ansible/config/var_input.yml
  tasks:
    - name: Get AWS credentials
      sts_assume_role:
        role_arn: "{{ role_arn }}"
        role_session_name: "{{ role_session_name }}"
      register: assumed_role
    - name: Provision a set of instances
      ec2:
         key_name: "{{ key_name }}"
         group: "{{ group }}"
         instance_type: "{{ instance_type }}"
         region: "{{ region }}"
         image: "{{ image }}"
         wait: "{{ wait }}"
         wait_timeout: "{{ wait_timeout }}"
         #count: "{{ count }}"
         count: "{{ item.ec2_count }}"
         instance_profile_name: "{{ instance_profile_name }}"
         instance_tags:
           Name: "{{ Name }}"
           deployer: "{{ deployer }}"
           resourceowner: "{{ resourceowner }}"
         monitoring: "{{ monitoring }}"
         ec2_zone: "{{ item.zone }}"
         vpc_subnet_id: "{{ item.subnet }}"
         assign_public_ip: "{{ assign_public_ip }}"
         aws_access_key: "{{ assumed_role.sts_creds.access_key }}"
         aws_secret_key: "{{ assumed_role.sts_creds.secret_key }}"
         security_token: "{{ assumed_role.sts_creds.session_token }}"
         volumes:
         - device_name: /dev/sda1
           volume_type: gp2
           encrypted: true
           volume_name: HadoopMaster-rootvolume /
           volume_size: 100
           delete_on_termination: true
         - device_name: /dev/sdf
           volume_type: gp2
           encrypted: true
           volume_name:  HadoopMaster /edw/xxx
           volume_size: 70
           delete_on_termination: true
      with_items: "{{ ec2_zone_subnet }}"
      register: ec2

    - name: Add new instance to host group
      add_host:
        hostname: "{{ item.public_ip }}"
        groupname: launched
      loop: "{{ ec2.instances }}"

    - name: Wait for SSH to come up
      delegate_to: "{{ item.public_dns_name }}"
      wait_for_connection:
        delay: 60
        timeout: 320
      loop: "{{ ec2.instances }}"

- hosts: launched
  name: Mounting the attached EBS volumes
  user: ec2-user
  become: yes
  gather_facts: no
  tasks:
      - name: Run a script with arguments (free form)
        script: ~/files/format.sh

Это моя запись в файле переменных

ec2_zone_subnet: [{"zone":"us-east-1a", "subnet":"subnet-5f87a23b", "ec2_count":1},{"zone":"us-east-1b", "subnet":"subnet-a9d2bd86","ec2_count":1},{"zone":"us-east-1c", "subnet":"subnet-0cab05cf89718e652","ec2_count":0},{"zone":"us-east-1d", "subnet":"subnet-079c66d30dcd53ce7","ec2_count":0}]

Я получаю ошибку

ok: [localhost] => (item={u'subnet': u'subnet-0cab05cf89718e652', u'ec2_count': 0, u'zone': u'us-east-1c'}) => {"changed": false, "instance_ids": [], "instances": [], "item": {"ec2_count": 0, "subnet": "subnet-0cab05cf89718e652", "zone": "us-east-1c"}, "tagged_instances": []}
ok: [localhost] => (item={u'subnet': u'subnet-079c66d30dcd53ce7', u'ec2_count': 0, u'zone': u'us-east-1d'}) => {"changed": false, "instance_ids": [], "instances": [], "item": {"ec2_count": 0, "subnet": "subnet-079c66d30dcd53ce7", "zone": "us-east-1d"}, "tagged_instances": []}

TASK [Add new instance to host group] **********************************************************************************************************************
task path: /home/desind/aws-common/automation/ansible/files/testnew.yaml:51
fatal: [localhost]: FAILED! => {"msg": "'dict object' has no attribute 'instances'"}

Теперь элементы имеют только ec2-count subnet и host. Я не вижу других атрибутов. Может ли кто-нибудь помочь мне с использованием реестра: ec2 и with_items

1 Ответ

0 голосов
/ 05 августа 2020

Это следующие изменения, которые я внес в playbook

  1. Изменен vpc_subnet_id: "{{item}}"
  2. with_items: "{{subnet_ids}}"
- name: Add all instance private IPs to host group
      add_host: hostname={{ item.instances[0].private_ip }} instance_id={{ item.instance_ids[0] }} groups=launched
      with_items: '{{ ec2.results }}'

    - name: Wait for SSH to come up
      delegate_to: "{{ item.instances[0].private_ip }}"
      wait_for_connection:
        delay: 100
        timeout: 320
      loop: "{{ ec2.results }}"
Добавлены такие подсети в файл переменных subnet_ids: [su bnet -xxxx, su bnet -axxx, su bnet -xxxx]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...