2017-09-06 3 views
0

I 오류를 던지고되지 다음 코드를 가지고 있지만 사실은 여기 ansible 구문 분석 여분의 var에 사용하는 기본 정규식이 실패 할 경우

- shell: echo '{{ p }}' 
    register: results 

    - debug: 
     var: results 

    - set_fact: 
     myrepo: "{{ results.stdout | regex_search(regexp,'\\1') | default ({'0':'global'}) }}" 
    vars: 
     regexp: '(.*)/(.*)' 

이 명령은 출력

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": "" 
} 

입니다 비어 ansible-playbook -i hosts -c local file.yml --extra-vars "p=tim" 정규 표현식 결과가 비어있는 경우 myrepo가 ​​global이됩니다.

답변

1

매개 변수가없는 경우 default 값이 Undefin 일 때만 필터가 트리거됩니다. 에드. 그러나 일치하지 않는 regexp의 결과는 빈 문자열이며 이 아닌은 정의되지 않습니다. boolean 플래그를 설정할 수도 있습니다.

- set_fact: 
     myrepo: "{{ results.stdout | regex_search(regexp,'\\1') | default('global', boolean=True) }}" 
    vars: 
     regexp: '(.*)/(.*)'