Ansible shell module returns error when grep results are empty

前端 未结 2 552
遇见更好的自我
遇见更好的自我 2020-12-01 12:33

I am using Ansible\'s shell module to find a particular string and store it in a variable. But if grep did not find anything I am getting an error.

Example:

2条回答
  •  执念已碎
    2020-12-01 12:43

    Like you observed, ansible will stop execution if the grep exit code is not zero. You can ignore it with ignore_errors.

    Another trick is to pipe the grep output to cat. So cat exit code will always be zero since its stdin is grep's stdout. It works if there is a match and also when there is no match. Try it.

    - name: Get the http_status
      shell: grep "http_status=" /var/httpd.txt | cat
      register: cmdln
      check_mode: no
    

提交回复
热议问题