Running multiple volumes with an ec2 instance using

I provide an ec2 instance with the number of volumes connected to it. Follow my book to do the same.

---
- hosts: localhost
  connection: local
  gather_facts: false
  vars:
    instance_type: 't2.micro'
    region: 'my-region'
    aws_zone: 'myzone'
    security_group: my-sg
    image: ami-sample
    keypair: my-keypair
    vpc_subnet_id: my-subnet
  tasks:
  - name: Launch instance
    ec2:
      image: "{{ image }}"
      instance_type: "{{ instance_type }}"
      keypair: "{{ keypair}}"
      instance_tags: '{"Environment":"test","Name":"test-provisioning"}'
      region: "{{region}}"
      aws_zone: "{{ region }}{{ aws_zone }}"
      group: "{{ security_group }}"
      vpc_subnet_id: "{{vpc_subnet_id}}"
      wait: true
      volumes:
        - device_name: "{{ item }}"
          with_items:
             - /dev/sdb
             - /dev/sdc
          volume_type: gp2
          volume_size: 100
          delete_on_termination: true
          encrypted: true
    register: ec2_info

But getting the following error

fatal: [localhost]: FAILED! => {"failed": true, "msg": "the" args "field has an invalid value, which appears to contain an undefined variable. Error:" item "- undefined

If I replace {{item}}with /dev/sdb, the instance will be launched with the specific volume. But I want to create more than one volume with the specified list of items - / dev / sdb, / dev / sdc, etc. Any possible way to achieve this?

+4
1

with_items vars - .
:

- name: Populate volumes list
  set_fact:
    vol:
      device_name: "{{ item }}"
      volume_type: gp2
      volume_size: 100
      delete_on_termination: true
      encrypted: true
  with_items:
     - /dev/sdb
     - /dev/sdc
  register: volumes

exec ec2 :

volumes: "{{ volumes.results | map(attribute='ansible_facts.vol') | list }}"

: set_fact:

Variable ( device_name):

vol_default:
  volume_type: gp2
  volume_size: 100
  delete_on_termination: true
  encrypted: true

ec2 :

volumes: "{{ [{'device_name': '/dev/sdb'},{'device_name': '/dev/sdc'}] | map('combine',vol_default) | list }}"
+3

Source: https://habr.com/ru/post/1655483/


All Articles