Home > front end >  extract password as variable
extract password as variable

Time:10-28

new to ansible ..

Trying to get this result:

variable tgt_wls_pwd = the result of env1.wls_pwd = 1234
variable tgt_apps_pwd = the result of env1.apps_pwd = 5678

The unencrypted password file vault.yml

env1:
 wls_pwd: 1234
 apps_pwd: 5678

Playbook

ansible-playbook tgt-app-stop.yml --extra-vars="target_clone=env1"
 - name: Stop Application Tier(s) process
   hosts: " {{ target_clone }}-app01"
   any_errors_fatal: true
   remote_user: ansible
   become: yes
   become_user: install
   roles:
      - oraapp-stop
   vars_files:
      - vault.yml
   tasks:
      - set_fact:
          target_clone: "{{ target_clone }}"
   vars:
#       tgt_wls_pwd: "{{ target_clone }}||{{ wls_pwd }}"
#       tgt_apps_pwd: "{{ target_clone }}||{{ apps_pwd }}"
#     tgt_wls_pwd: "{{ target_clone ['{{ wls_pwd }}'] }}"
#     tgt_apps_pwd: "{{ target_clone ['{{ apps_pwd }}'] }}"
     tgt_wls_pwd: "{{ target_clone.wls_pwd }}"
     tgt_apps_pwd: "{{ target_clone.apps_pwd }}"

I've tried quite a few permutations

target_clone is an extra variable passed to the playbook when running.

Thanks.

CodePudding user response:

You'll need the vars lookup plugin. See

shell> ansible-doc -t lookup vars

For example, given the file

shell> cat vault.yml 
env1:
  wls_pwd: 1234
  apps_pwd: 5678

and the inventory

shell> cat hosts
env1-app01

The playbook

shell> cat tgt-app-stop.yml
- hosts: "{{ target_clone }}-app01"
  gather_facts: false
  vars_files:
    - vault.yml
  vars:
    tgt_wls_pwd: "{{ lookup('vars', target_clone).wls_pwd }}"
    tgt_apps_pwd: "{{ lookup('vars', target_clone).apps_pwd }}"
  tasks:
    - debug:
        msg: |
          tgt_wls_pwd: {{ tgt_wls_pwd }}
          tgt_apps_pwd: {{ tgt_apps_pwd }}

gives

shell> ansible-playbook tgt-app-stop.yml -e "target_clone=env1"

PLAY [env1-app01] ****************************************************************************

TASK [debug] *********************************************************************************
ok: [env1-app01] => 
  msg: |-
    tgt_wls_pwd: 1234
    tgt_apps_pwd: 5678

PLAY RECAP ***********************************************************************************
env1-app01: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  • Related