1

I have the following code which isn't throwing an error but the fact is empty

 - shell: echo '{{ p }}'
 register: results
 - debug:
 var: results
 - set_fact:
 myrepo: "{{ results.stdout | regex_search(regexp,'\1円') | default ( {'0':'global'} ) }}"
 vars:
 regexp: '(.*)/(.*)'

Here is the output

TASK [command] **************************************************************************************************************************************************************************************
changed: [localhost]
TASK [debug] ****************************************************************************************************************************************************************************************
ok: [localhost] => {
 "results": {
 "changed": true, 
 "cmd": "echo 'tim'", 
 "delta": "0:00:00.095831", 
 "end": "2017-09-06 16:37:19.977023", 
 "rc": 0, 
 "start": "2017-09-06 16:37:19.881192", 
 "stderr": "", 
 "stderr_lines": [], 
 "stdout": "tim", 
 "stdout_lines": [
 "tim"
 ]
 }
}
TASK [set_fact] *************************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug] ****************************************************************************************************************************************************************************************
ok: [localhost] => {
 "myrepo": ""
}

The command is ansible-playbook -i hosts -c local file.yml --extra-vars "p=tim" I want myrepo to be global if the regex results are empty

asked Sep 6, 2017 at 16:55

1 Answer 1

2

Without any parameters default filter is triggered only when value is Undefined. But result of unmatched regexp is an empty string, which is not Undefined. You may want to set boolean flag:

 - set_fact:
 myrepo: "{{ results.stdout | regex_search(regexp,'\1円') | default('global', boolean=True) }}"
 vars:
 regexp: '(.*)/(.*)'
answered Sep 6, 2017 at 17:09
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.