Ansible: Create a new user account and password using vars_promt

It will asks for the username and password for a new account right after you run the ansible playbook.

Ansible hosts.yaml

- hosts: hasnan1x
  roles:
    - common

  vars_prompt:
  - name: "group_account"
    prompt: "Enter a new account for the group"
    private: no

  - name: "group_password"
    prompt: "Enter a new password for the group"
    private: yes
    encrypt: "md5_crypt" #need to have python-passlib installed in local machine before we can use it
    confirm: yes
    salt_size: 7

Place this somewhere in your task in "common" roles.

- name: create new group account
  user: name={{group_account}} password={{group_password}}
  tags: group-pass

Test

ansible-playbook -i hosts sites.yaml -u root --ask-pass --tags group-pass

Ansible: running command only if the file doesn’t exist

In order to achieve this, you have to use Ansible module, ‘command’ . You also have to make sure that the file that you want to execute is already in the remote host.

Command module has ‘creates’ parameter that checks if the folder already been created, if it doesn’t it will run this command, otherwise Ansible will skip this command.

 
- name: Install Intel Compilers (Skip if it's already been installed) 
  command: /tmp/install_compilers.sh creates=/opt/intel