ansible-modules-hashivault

repository·main·Indexed 19 days ago

https://github.com/terryhowe/ansible-modules-hashivault

Ansible modules and a lookup plugin for interacting with HashiCorp Vault. Version 5.6.1 provides capabilities to read and write secrets using modules like hashivault_secret and hashivault_read, as well as administrative tools for Vault initialization, sealing, and unsealing. The package requires hvac>=2.1.0 for ansible-galaxy installations and supports authentication via environment variables such as VAULT_ADDR and VAULT_TOKEN.

Tokens
1.8K
Snippets
11
Records
11
Agent score
16%

What's inside ansible-modules-hashivault

  1. Install ansible-modules-hashivault

    main

    You can install these Ansible modules for HashiCorp Vault using either pip or ansible-galaxy.

    Note: If you install via ansible-galaxy, the hashicorp lookup plugin will not work due to an Ansible limitation. In that case, fallback to the built-in hashi_vault lookup plugin.

    Requirements: hvac>=2.1.0 is required for the ansible-galaxy installation method.

    # Via pip
    pip install ansible-modules-hashivault
    
    # Via ansible-galaxy
    ansible-galaxy install 'git+https://github.com/TerryHowe/ansible-modules-hashivault.git'
  2. Run Vault in Docker for testing

    main

    You can use the automated functional tests to spin up an initialized Vault instance running in Docker. The ./start.sh script in the functional directory initializes Vault and generates a vaultenv.sh file containing the necessary environment variables for communication.

    cd ../functional
    ./start.sh
    source ./vaultenv.sh
  3. SSH into the sandbox to verify secrets

    main

    You can verify that the playbook successfully wrote the secret by SSHing into the sandbox container. Note that the container uses port 3022 and the identity file .ssh/id_rsa.

    Note: If you run the sandbox multiple times, you may need to clear the entry in your ~/.ssh/known_hosts file to avoid SSH host key verification errors.

    $ ssh -i .ssh/id_rsa -p 3022 root@127.0.0.1
    root@sandbox:~# cat /giant.txt 
    I smell the blood
  4. Read and write secrets using hashivault modules

    main

    The hashivault_secret module (which defaults to KV version 2) and hashivault_read can be used to manage secrets. By default, these assume the /secret mount point. To use a different mount point, specify the mount_point parameter.

    To support KV version 2 explicitly, use the version: 2 parameter.

    - hosts: localhost
      tasks:
        - hashivault_secret:
            secret: giant
            data:
                foo: foe
                fie: fum
        - hashivault_read:
            secret: giant
            key: fie
            version: 2
          register: vault_read
    
    # Using a custom mount point
    - hosts: localhost
      tasks:
        - hashivault_write:
            mount_point: /stories
            secret: stuart
            data:
                last: 'little'
        - hashivault_read:
            mount_point: /stories
            secret: stuart
            key: last
            version: 2
  5. Use the hashivault lookup plugin

    main

    You can retrieve secrets directly within Ansible templates or facts using the hashivault lookup plugin. You can specify the mount_point and version (for KV v2) within the lookup call.

    # Basic lookup
    - set_fact:
        looky: "{{lookup('hashivault', 'giant', 'foo', version=2)}}"
    
    # Lookup with custom mount point and version
    - set_fact:
        book: "{{lookup('hashivault', 'stuart', 'last', mount_point='/stories', version=2)}}"
  6. Manage Vault initialization, sealing, and unsealing

    main

    The modules provide administrative capabilities to manage the Vault lifecycle, including initialization, checking status, sealing, and unsealing.

    - hosts: localhost
      vars:
        vault_keys: "{{ lookup('env','VAULT_KEYS') }}"
      tasks:
        - hashivault_init:
            register: vault_init
    
        - hashivault_status:
            register: vault_status
    
        - block:
            - hashivault_seal:
                register: vault_seal
          when: "{{vault_status.status.sealed}} == False"
    
        - hashivault_unseal:
            keys: '{{vault_keys}}'
  7. Configure HashiCorp Vault authentication via environment variables

    main

    To authenticate to your HashiCorp Vault instance, you must export specific environment variables to the environment where you run Ansible.

    Common variables include:

    • VAULT_ADDR: The URL for your Vault instance.
    • VAULT_TOKEN: The token for Vault.
    • VAULT_AUTHTYPE: Authentication type (e.g., token, userpass, github, ldap, radius, approle).
    • VAULT_NAMESPACE: The Vault Namespace (if applicable).

    For approle authentication, you must also provide VAULT_ROLE_ID and VAULT_SECRET_ID.

    VAULT_ADDR
    VAULT_SKIP_VERIFY (set to true to skip TLS verification, not recommended for production)
    VAULT_AUTHTYPE (token, userpass, github, ldap, radius, approle)
    VAULT_LOGIN_MOUNT_POINT
    VAULT_TOKEN
    VAULT_ROLE_ID (required for approle)
    VAULT_SECRET_ID (required for approle)
    VAULT_USER
    VAULT_PASSWORD
    VAULT_CLIENT_KEY
    VAULT_CLIENT_CERT
    VAULT_CACERT
    VAULT_CAPATH
    VAULT_AWS_HEADER
    VAULT_NAMESPACE