Using with_sequence with register

1,394 views
Skip to first unread message

Imran Khan

unread,
Aug 20, 2014, 7:48:17 AM8/20/14
to ansible...@googlegroups.com
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

Imran Khan

unread,
Aug 20, 2014, 7:53:09 AM8/20/14
to ansible...@googlegroups.com
What I need is a register array!
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!

Michael DeHaan

unread,
Aug 20, 2014, 8:01:56 AM8/20/14
to ansible...@googlegroups.com
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.




--
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 ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/abdb1d0d-05e1-4c1a-ba4e-f63a56ca56b0%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Imran Khan

unread,
Aug 20, 2014, 8:11:20 AM8/20/14
to ansible...@googlegroups.com
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

Michael DeHaan

unread,
Aug 20, 2014, 8:16:36 AM8/20/14
to ansible...@googlegroups.com
I'm perhaps not understanding the underlying question, but:

{{ an_array | length }}

will tell you the length of an array in Jinja2




Imran Khan

unread,
Aug 20, 2014, 8:55:53 AM8/20/14
to ansible...@googlegroups.com
#$ 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

Michael DeHaan

unread,
Aug 20, 2014, 9:03:41 AM8/20/14
to ansible...@googlegroups.com
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.






Imran Khan

unread,
Aug 20, 2014, 9:18:46 AM8/20/14
to ansible...@googlegroups.com
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)

Alex Elent

unread,
Jun 18, 2015, 10:04:51 AM6/18/15
to ansible...@googlegroups.com
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}}"

Alex Elent

unread,
Jun 18, 2015, 1:18:15 PM6/18/15
to ansible...@googlegroups.com
Sorry my reply was slightly incorrect. You don't need "item.item|int -1". "0" would suffice and provide all items values.
Reply all
Reply to author
Forward
0 new messages