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
What I need is a register array!
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.
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
I’m perhaps not understanding the underlying question, but:
{{ an_array | length }}
will tell you the length of an array in Jinja2
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.
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)
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}}”
Sorry my reply was slightly incorrect. You don’t need “item.item|int -1”. “0” would suffice and provide all items values.