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

Share Tweet Send
0 Comments
Loading...
You've successfully subscribed to Linux Developer
Great! Next, complete checkout for full access to Linux Developer
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.