Checking folder exists

488 views
Skip to first unread message

Francisco Reyes

unread,
Mar 29, 2014, 6:18:23 PM3/29/14
to ansible...@googlegroups.com
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
-----------------------
TASK: [Attempt to check .ssh folder exists] *********************************** 
<107.170.122.172> REMOTE_MODULE command test -d /home/user1/.ssh && echo “exists” || echo “” #USE_SHELL
changed: [ansible-test.wordjacktech.com] => (item={'passwd': '$6$rounds=100000$0mTt0BWi27Wkfbq.$nni86oU/8EIVswOll5o5YzxJzsKuy8Wx7im1VUhpWX/OMQsdhEoWV9vqrLJOpL8PWhgmFmQoqQTzqiqRqtXUh0', 'uname': 'user1', 'francisco': None}) => {"changed": true, "cmd": "test -d /home/freyes/.ssh && echo \u201cexists\u201d || echo \u201c\u201d ", "delta": "0:00:00.002813", "end": "2014-03-29 18:11:06.482901", "item": {"francisco": null, "passwd": "$6$rounds=100000$0mTt0BWi27Wkfbq.$nni86oU/8EIVswOll5o5YzxJzsKuy8Wx7im1VUhpWX/OMQsdhEoWV9vqrLJOpL8PWhgmFmQoqQTzqiqRqtXUh0", "uname": "freyes"}, "rc": 0, "start": "2014-03-29 18:11:06.480088", "stderr": "", "stdout": ""}
<107.170.122.172> REMOTE_MODULE command test -d /home/user2/.ssh && echo “exists” || echo “” #USE_SHELL
changed: [ansible-test.wordjacktech.com] => (item={'passwd': '$6$rounds=100000$O.9Oyl0Xfar43LvS$X5P1RBXxwHBd4ktnq4sAmMLYkc8/m/WcS5hJKGqT/FQJYolSFHNSLrhfaiL4.dPtolM/p3aHOhMse88Cx.74G0', 'user2': None, 'uname': 'kelvin'}) => {"changed": true, "cmd": "test -d /home/kelvin/.ssh && echo \u201cexists\u201d || echo \u201c\u201d ", "delta": "0:00:00.002727", "end": "2014-03-29 18:11:08.438441", "item": {"kelvin": null, "passwd": "$6$rounds=100000$O.9Oyl0Xfar43LvS$X5P1RBXxwHBd4ktnq4sAmMLYkc8/m/WcS5hJKGqT/FQJYolSFHNSLrhfaiL4.dPtolM/p3aHOhMse88Cx.74G0", "uname": "kelvin"}, "rc": 0, "start": "2014-03-29 18:11:08.435714", "stderr": "", "stdout": ""}

TASK: [Make .ssh folder for each user] **************************************** 
skipping: [ansible-test.wordjacktech.com] => (item={'passwd': '$6$rounds=100000$0mTt0BWi27Wkfbq.$nni86oU/8EIVswOll5o5YzxJzsKuy8Wx7im1VUhpWX/OMQsdhEoWV9vqrLJOpL8PWhgmFmQoqQTzqiqRqtXUh0', 'uname': 'user1', 'francisco': None})
skipping: [ansible-test.wordjacktech.com] => (item={'passwd': '$6$rounds=100000$O.9Oyl0Xfar43LvS$X5P1RBXxwHBd4ktnq4sAmMLYkc8/m/WcS5hJKGqT/FQJYolSFHNSLrhfaiL4.dPtolM/p3aHOhMse88Cx.74G0', 'user2': None, 'uname': 'kelvin'})
-----------------------

The .ssh folder is not getting created.
Tried changing from 
    when: not folder_exists
to
    when: folder_exists

and the folders get created. However on the second run it tries again to create the folders.
So either the folders are never created or try to be created every time. A little puzzled.

Any suggestions?

Adam Morris

unread,
Mar 31, 2014, 4:32:36 PM3/31/14
to ansible...@googlegroups.com


On Saturday, March 29, 2014 3:18:23 PM UTC-7, Francisco Reyes wrote:
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



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 

Adam Morris

unread,
Mar 31, 2014, 4:49:34 PM3/31/14
to ansible...@googlegroups.com
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


Francisco Reyes

unread,
Mar 31, 2014, 4:53:19 PM3/31/14
to ansible...@googlegroups.com
On Monday, March 31, 2014 4:32:36 PM UTC-4, Adam Morris wrote:
What you want to do here is....
....
2) Add the .ssh directory 

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.

Francisco Reyes

unread,
Mar 31, 2014, 5:01:29 PM3/31/14
to ansible...@googlegroups.com
On Monday, March 31, 2014 4:49:34 PM UTC-4, Adam Morris wrote:

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.
 

Adam Morris

unread,
Mar 31, 2014, 6:26:24 PM3/31/14
to ansible...@googlegroups.com


On Monday, March 31, 2014 1:53:19 PM UTC-7, Francisco Reyes wrote:
On Monday, March 31, 2014 4:32:36 PM UTC-4, Adam Morris wrote:
What you want to do here is....
....
2) Add the .ssh directory 

What module should I use for that?
Tried calling mkdir from the shell command, which fails when the folder already exists.

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

Brian Coca

unread,
Mar 31, 2014, 8:16:32 PM3/31/14
to ansible...@googlegroups.com
the authorized_keys module takes care of all of this if you let it manage the .ssh dir.

Adam Morris

unread,
Apr 1, 2014, 1:10:09 PM4/1/14
to ansible...@googlegroups.com


On Monday, March 31, 2014 5:16:32 PM UTC-7, Brian Coca wrote:
the authorized_keys module takes care of all of this if you let it manage the .ssh dir.

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 
Reply all
Reply to author
Forward
0 new messages