# Looping through a dictionary to create links

**URL:** <https://forum.ansible.com/t/looping-through-a-dictionary-to-create-links/30876>\
**Category:** Ansible Project\
**Created:** [July 26, 2019, 3:37am UTC](https://forum.ansible.com/t/looping-through-a-dictionary-to-create-links/30876 "2019-07-26T03:37:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaron\_Hicks](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@Aaron\_Hicks](https://forum.ansible.com/u/Aaron_Hicks)\
**Post date:** [July 26, 2019, 3:37am UTC](https://forum.ansible.com/t/looping-through-a-dictionary-to-create-links/30876/1 "2019-07-26T03:37:06Z")

</div>

Hello the list,

I have a bunch of machines with some shared file systems. Some of the file systems are not mounted on all the machines. For the filesystems that are mounted, I’d like to check/create a bunch of symbolic links to those filesystems from various places in the filesystem. Some filesystems do not have links (yet) but we may do some other things with them later.

So given a YAML inventory file:

all:  
vars:  
filesystems:  
/sharedfs1:

- source: /sharedfs1/filesets/home  
target: /home
- source: /sharedfs1/filesets/foo  
target: /opt/foo
- source: /sharedfs1/filesets/baa  
target: /opt/baa
- source: /sharedfs1/filesets/lmod  
target: /opt/lmod
- source: /scale\_wlg\_persistent/filesets/baz  
target: /quack/baz  
/sharedfs2:
- source: /sharedfs2/filesets/scratch  
target: /quack/scratch  
/sharedfs3:  
/sharedfs4:  
children:  
hostlist:  
hosts:  
host1:

host2:

And the following play, which checks the mount point exists

---

<div class="post-metadata">

**Author:** ![Aaron\_Hicks](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@Aaron\_Hicks](https://forum.ansible.com/u/Aaron_Hicks)\
**Post date:** [July 26, 2019, 3:44am UTC](https://forum.ansible.com/t/looping-through-a-dictionary-to-create-links/30876/2 "2019-07-26T03:44:27Z")

</div>

The Python equivalent I’m trying to make is:

for sharedfs in filesystems:  
if os.path.exists(sharedfs):  
for link in filesystems[sharedfs]:  
os.symlink(link[‘source’]. link[‘target’])

---

<div class="post-metadata">

**Author:** ![Aaron\_Hicks](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@Aaron\_Hicks](https://forum.ansible.com/u/Aaron_Hicks)\
**Post date:** [July 29, 2019, 2:33am UTC](https://forum.ansible.com/t/looping-through-a-dictionary-to-create-links/30876/3 "2019-07-29T02:33:15Z")

</div>

Well, I have a solution but it’s very [https://www.goodreads.com/quotes/7053458-but-i-am-very-poorly-today-very-stupid](https://www.goodreads.com/quotes/7053458-but-i-am-very-poorly-today-very-stupid)

I’ve had to restructure the inventory:

all:  
vars:  
filesystems:

- path: /sharedfs1  
links:
- src: /sharedfs1/filesets/home  
dest: /home
- src: /sharedfs1/filesets/foo  
dest: /opt/foo
- src: /sharedfs1/filesets/baa  
dest: /opt/baa
- src: /sharedfs1/filesets/lmod  
dest: /opt/lmod
- src: /sharedfs1/filesets/baz  
dest: /quack/baz
- path: /sharedfs2  
links:
- src: /sharedfs2/filesets/nobackup  
dest: /quack/nobackup
- path: /sharefds3
- path: /sharedfs4  
children:  
hostlist:  
hosts:  
host1:  
host2:

And then the playbook looks like, which does a bit more should/shouldn’t testing than initially asked for
