Reputation: 2972
I have a base64 string encoded that contains a username and password separated by a colon:
echo "dXNlcjpwYXNzCg==" | base64 -d
user:pass
and the following ansible playbook:
- hosts: localhost
remote_user: ec2-user
become: yes
tasks:
- name: echo user
debug:
msg: "user: {{ 'dXNlcjpwYXNzCg==' | b64decode }}" //how to split from : and get the first value?
- name: echo pass
debug:
msg: "pass: {{ 'dXNlcjpwYXNzCg==' | b64decode }}" //how to split from : and get the second value?
I want to get both values to retrieve username and password from that decoded string. I know there is a split() function and I tried to do:
{{ 'dXNlcjpwYXNzCg==' | b64decode | split(':') }}"
but it doesn't work.
Upvotes: 0
Views: 3975
Reputation: 5740
Got it.
---
- hosts: test-multi-01
tasks:
- name: decode64 user and pass
debug:
msg: "{{ 'dXNlcjpwYXNzCg==' | b64decode }}"
register: user_pass
- set_fact:
user_and_pass: "{{ item.value.split(':') }}"
loop: "{{ lookup('dict', user_pass) }}"
when: item.key == 'msg'
- name: username
debug:
msg: "{{ user_and_pass[0] }}"
- name: password
debug:
msg: "{{ user_and_pass[1] }}"
Gives
TASK [username] ****************************************************************************
Wednesday 16 September 2020 10:15:33 +0200 (0:00:00.057) 0:00:01.146 ***
ok: [test-multi-01] => {
"msg": "user"
}
TASK [password] ****************************************************************************
Wednesday 16 September 2020 10:15:33 +0200 (0:00:00.040) 0:00:01.186 ***
ok: [test-multi-01] => {
"msg": "pass\n"
}
Note that when the username or password contains a column :
, the script will break.
Upvotes: 3