<p>In the following task I want to make ansible <strong>not</strong> to fail when <code>stderr</code> contains <code>the object has been modified</code> or <code>have a resource type</code>.</p>
<pre><code>- name: List nodes
  delegate_to: localhost
  shell: kubectl get nodes
  run_once: true
  register: output_error
  failed_when: (output_error.stderr_lines | length &gt; 0) and (&quot;'the object has been modified' not in output_error.stderr or 'have a resource type' not in output_error.stderr&quot;)
</code></pre>
<p>However I am reveiving the following error message:</p>
<pre><code>kubectl get nodes&quot;, &quot;delta&quot;: &quot;0:00:00.498169&quot;, &quot;end&quot;: &quot;2022-08-05 11:08:01.386024&quot;, &quot;failed_when_result&quot;: true, &quot;item&quot;: &quot;nodes&quot;, &quot;rc&quot;: 0, &quot;start&quot;: &quot;2022-08-05 11:08:00.887855&quot;, &quot;stderr&quot;: &quot;error: the server doesn't have a resource type \&quot;nodes\&quot;&quot;, &quot;stderr_lines&quot;: [&quot;error: the server doesn't have a resource type \&quot;nodes\&quot;&quot;], &quot;stdout&quot;: &quot;&quot;, &quot;stdout_lines&quot;: []}
</code></pre>

user3580316
<p>Use a regex which includes both alternatives:</p>
<pre class="lang-yaml prettyprint-override"><code>- name: List nodes
  delegate_to: localhost
  shell: kubectl get nodes
  run_once: true
  register: output_error
  failed_when:
    - output_error.stderr_lines | length &gt; 0
    - output_error.stderr is not regex('the object has been modified|have a resource type')
</code></pre>
<p>For easier reading and maintenance you can set your positive matches in a list and join them together in an expression.</p>
<pre class="lang-yaml prettyprint-override"><code>- name: List nodes
  vars:
    acceptable_err_matches:
      - the object has been modified
      - have a resource type
    match_regex: acceptable_err_matches | map('regex_escape') | join('|')
  delegate_to: localhost
  shell: kubectl get nodes
  run_once: true
  register: output_error
  failed_when:
    - output_error.stderr_lines | length &gt; 0
    - output_error.stderr is not regex(match_regex)
</code></pre>
