Howto: Prevent Ansible from Running if --limit Argument Not Set

Howto: Prevent Ansible from Running if --limit Argument Not Set

There are many cases where running a playbook across all hosts would be disastrous. Instead the playbook is used by limiting the hosts using the --limit argument.

It would be ideal if we could force the user to use the --limit argument, and fail the Ansible run if the --limit argument is missing.

We can do that with the following task code.

tasks:
  - name: "Fail if no --limit set"
    fail:
      msg: "you must use -l or --limit"
    when: ansible_limit is not defined
    run_once: true

Subscribe to Linux Developer

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe