# Checking folder exists

**URL:** <https://forum.ansible.com/t/checking-folder-exists/14893>\
**Category:** Ansible Project\
**Created:** [March 29, 2014, 10:18pm UTC](https://forum.ansible.com/t/checking-folder-exists/14893 "2014-03-29T22:18:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francisco\_Reyes](https://avatars.discourse-cdn.com/v4/letter/f/b77776/32.png) [@Francisco\_Reyes](https://forum.ansible.com/u/Francisco_Reyes)\
**Post date:** [March 29, 2014, 10:18pm UTC](https://forum.ansible.com/t/checking-folder-exists/14893/1 "2014-03-29T22:18:23Z")

</div>

New to ansible.

Trying to create a playbook to do the following:  
1 Checks if a user exists and if not create  
2 Check if /home/user/.ssh exists and if not create it  
3 Copy ssh keys for the user

Having a problem with part 2

I have a variable file as follows

Ops:

- User1:  
uname: user1  
passwd: ValidHash
- User2:  
uname: user2  
passwd: ValidHash

Relevant part of playbook that is giving me trouble…

- name: Attempt to check .ssh folder exists  
action: shell test -d /home/{{item[‘uname’]}}/.ssh && echo “exists” || echo “”  
register: folder\_exists  
with\_items: Ops

- name: Make .ssh folder for each user  
action: shell mkdir /home/{{item[‘uname’]}}/.ssh  
with\_items: Ops  
when: not folder\_exists

The “when” check fails even though the folders do not exist. Run with -vv

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [March 31, 2014, 8:32pm UTC](https://forum.ansible.com/t/checking-folder-exists/14893/2 "2014-03-31T20:32:36Z")

</div>

Ansible modules are pretty much idempotent, so you shouldn’t need to check something and then create it…

What you want to do here is…

1. Create a user
2. Add the .ssh directory
3. copy the .ssh keys

No need to check things (Ansible does that for you).

Adam

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [March 31, 2014, 8:49pm UTC](https://forum.ansible.com/t/checking-folder-exists/14893/3 "2014-03-31T20:49:34Z")

</div>

The UserX: variable portion wasn’t used so I’ve removed it…

> Ops:
> 
> - uname: user1

> passwd: ValidHash

> - uname: user2  
> passwd: ValidHash

Then your tasks would be something like this…

- user: name={{ item.uname }} state=present update\_password=on\_create password={{ item.password }} createhome=yes  
with\_items: Ops

- file: path=/home/{{ item.uname }}/.ssh state=directory mode=0700 owner={{item.uname}}  
with\_items: Ops

I’m not sure what files you would want to copy… You might prefer to use generate\_ssh\_key in the user task and then copy an authorized key using authorized\_key… I’m not entirely sure which fits best with what you want…

Your problem earlier was that register will create folder\_exists… And you probably shouldn’t be using shell actions for things that real modules exist for. Shell is not idempotent, but the other modules are. So if they don’t need to do anything they won’t.

Adam

---

<div class="post-metadata">

**Author:** ![Francisco\_Reyes](https://avatars.discourse-cdn.com/v4/letter/f/b77776/32.png) [@Francisco\_Reyes](https://forum.ansible.com/u/Francisco_Reyes)\
**Post date:** [March 31, 2014, 8:53pm UTC](https://forum.ansible.com/t/checking-folder-exists/14893/4 "2014-03-31T20:53:19Z")

</div>

What module should I use for that?  
Tried calling mkdir from the shell command, which fails when the folder already exists.  
Tried looking at the Files Modules, but did not see a command to make directories.

This is how I was trying to create the folder  
action: shell mkdir /home/{{item[‘uname’]}}/.ssh

stderr: mkdir: cannot create directory `/home/user1/.ssh’: File exists

Thanks for any additional pointers.

---

<div class="post-metadata">

**Author:** ![Francisco\_Reyes](https://avatars.discourse-cdn.com/v4/letter/f/b77776/32.png) [@Francisco\_Reyes](https://forum.ansible.com/u/Francisco_Reyes)\
**Post date:** [March 31, 2014, 9:01pm UTC](https://forum.ansible.com/t/checking-folder-exists/14893/5 "2014-03-31T21:01:29Z")

</div>

I was writing a reply to your previous post when you sent this…

> - file: path=/home/{{ item.uname }}/.ssh state=directory mode=0700 owner={{item.uname}}  
> with\_items: Ops

That is what I was looking for thanks.

> > And you probably shouldn’t be using shell actions for things that real modules exist for.

Agree. Still going over the documentation and figuring out what modules exist. Could not find a way to make a folder with a module.

Thanks again.

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [March 31, 2014, 10:26pm UTC](https://forum.ansible.com/t/checking-folder-exists/14893/6 "2014-03-31T22:26:24Z")

</div>

You might want to look into the command module rather than shell (a bit safer but also a bit more limited) and the creates= argument to it…

But yes, you use the file module to create directories as well as files. It’s worth looking through the whole modules documentation index every now and again to see what is in there, and what has been added.

I hope that this helps,  
Adam

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [April 1, 2014, 12:16am UTC](https://forum.ansible.com/t/checking-folder-exists/14893/7 "2014-04-01T00:16:32Z")

</div>

the authorized\_keys module takes care of all of this if you let it manage the .ssh dir.

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [April 1, 2014, 5:10pm UTC](https://forum.ansible.com/t/checking-folder-exists/14893/8 "2014-04-01T17:10:09Z")

</div>

I wasn’t sure from the original description whether the intention is to set up the authorized\_keys file or to add the users SSH keys into their home directory… Authorized\_keys works for one but not the other.

Adam
