# Question: with\_sequence and registered variables not having anticipated outcome

**URL:** <https://forum.ansible.com/t/question-with-sequence-and-registered-variables-not-having-anticipated-outcome/22660>\
**Category:** Ansible Project\
**Created:** [September 28, 2016, 1:07am UTC](https://forum.ansible.com/t/question-with-sequence-and-registered-variables-not-having-anticipated-outcome/22660 "2016-09-28T01:07:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Stobie](https://avatars.discourse-cdn.com/v4/letter/c/e47c2d/32.png) [@Chris\_Stobie](https://forum.ansible.com/u/Chris_Stobie)\
**Post date:** [September 28, 2016, 1:07am UTC](https://forum.ansible.com/t/question-with-sequence-and-registered-variables-not-having-anticipated-outcome/22660/1 "2016-09-28T01:07:08Z")

</div>

I have a playbook that creates an ec2 instance and from that creation I want to register the output as a var. From there I would use the registered var to create and attach an ebs volume. However I’m running into the following error.

```auto
 - name: Create EC2 instance for zone A
   ec2:
    key_name: "{{ keypair }}"
    group: "{{ security_groups }}"
    image: "{{ ami }}"
    instance_type: "{{ instance_type }}"
    wait: true
    region: "{{ ec2_region }}"
    vpc_subnet_id: "{{ subneta }}"
    assign_public_ip: "{{ public_choice }}"
    zone: "{{ zonea }}"
    count: 1
    instance_tags:
      Name: "db{{ item }}a.{{ env }}"
      envtype: "{{ envtype }}"
   register: ec2
   with_sequence: "start=1 end={{ num }}"

 - debug:
    msg: "{{ ec2 }}"
 - debug:
    msg: "{{ ec2.instance_ids }}"

```

With this I get the following output

```auto
dumbledore@ansible1a:/etc/ansible/roles/db_ec2/tasks > ansible-playbook db_ec2.yml -e 'env=qa num=1 ebs=true'
 ______
< PLAY >
 ------
        \ ^__^
         \ (oo)\ _______
            (__)\ )\/\
                >>----w |
                >> >>

 ______________________
< TASK [db_ec2 : fail] >
 ----------------------
        \ ^__^
         \ (oo)\ _______
            (__)\ )\/\
                >>----w |
                >> >>

 _______________________
< TASK [db_ec2 : debug] >
 -----------------------
        \ ^__^
         \ (oo)\ _______
            (__)\ )\/\
                >>----w |
                >> >>

ok: [localhost] => {
    "msg": "If you need to change any default variables for this playbook edit vars/qa.yml and vars/ebs.yml for ebs configs"
}
 _________________________
< TASK [db_ec2 : include] >
 -------------------------
        \ ^__^
         \ (oo)\ _______
            (__)\ )\/\
                >>----w |
                >> >>

included: /etc/ansible/roles/db_ec2/tasks/./db_create.yml for localhost
 ________________________________________________
< TASK [db_ec2 : Create EC2 instance for zone A] >
 ------------------------------------------------
        \ ^__^
         \ (oo)\ _______
            (__)\ )\/\
                >>----w |
                >> >>

changed: [localhost] => (item=1)
 _______________________
< TASK [db_ec2 : debug] >
 -----------------------
        \ ^__^
         \ (oo)\ _______
            (__)\ )\/\
                >>----w |
                >> >>

ok: [localhost] => {
    "msg": {
        "changed": true,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_no_log": false,
                "changed": true,
                "instance_ids": [
                    "i-1046c108"
                ],
                "instances": [
                    {
                        "ami_launch_index": "0",
                        "architecture": "x86_64",
                        "block_device_mapping": {
                            "/dev/sda1": {
                                "delete_on_termination": true,
                                "status": "attached",
                                "volume_id": "vol-55812edd"
                            }
                        },
                        "dns_name": "",
                        "ebs_optimized": false,
                        "groups": {
                            "sg-749f3c0d": "qa-ssh",
                            "sg-8f983bf6": "qa-db"
                        },
                        "hypervisor": "xen",
                        "id": "i-1046c108",
                        "image_id": "ami-55e31a35",
                        "instance_type": "m4.xlarge",
                        "kernel": null,
                        "key_name": "ccpkey",
                        "launch_time": "2016-09-28T00:28:00.000Z",
                        "placement": "us-west-2a",
                        "private_dns_name": "ip-10-50-36-201.us-west-2.compute.internal",
                        "private_ip": "10.50.36.201",
                        "public_dns_name": "",
                        "public_ip": null,
                        "ramdisk": null,
                        "region": "us-west-2",
                        "root_device_name": "/dev/sda1",
                        "root_device_type": "ebs",
                        "state": "running",
                        "state_code": 16,
                        "tags": {
                            "Name": "db1a.qa",
                            "envtype": "qa-db"
                        },
                        "tenancy": "default",
                        "virtualization_type": "hvm"
                    }
                ],
                "invocation": {
                    "module_args": {
                        "assign_public_ip": false,
                        "aws_access_key": null,
                        "aws_secret_key": null,
                        "count": 1,
                        "count_tag": null,
                        "ebs_optimized": false,
                        "ec2_url": null,
                        "exact_count": null,
                        "group": [
                            "qa-db",
                            "qa-ssh"
                        ],
                        "group_id": null,
                        "id": null,
                        "image": "ami-55e31a35",
                        "instance_ids": null,
                        "instance_profile_name": null,
                        "instance_tags": {
                            "Name": "db1a.qa",
                            "envtype": "qa-db"
                        },
                        "instance_type": "m4.xlarge",
                        "kernel": null,
                        "key_name": "ccpkey",
                        "monitoring": false,
                        "network_interfaces": null,
                        "placement_group": null,
                        "private_ip": null,
                        "profile": null,
                        "ramdisk": null,
                        "region": "us-west-2",
                        "security_token": null,
                        "source_dest_check": true,
                        "spot_price": null,
                        "spot_type": "one-time",
                        "spot_wait_timeout": 600,
                        "state": "present",
                        "tenancy": "default",
                        "termination_protection": false,
                        "user_data": null,
                        "validate_certs": true,
                        "volumes": null,
                        "vpc_subnet_id": "subnet-5d625c39",
                        "wait": true,
                        "wait_timeout": 300,
                        "zone": "us-west-2a"
                    },
                    "module_name": "ec2"
                },
                "item": "1",
                "tagged_instances": []
            }
        ]
    }
}
 _______________________
< TASK [db_ec2 : debug] >
 -----------------------
        \ ^__^
         \ (oo)\ _______
            (__)\ )\/\
                >>----w |
                >> >>

fatal: [localhost]: FAILED! => {"failed": true, "msg": "'dict object' has no attribute 'instance_ids'"}
 ____________
< PLAY RECAP >
 ------------
        \ ^__^
         \ (oo)\ _______
            (__)\ )\/\
                >>----w |
                >> >>

localhost : ok=4 changed=1 unreachable=0 failed=1

```

So I can see that the variable that should exist does in the output of the full ec2 variable. But when trying to use the key specific variable of ec2.instance\_ids it fails…

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 28, 2016, 6:15am UTC](https://forum.ansible.com/t/question-with-sequence-and-registered-variables-not-having-anticipated-outcome/22660/2 "2016-09-28T06:15:22Z")

</div>

\<snip\>

> - name: Create EC2 instance for zone A  
> &nbsp;&nbsp;&nbsp;ec2:  
> &nbsp;&nbsp;&nbsp;&nbsp;key\_name: "{{ keypair }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;group: "{{ security\_groups }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;image: "{{ ami }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;instance\_type: "{{ instance\_type }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;wait: true  
> &nbsp;&nbsp;&nbsp;&nbsp;region: "{{ ec2\_region }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;vpc\_subnet\_id: "{{ subneta }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;assign\_public\_ip: "{{ public\_choice }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;zone: "{{ zonea }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;count: 1  
> &nbsp;&nbsp;&nbsp;&nbsp;instance\_tags:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name: "db{{ item }}a.{{ env }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;envtype: "{{ envtype }}"  
> &nbsp;&nbsp;&nbsp;register: ec2  
> &nbsp;&nbsp;&nbsp;with\_sequence: "start=1 end={{ num }}"

Remember that all the with\_ is a loop, and the register is a little different as described here  
[https://docs.ansible.com/ansible/playbooks\_loops.html#using-register-with-a-loop](https://docs.ansible.com/ansible/playbooks_loops.html#using-register-with-a-loop)

The result is stored in a list ec2.results

\<snip\>

> ok: [localhost] =\> {  
> &nbsp;&nbsp;&nbsp;&nbsp;"msg": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"changed": true,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"msg": "All items completed",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"results": [

Here you can see the results list.

\<snip\>

> So I can see that the variable that should exist does in the output of the  
> full ec2 variable. But when trying to use the key specific variable of  
> ec2.instance\_ids it fails...

The correct entry would be ec2.results[0].instance\_ids for the first iteration of the loop and for the second iteration ec2.results[1].instance\_ids and so on.

---

<div class="post-metadata">

**Author:** ![Christopher\_Stobie](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@Christopher\_Stobie](https://forum.ansible.com/u/Christopher_Stobie)\
**Post date:** [September 28, 2016, 5:25pm UTC](https://forum.ansible.com/t/question-with-sequence-and-registered-variables-not-having-anticipated-outcome/22660/3 "2016-09-28T17:25:59Z")

</div>

OK that makes total sense.

Follow up question, what is the syntax in ansible to iterate over every entry in the array.

So for individual results you do ec2.results[0].instance\_ids.

What do you do for all results or to loop over this to get every instance id from multiple entries.

like ec2.results[i].instance\_ids or something?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 28, 2016, 6:15pm UTC](https://forum.ansible.com/t/question-with-sequence-and-registered-variables-not-having-anticipated-outcome/22660/4 "2016-09-28T18:15:57Z")

</div>

- debug: var=item.instance\_ids  
&nbsp;&nbsp;&nbsp;with\_items: "{{ ec2.results }}"

---

<div class="post-metadata">

**Author:** ![Christopher\_Stobie](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@Christopher\_Stobie](https://forum.ansible.com/u/Christopher_Stobie)\
**Post date:** [September 28, 2016, 6:41pm UTC](https://forum.ansible.com/t/question-with-sequence-and-registered-variables-not-having-anticipated-outcome/22660/5 "2016-09-28T18:41:54Z")

</div>

That’s what I thought but I get an error when trying to pass it to an ebs volume attachment.

- name: Create and attach ec2 volume  
ec2\_vol:  
instance: “{{ item.instance\_ids }}”  
volume\_size: “{{ volume\_size }}”  
volume\_type: “{{ volume\_type }}”  
device\_name: “/dev/xvd{{ drive }}”  
region: us-west-2  
with\_items: “{{ ec2a.results }}”  
when: ebs is defined

dumbledore@ansible1a:/etc/ansible \> ansible-playbook /etc/ansible/playbooks/db\_ec2.yml -e ‘env=qa num=1 ebs=true’

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 29, 2016, 4:26am UTC](https://forum.ansible.com/t/question-with-sequence-and-registered-variables-not-having-anticipated-outcome/22660/6 "2016-09-29T04:26:05Z")

</div>

> That's what I thought but I get an error when trying to pass it to an ebs  
> volume attachment.  
> - name: Create and attach ec2 volume  
> &nbsp;&nbsp;&nbsp;ec2\_vol:  
> &nbsp;&nbsp;&nbsp;&nbsp;instance: "{{ item.instance\_ids }}"

I have no knowledge about ec2\_vol but can instance: be a list?

> &nbsp;&nbsp;&nbsp;&nbsp;volume\_size: "{{ volume\_size }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;volume\_type: "{{ volume\_type }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;device\_name: "/dev/xvd{{ drive }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;region: us-west-2  
> &nbsp;&nbsp;&nbsp;with\_items: "{{ ec2a.results }}"  
> &nbsp;&nbsp;&nbsp;when: ebs is defined

\<snip\>

> u'group\_id': None, u'validate\_certs': True}}}) =\> {  
> &nbsp;&nbsp;&nbsp;&nbsp;"item": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"changed": true,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"instance\_ids": [  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"i-3a0e8822"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;],

Here you see that item.instance\_ids is a list.

\<snip\>

> "tagged\_instances": }, "msg": "Invalid id: \"['i-3a0e8822']\""}
> 
> Tells me invalid ID. I have wait set to wait for the instance to be  
> running.

I guess instance: can't be a list, if so you can get the first item in the list by using item.instance[0].
