The blog about containerisation, virtual machines and useful shell snippets and findings

Ansible add entries to .htpassword

There are several ways to do this:

  1. Use template action and use template file
  2. Use array of entries and loop with lininfile command
  3. Use https://docs.ansible.com/ansible/latest/collections/community/general/htpasswd_module.html htpasswd module.

I decided to go with approach #2.
in variables

htpasswd:
  - user1:password1
  - user2:password2

in the playbook

- name: set password file
      lineinfile:
        path: "{{ webroot }}/shared/.htpasswd"
        line: "{{ item }}"
        create: yes
      when: oxid.configuration == "production"
      loop: "{{ htpasswd }}"