Setting check mode in ansible-playbook execution with env var

Is there a way to trigger a playbook to be executed with check mode using an environment variable? To put it another way, can an env var be set to be the equivalent of the flag --check on ansible-playbook?

It’s only configurable as a CLI arg or the check_mode keyword. Individual plays can configure check_mode using an environment variable though.

- hosts: all
  check_mode: "{{ lookup('env', 'ANSIBLE_CHECK_MODE', default='false') | bool }}"

If your play uses the special variable ansible_check_mode, keep in mind it only reflects the value of --check, not check_mode.

- hosts: all
  vars:
    play_check_mode: "{{ lookup('env', 'ANSIBLE_CHECK_MODE', default='false') | bool }}"
  check_mode: "{{ play_check_mode }}"
  tasks:
  - action: ...
    when:
      - ansible_check_mode is falsy
      - play_check_mode is falsy
3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.