Evaluating return code in ansible conditional

后端 未结 1 1455
囚心锁ツ
囚心锁ツ 2021-01-31 17:41

I\'m working on automating a task which needs to append the latest version of software to a file. I don\'t want to it do this multiple times for the same version.

It loo

相关标签:
1条回答
  • 2021-01-31 17:59

    As nikobelia pointed out in the comments, grep returns an exit code of 1 when it doesn't match any lines. Ansible then interprets this (actually any status code other than 0 from a shell/command task) as an error and so promptly fails.

    You can tell Ansible to ignore the response code from the shell/command task by using ignore_errors. Although with grep this will ignore actual errors (given by a return code of 2) so instead you might want to use failed_when like this:

    - name: register version check
      shell: cat /root/versions.js | grep -q {{VERSION}}
      register: current_version
      failed_when: current_version.rc == 2
    
    0 讨论(0)
提交回复
热议问题