Ansible.utils.cli_parse fails when command issues warning

The following task fails with the message WARNING: This command is using service account impersonation.. Even though the command runs with error. I suppose I could add ignore_errors to the task, but I would like the task to fail, if the command fails. Any ideas how to get the task to not fail when this warning is present while permitting it to fail, if the command fails?

- name: 'Get all user managed GCP service account keys'
  ansible.utils.cli_parse:
    command: >
      gcloud iam service-accounts keys list
      --iam-account={{ service_account.name }}
      --format=json
      --filter="keyType=USER_MANAGED"
      --sort-by=~validAfterTime
    parser:
      name: ansible.utils.json
    set_fact: _current_sa_keys

You have to play with failed_when, thus defining the condition(s) to be considered a failure.

There are many examples in the documentation
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_error_handling.html

Good luck!

2 Likes