I'm trying to get the value of a variable in a file into an Ansible variable so I can use it.
Here's what I've got:
- name: extract Unique Key
shell: "grep UNIQUE_KEY ../config.py | cut -d' ' -f 3"
register: command_output
- set_fact:
unique_key: x{{ command_output.stdout | regex_replace("^'", '') | regex_replace('^"', '') | regex_replace("'$", '') | regex_replace('"$', '') }}
- set_fact:
unique_key: "{{ unique_key | regex_replace('^x', '') }}"
- debug: var=unique_key
This works, but feels kludgy and looks ugly.
I've already tried to add sed to my original shell module, but I couldn't figure out how to get the quotes escaped correctly. I also couldn't figure out how to escape the regex_replace to get it to work in a single variable assignment.
Is there a simpler way to go from this:
"TEST"
or
'TEST'
to this:
TEST
in Ansible? (I'm also really new to Ansible so that's not helping either)
EDIT: After the answer by @Vladimir-Botka which I initially accepted, I found this issue:
If I don't strip the quotes and embed the variable in another variable, it keeps the quotes:
I need to use this value to construct a path:
vars:
service_location: "/opt/{{ unique_key }}-scheduler-service"
If I don't remove the quotes using my method above, The variable will contain the quotes as in this output of a debug statement:
ok: [fedorasvr1] => {
"service_location": "/opt/'TEST'-scheduler-service"
}