How to use Ansible to log in a machine just for creating a home directory

33 views
Skip to first unread message

zeld...@gmail.com

unread,
Nov 13, 2014, 1:09:43 PM11/13/14
to ansible...@googlegroups.com
I have a virtual machine that when first login as a user, it will create the home directory for the user if it doesn't have one.

So just sth like, 
$ ssh username@myhostname
$ Creating directory '/home/username'.

How could I use Ansible to do that(just login in order to generate a home directory)? 

I tried this in playbook:
---
- hosts: all
  gather_facts: no
  remote_user: theusername
  tasks:
    - name: Auto generate user home directory
      shell: /usr/bin/whoami

It threw an exception saying "ControlPath too long". But when I do the same thing with a user that has a home directory in the machine, it will succeed.
The control path is: 'ControlPath=/net/myhostname/userhome/myusername/.ansible/cp/ansible-ssh-%h-%p-%r'

Toshio Kuratomi

unread,
Nov 16, 2014, 11:33:15 AM11/16/14
to ansible...@googlegroups.com
There are some ansible config values that can control where the
ControlPath file is created and the remote temporary path but overall
it may be easier to use the shell module on localhost for this

- hosts: all
vars:
- new_username: badger
gather_facts: no
tasks:
- name: Auto generate user home directory
command: ssh {{ new_username }}@{{ inventory_hostname }} /usr//bin/whoami
delegate_to: localhost


Note that the above playbook assumes that your inventory_hostnames are
all resolvable to real hostnames. If that's not the case you may need
to make some modifications (for instance, create a group in the
inventory that contains the resolvable hostnames of the machines
you're interested in and use that group in the hosts: field)

-Toshio
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/228dc9ac-d213-4afb-a3c1-6fffec6b06a9%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Michael DeHaan

unread,
Nov 17, 2014, 3:52:36 PM11/17/14
to ansible...@googlegroups.com
The user module has a flag to create the homedir.

In any case, manually executing SSH with ansible is weird, and not something I'd want to see in most playbooks, Toshio here I think was proposing this for a debugging case, but don't do this :)

You could of course simply execute the whoami with the shell module like so, if you wanted:

- hosts: all
  user: username
  tasks:
    - shell: whoami




Reply all
Reply to author
Forward
0 new messages