# Nested loop: referring to the value of the current item of the outer loop

**URL:** <https://forum.ansible.com/t/nested-loop-referring-to-the-value-of-the-current-item-of-the-outer-loop/17350>\
**Category:** Ansible Project\
**Created:** [January 22, 2015, 9:29am UTC](https://forum.ansible.com/t/nested-loop-referring-to-the-value-of-the-current-item-of-the-outer-loop/17350 "2015-01-22T09:29:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tadej\_Janez](https://avatars.discourse-cdn.com/v4/letter/t/f475e1/32.png) [@Tadej\_Janez](https://forum.ansible.com/u/Tadej_Janez)\
**Post date:** [January 22, 2015, 9:29am UTC](https://forum.ansible.com/t/nested-loop-referring-to-the-value-of-the-current-item-of-the-outer-loop/17350/1 "2015-01-22T09:29:41Z")

</div>

Hi!

I’m trying to develop a simple backup solution with Ansible.

My inventory file contains:  
[backup-hosts]  
backup-host1  
backup-host2

[backup-targets]  
backup-target1  
backup-target2  
backup-target3  
backup-target4

Each backup-target has a host\_vars file with contents similar to the following:

# list of users and their directories that should be backed up

backup\_target\_users\_dirs:

- user: “alice”  
dirs:
- “Maildir”
- “Important”
- user: “bob”  
dirs:
- “Maildir”
- “ProjectX”

I have a role that is played on each backup-host that contains  
a task to prepare directories for each backup target:

- name: prepare directories for each backup target  
file:  
path: “~/{{ item[0] }}/{{ item[1][‘dir’] }}”  
state: directory  
sudo: yes  
sudo\_user: “{{ backup\_host\_user }}”  
with\_nested:
- groups[“backup-targets”]
- hostvars[item[0]][“backup\_target\_users\_dirs”]

As it is, it doesn’t work. What I would like to achieve is that “item[0]” in  
the nested loop would refer to the value of the current item of the outer loop.  
How can I do that?

Thanks and best regards,  
Tadej

---

<div class="post-metadata">

**Author:** ![Tadej\_Janez](https://avatars.discourse-cdn.com/v4/letter/t/f475e1/32.png) [@Tadej\_Janez](https://forum.ansible.com/u/Tadej_Janez)\
**Post date:** [January 27, 2015, 10:43pm UTC](https://forum.ansible.com/t/nested-loop-referring-to-the-value-of-the-current-item-of-the-outer-loop/17350/2 "2015-01-27T22:43:40Z")

</div>

Hi!

Just a quick ping. Does anyone have any idea how I could solve the  
problem described below? Maybe a work-around?

Thanks and best regards,  
Tadej

---

<div class="post-metadata">

**Author:** ![Henning\_Eggers](https://avatars.discourse-cdn.com/v4/letter/h/5f9b8f/32.png) [@Henning\_Eggers](https://forum.ansible.com/u/Henning_Eggers)\
**Post date:** [January 29, 2015, 2:43pm UTC](https://forum.ansible.com/t/nested-loop-referring-to-the-value-of-the-current-item-of-the-outer-loop/17350/3 "2015-01-29T14:43:37Z")

</div>

Hi,

I think you might want to use with\_subelements instead of with\_nested.

[http://docs.ansible.com/playbooks\_loops.html#looping-over-subelements](http://docs.ansible.com/playbooks_loops.html#looping-over-subelements)

Cheers,  
Henning

---

<div class="post-metadata">

**Author:** ![michael1](https://avatars.discourse-cdn.com/v4/letter/m/eada6e/32.png) [@michael1](https://forum.ansible.com/u/michael1)\
**Post date:** [February 1, 2015, 11:39am UTC](https://forum.ansible.com/t/nested-loop-referring-to-the-value-of-the-current-item-of-the-outer-loop/17350/4 "2015-02-01T11:39:58Z")

</div>

Hi,

I was hoping to get more details from you regarding this issue. I appreciate what you’re trying to do at Ansible’s level, but what’s going to do the data transfer? If it’s rsync, then I would like to propose a new approach to this, but if it’s not, I need to know before I can write some example code for you.

Cheers,

Mike.

---

<div class="post-metadata">

**Author:** ![Tadej\_Janez](https://avatars.discourse-cdn.com/v4/letter/t/f475e1/32.png) [@Tadej\_Janez](https://forum.ansible.com/u/Tadej_Janez)\
**Post date:** [February 1, 2015, 12:57pm UTC](https://forum.ansible.com/t/nested-loop-referring-to-the-value-of-the-current-item-of-the-outer-loop/17350/5 "2015-02-01T12:57:17Z")

</div>

Hi Henning,

> I think you might want to use with\_subelements instead of with\_nested.

> [http://docs.ansible.com/playbooks\_loops.html#looping-over-subelements](http://docs.ansible.com/playbooks_loops.html#looping-over-subelements)

thanks for your advice.

Unfortunately, I think 'with\_subelements' is not powerful enough to do  
what I want.  
As explained in the documentation:  
"Subelements walks a list of hashes (aka dictionaries) and then  
traverses a list with a given key inside of those records."

What I want to do is a bit different. I need to extract information from  
the 'hostvars' hash and I only want to extract the information for hosts  
belonging to a specific group (i.e. groups["backup-targets"]).

Regards,  
Tadej

---

<div class="post-metadata">

**Author:** ![Tadej\_Janez](https://avatars.discourse-cdn.com/v4/letter/t/f475e1/32.png) [@Tadej\_Janez](https://forum.ansible.com/u/Tadej_Janez)\
**Post date:** [February 1, 2015, 1:17pm UTC](https://forum.ansible.com/t/nested-loop-referring-to-the-value-of-the-current-item-of-the-outer-loop/17350/6 "2015-02-01T13:17:21Z")

</div>

Hi,

> I was hoping to get more details from you regarding this issue. I  
> appreciate what you're trying to do at Ansible's level, but what's  
> going to do the data transfer? If it's rsync, then I would like to  
> propose a new approach to this, but if it's not, I need to know before  
> I can write some example code for you.

Yes, I intend to use rsync.

My plan is to use a Bash script template that uses rsync and use Ansible  
to fill in the directories that need to be rsync-ed. I intend to use a  
separate Bash script for each backup target.  
Then I plan to use another Ansible task that will set up a cron job for  
each of these backup targets.

Of course, I'm eager to hear new approaches to the problem I'm tackling.

Regards,  
Tadej

---

<div class="post-metadata">

**Author:** ![michael1](https://avatars.discourse-cdn.com/v4/letter/m/eada6e/32.png) [@michael1](https://forum.ansible.com/u/michael1)\
**Post date:** [February 1, 2015, 1:41pm UTC](https://forum.ansible.com/t/nested-loop-referring-to-the-value-of-the-current-item-of-the-outer-loop/17350/7 "2015-02-01T13:41:52Z")

</div>

Hi,

I think your approach has been overthought, a very common issue everyone runs into when designing a backup solution. I also think you should use an existing backup solution, such as [http://www.rsnapshot.org](http://www.rsnapshot.org). Ideally you want a solution that can be configured from a central point and left to do its job.

One thing to remember, also, is that Ansible is a Configuration Management suite. It’s not designed to replace a scripting or programming language - it simply can’t do the same things. Instead, use Ansible to configure a piece of software, like rsnapshot, and then maintain this state to fit your needs.

I believe you need to simplify the data structures in your example cases. Also, instead of having a list of users to backup along with their individual backup targets, use rsync’s ‘–exclude-from’ and ‘–include-from’ to build a list of regular expressions or globs for matching what you do and do not want to backup, like:

`+/home/alice/* +/home/bob/my_files/*.txt -/root/*`

If you do this, you just need a set of templates for populating the exclude and include files, which you can then feed to rsync, alone with the remote system’s (IP) address. Working with these patterns means you’re not dealing with complex data structures from within Ansible, but instead have something simple such as this:

`  
backup\_target\_systems:

- system: “localdev”  
active: true  
backup\_include\_patterns:
- “/home/alice/”
- “/home/bob/files/”  
backup\_exclude\_patterns:
- “/root/”  
`

I hope this has helped. Please let me know if you need any further assistance.

- M
