Better late than never but I ran into this same thing and this is how I 
solved it with example code:

    - name: Launch instances
      ec2:
        user_data: "{{ lookup('file', 'user_data.yml') }}"
        keypair: "{{ keypair }}"
        group_id: "{{ security_group }}"
        instance_type: "{{ instance_type }}"
        image: "{{ image }}"
        count: 1
        region: "{{ region }}"
        wait: true
        volumes:
        - device_name: /dev/sdb
          ephemeral: ephemeral0
        - device_name: /dev/sdc
          ephemeral: ephemeral1
      with_sequence: count=3
      register: inst

    - debug: msg="{{item.instances[item.item|int - 1].id}}"
      with_items: "{{inst.results}}"




On Wednesday, August 20, 2014 at 6:18:46 AM UTC-7, Imran Khan wrote:
>
> I just need to write some values into a file in a specified format, so 
> that I don't have to individually access each VM and make changes to the 
> file. The rest of what happens is not ansible's concern. All I would like 
> to know is how can I run a task in a power of 2 loop (when the loop value 
> is entered by the user) and save some values in a register , values which 
> can be permanently stored in some array and be accessed in any iteration 
> (Tasks run multiple times on each individual VM due to *serial: 1* 
> constraint)
>
> On Wednesday, August 20, 2014 6:03:41 AM UTC-7, Michael DeHaan wrote:
>>
>> Sounds like this is more of a question about asking ansible to do 
>> "static" DHCP reservations for you, and reserve a MAC for a future host.
>>
>> Ansible's not really a programming language.  Can you just use DHCP 
>> dynamic ranges?  I'm not following the "send to vm" parts and why you would 
>> need
>> to set that up there.
>>
>> If you are attempting to manage a large number of static IPs and auto-set 
>> up the networking configurations for a bare metal environment, something 
>> like Cobbler can be a good fit.
>>
>>
>>
>>
>>
>>
>> On Wed, Aug 20, 2014 at 8:55 AM, Imran Khan <[email protected]> wrote:
>>
>>> #$ cat bbb.yml
>>> ---
>>> - hosts: localhost
>>>   gather_facts: False
>>>
>>>   vars_prompt:
>>>     - name: epcrange
>>>       prompt: Enter the number of EPCs that you want to configure
>>>       private: False
>>>       default: "1"
>>>     - name: serverrange
>>>       prompt: Enter the number of Cleints that you want to configure
>>>       private: False
>>>       defualt: "1"
>>>
>>>   pre_tasks:
>>>     - name: Set EPC node id variables
>>>       set_fact:
>>>        # start: "{{ epcrange.split('..')[0] }}"
>>>       # stop: "{{ serverrange.split('..')[-1] }}"
>>>         start: 1
>>>         stop: "{{epcrange}}"
>>>     - name: "Add EPC hosts:"
>>>       add_host: name="vm{{item}}" groups=just_created_epcs
>>>       with_sequence: "start={{start}} end={{stop}} "
>>>     - name: Set Client node id variable
>>>       set_fact:
>>>         start: 1
>>>         stop: "{{serverrange}}"
>>>     - name: "Add Client hosts:"
>>>       add_host: name="vm{{item}}" groups=just_created_clients
>>>       with_sequence: "start={{start}} end={{stop}} "
>>>
>>>
>>>
>>> - hosts: just_created_epcs
>>>   serial: 1
>>>   gather_facts: False
>>>   tasks:
>>>
>>>   serial: 1
>>>   gather_facts: False
>>>
>>>   vars_prompt:
>>>     - name: ServerIP
>>>       prompt: Enter the ServerIP to replace
>>>       private: False
>>>       default: '11.11.4.10'
>>>
>>>     - name: ServerIPRange
>>>       prompt: Enter the ServerIP range
>>>       private: False
>>>       default: '128'
>>>
>>>   vars:
>>>     - String1: '"ServerIP"'
>>>     - String2: '"ServerIPRange"'
>>>
>>> #
>>> #
>>>     - name: Replace ServerIP in config_file
>>>       shell: cd /home/imran/Desktop/tobefetched; sed -i '/{{String1}}/ c 
>>> "ServerIP" ':' "{{ServerIP}}" ' config_file
>>>       when: inventory_hostname == "vm1"  # workaround, run_once won't 
>>> work
>>>       ignore_errors: yes
>>>
>>>
>>>     - name: Replace ServerIPRange in config_file
>>>       shell: cd /home/imran/Desktop/tobefetched; sed -i '/{{String2}}/ c 
>>> "ServerIPRange" ':' "{{ServerIPRange}}" ' config_file
>>>       when: inventory_hostname == "vm1"    # workaround, run_once won't 
>>> work
>>>       ignore_errors: yes
>>>
>>> #The first time ,the address that the user enters is copied onto 
>>> config_file on remote server as is
>>> #Now I need to do some mathematics here and so that I can write a value 
>>> into the config_file of the remote machine based on my calculations here.
>>> #Something like: The next VM(s) gets values written into the config_file 
>>> according to some formula (explained in c++ below)
>>>
>>> int x=0;
>>> for (int i=1; i<epcrange; i+=pow(2,x))                                   
>>>                                 //pow is a math function with pow(2,x)= 2^x
>>> {
>>> New_LastIPOctet = Last_ServerIPOctet +ServerIPRange/epcrange;           
>>> //user always enters values in the power of 2, Last_ServerIPOctet is 
>>> accessed from the IP that the user enters
>>> NewIP=append (ServerIP, New_LastIPOctet);                               
>>>                    //some function that appends a new value to the last 
>>> IPV4 octet of a previous IPV4 address.
>>> SendToFileOnRemoteVM = send ();                                         
>>>                          //send to my remote vm and make changes to 
>>> config_file
>>> x++;
>>> }
>>> //all values are integers
>>>
>>> As we can see here, epc range is decided by the user, so the loop is not 
>>> hard-coded.
>>> I am sorry for using c++ and jumbling it all up, but there is no easy 
>>> way to explain this
>>>
>>>
>>>
>>> On Wednesday, August 20, 2014 5:16:36 AM UTC-7, Michael DeHaan wrote:
>>>
>>>> I'm perhaps not understanding the underlying question, but:
>>>>
>>>> {{ an_array | length }}
>>>>
>>>> will tell you the length of an array in Jinja2
>>>>
>>>>
>>>>
>>>>
>>>> On Wed, Aug 20, 2014 at 8:11 AM, Imran Khan <[email protected]> 
>>>> wrote:
>>>>
>>>>> Thanks, but to use this I will need to know how many times this task 
>>>>> will run. That is, in-fact, decided by the person running the playbook, 
>>>>> at 
>>>>> run time
>>>>>
>>>>>
>>>>> On Wednesday, August 20, 2014 5:01:56 AM UTC-7, Michael DeHaan wrote:
>>>>>
>>>>>> This is easy to experiment with, by constructing a simple playbook:
>>>>>>
>>>>>> - shell: echo {{ foo }}
>>>>>>   with_items: [1,2,3,4,5]
>>>>>>   register: result
>>>>>>
>>>>>> - debug: var=result
>>>>>>
>>>>>> You will see that the result variable does get registered as an array.
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> On Wed, Aug 20, 2014 at 7:53 AM, Imran Khan <[email protected]> 
>>>>>> wrote:
>>>>>>
>>>>>>> What I need is a register array!
>>>>>>>
>>>>>>>
>>>>>>> On Wednesday, August 20, 2014 4:48:17 AM UTC-7, Imran Khan wrote:
>>>>>>>
>>>>>>>> I would like to know that If I run a task in a loop and save the 
>>>>>>>> value in a register, how can I save the values so that I have them 
>>>>>>>> accessible when I need them
>>>>>>>> P.S: I am running my playbook using *serial: 1 .* So, when I 
>>>>>>>> iterate a task, say 5 times, it should save them all seperately, so 
>>>>>>>> that 
>>>>>>>> when the playbook runs serially the second or third time around, I can 
>>>>>>>> use 
>>>>>>>> the registered values as needed. 
>>>>>>>> Example:
>>>>>>>>
>>>>>>>> - hosts: just_created_clients
>>>>>>>>   serial: 1
>>>>>>>>   gather_facts: False
>>>>>>>>  
>>>>>>>> #
>>>>>>>> #
>>>>>>>>   tasks:
>>>>>>>> #
>>>>>>>> #
>>>>>>>>     - name: Check if the changes to config_file were successful
>>>>>>>>       shell: cat /home/imran/Desktop/tobefetched/config_file
>>>>>>>>       with_sequence: count=5      
>>>>>>>>        register: my_content
>>>>>>>>       ignore_errors: yes
>>>>>>>> #
>>>>>>>> #
>>>>>>>>
>>>>>>>>
>>>>>>>> Now, how do I access the values in the register(s) ? Note: I will 
>>>>>>>> not be immediately using these values, so can't have a task 
>>>>>>>> immediately 
>>>>>>>> following this task, which uses the results of this register 
>>>>>>>> sequentially. 
>>>>>>>> What I need is a register array!
>>>>>>>>
>>>>>>>  -- 
>>>>>>> You received this message because you are subscribed to the Google 
>>>>>>> Groups "Ansible Project" group.
>>>>>>> To unsubscribe from this group and stop receiving emails from it, 
>>>>>>> send an email to [email protected].
>>>>>>> To post to this group, send email to [email protected].
>>>>>>> To view this discussion on the web visit 
>>>>>>> https://groups.google.com/d/msgid/ansible-project/abdb1d0d-
>>>>>>> 05e1-4c1a-ba4e-f63a56ca56b0%40googlegroups.com 
>>>>>>> <https://groups.google.com/d/msgid/ansible-project/abdb1d0d-05e1-4c1a-ba4e-f63a56ca56b0%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>>>> .
>>>>>>>
>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>
>>>>>>
>>>>>>  -- 
>>>>> You received this message because you are subscribed to the Google 
>>>>> Groups "Ansible Project" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send 
>>>>> an email to [email protected].
>>>>> To post to this group, send email to [email protected].
>>>>> To view this discussion on the web visit https://groups.google.com/d/
>>>>> msgid/ansible-project/06e4b287-486b-480a-8c4e-
>>>>> 3f26c2c8d145%40googlegroups.com 
>>>>> <https://groups.google.com/d/msgid/ansible-project/06e4b287-486b-480a-8c4e-3f26c2c8d145%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>>
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>
>>>>  -- 
>>> You received this message because you are subscribed to the Google 
>>> Groups "Ansible Project" group.
>>> To unsubscribe from this group and stop receiving emails from it, send 
>>> an email to [email protected].
>>> To post to this group, send email to [email protected].
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/ansible-project/2cb93c6a-e413-49c4-8769-1f2394df444f%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/ansible-project/2cb93c6a-e413-49c4-8769-1f2394df444f%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/64f719df-7ee8-4a20-b530-17e2ef2a4d4d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to