Password (variable) in ansible vault

Hello !
how can I make the ansible plybook read a encrypt variable in a ansible vault.
I have a file called secret (that i encrypted using the ansible vault) , I run the command “ansible-playbook -i inventory --ask-vault-pass playbook.yaml” , however the playbook file do not read the variables set on the secret files, it says that the variables are undifined.
What it is that i am missing?
tks!

Can you share your playbook?

Here is the playbook for this task
at the end of the file are the variables: vsphere_password and vsphere_host that are defined in a secret encrypted file (via ansible vault).
tks

- name: Update APT package cache
  apt:
    update_cache: yes

- name: Install required packages
  apt:
    name:
      - apt-transport-https
      - ca-certificates
      - curl
      - gnupg
      - lsb-release
    state: present

- name: Add Docker GPG key
  apt_key:
    url: https://download.docker.com/linux/ubuntu/gpg
    state: present

- name: Add Docker APT repository
  apt_repository:
    repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_lsb.codename }} stable
    state: present

- name: Install Docker
  apt:
    name: docker-ce
    state: present

- name: Start Docker service
  service:
    name: docker
    state: started

- name: Run VMware Exporter container
  docker_container:
    name: vmware_exporter
    image: pryorda/vmware_exporter
    state: started
    restart_policy: always
    detach: yes
    ports:
      - "9272:9272"
    env:
      VSPHERE_USER: "[email protected]"
      VSPHERE_PASSWORD: "{{ VSPHERE_PASSWORD }}"
      VSPHERE_HOST: "{{ VSPHERE_HOST }}"
      VSPHERE_IGNORE_SSL: "True"
      VSPHERE_SPECS_SIZE: "2000"
  vars:
    VSPHERE_PASSWORD: vsphere_password
    VSPHERE_HOST: vsphere_host

You’ve not specified the var file. How does ansible know which file to read? You can specify this on the command line or use this reference in your playbook:

vars_files:
  - /path/encrypted_vars.yml
1 Like

Tks !! that was exactly what I was missing !