Multiple Set Facts not working with conditional set

Viewed 18

I created roles for getting different passwords from CyberArk. I noticed that everything in the roles was the same except for the query parameter to find the password, so I decided to make the query parameter a variable, and I send it to a new single CyberArk role as a variable.

Example from Playbook:

- name: Get AVI Dev Or Prod password
  import_role:
    name: /Users/n0118883/python/ansible/roles/cyberark_creds
  vars:
    query_parm: "Username=admin;Address=avi;Environment=Development"

- name: Get Venafi password
  import_role:
    name: /Users/n0118883/python/ansible/roles/cyberark_creds
  vars:
    query_parm: "Username=sahsp-avi-venafi;Address=LM"

I was hoping to call the role, pass the query parameter, and use multiple "set_fact" with a when clause to have the correct password assigned to the correct fact.

Example from Role:

- name: Get PW from cyberark
  cyberark_credential:
    api_base_url: "https://cyberark.lmig.com"
    app_id: "AVI_Cyberark_Automation"
    query: "{{ query_parm }}"
register: cyberark_command_output

- set_fact:
    admin_password: "{{ cyberark_command_output | json_query('result.Content')}}"
  when: '"Address=avi" in query_parm'

- set_fact:
    venafi_password: "{{ cyberark_command_output | json_query('result.Content')}}"
  when: '"sahsp-avi-venaf" in query_parm'

- name: "Return"
  debug:
    msg: "{{ admin_password }}"
- name: "Return"
  debug:
    msg: "{{ venafi_password }}"

No matter what I change when I run the playbook, I either get the first password twice or I get the password for the first and the error "The error was: 'venafi_password' is undefined" for the second.

In one playbook, I go to CyberArk to get three different passwords. I'm not sure if I'm trying to do too much with a role and should go back to three separate roles, or is this possible, but I'm just doing it wrong.

0 Answers
Related