Hello!
I need to export an session key i get after logging into an application, but i always hace an missing “” or an additinally \ somehow added to the " "
The session keys are random after each login and i cannot set a fixed one:
I have to export this in the end:
export BW_SESSION="V5V7QpSo6LkdsCWhuK37Mm/r9pnFvnAexLNg7ZrQ=="
My playbook:
- name: Retrieve Bitwarden password test
hosts: www43
gather_facts: true
become: true
tasks:
- name: Login to Bitwarden Vault
ansible.builtin.command:
cmd: "bw login {{ bw_usr }} '{{ bw_pw }}' --raw"
ignore_errors: yes
no_log: true
- name: Unlock Bitwarden vault
ansible.builtin.command:
cmd: "bw unlock '{{ bw_pw }}'"
register: bw_unlock
ignore_errors: no # Ensure the play stops if unlocking fails
- name: Check if Bitwarden unlock was successful
ansible.builtin.debug:
msg: "Bitwarden unlocked successfully."
when: bw_unlock.rc == 0 # Show message only if unlock succeeded
- name: Extract BW_SESSION from unlock output
ansible.builtin.set_fact:
bw_session: "{{ (bw_unlock.stdout | regex_search('BW_SESSION=\"([^\"]+)', '\\1')) | first }}"
# Updated task: Define bwsessionfull without extra escaping
- name: Set bwsessionfull variable with BW_SESSION string
ansible.builtin.set_fact:
bwsessionfull: "BW_SESSION={{ bw_session | quote }}"
- name: Export BW_SESSION environment variable
ansible.builtin.shell: export BW_SESSION="{{ bw_session }}"
environment:
BW_SESSION: "{{ bw_session }}"
- name: Debug output of BW_SESSION
ansible.builtin.debug:
msg: "The exported BW_SESSION variable is: {{ bw_session }}"
- name: Debug output of bwsessionfull
ansible.builtin.debug:
msg: "The bwsessionfull variable is: {{ bwsessionfull }}"
- name: "Get 'password' from all Bitwarden records named 'nagivis'"
ansible.builtin.debug:
msg: >-
{{ lookup('community.general.bitwarden', 'nagivis', field='password') }}
Thank you!