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

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:

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

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 :slight_smile:

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

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