Scenario: You want to create a user in Linux, and set a Samba password for it, all from Ansible:
Notes:
- This will not update the Samba password if you change the variable.
To create the encrypted vault string, run:
ansible-vault encrypt_string --ask-vault-password 'some_password'
- name: Create OS group
  group:
    name: smbgroup
    state: present
    system: no
- name: Create OS user
  user:
    name: smbuser
    group: smbgroup
    createhome: no
    system: no
    state: present
    shell: /sbin/nologin
- name: Fetch current smbpasswd users
  command: /usr/bin/pdbedit -L
  register: pdb_users
- name: Set Samba password for smbuser
  shell: echo '{{ smbuser_password }}' | /usr/bin/smbpasswd -s -a smbuser
  when: pdb_users.stdout.find('smbuser') == -1
  vars:
    smbuser_password: !vault |
          $ANSIBLE_VAULT;1.1;AES256
          123456encryptedblah
          goeshere123456
You don't need a
shellcommand with anechostatement here. You can use acommandtask with thestdindirective:Note that we need to provide the password twice, because
smbpasswdwants both the password and a confirmation.