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

**URL:** <https://forum.ansible.com/t/how-to-use-ansible-to-log-in-a-machine-just-for-creating-a-home-directory/16776>\
**Category:** Ansible Project\
**Created:** [November 13, 2014, 6:09pm UTC](https://forum.ansible.com/t/how-to-use-ansible-to-log-in-a-machine-just-for-creating-a-home-directory/16776 "2014-11-13T18:09:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zelda0102](https://avatars.discourse-cdn.com/v4/letter/z/3da27b/32.png) [@zelda0102](https://forum.ansible.com/u/zelda0102)\
**Post date:** [November 13, 2014, 6:09pm UTC](https://forum.ansible.com/t/how-to-use-ansible-to-log-in-a-machine-just-for-creating-a-home-directory/16776/1 "2014-11-13T18:09:43Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![Toshio\_Kuratomi](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Toshio\_Kuratomi](https://forum.ansible.com/u/Toshio_Kuratomi)\
**Post date:** [November 16, 2014, 4:33pm UTC](https://forum.ansible.com/t/how-to-use-ansible-to-log-in-a-machine-just-for-creating-a-home-directory/16776/2 "2014-11-16T16:33:11Z")

</div>

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  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;- new\_username: badger  
&nbsp;&nbsp;gather\_facts: no  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: Auto generate user home directory  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: ssh {{ new\_username }}@{{ inventory\_hostname }} /usr//bin/whoami  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [November 17, 2014, 8:52pm UTC](https://forum.ansible.com/t/how-to-use-ansible-to-log-in-a-machine-just-for-creating-a-home-directory/16776/3 "2014-11-17T20:52:33Z")

</div>

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
