Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force limit parameter to be set in ansible

Tags:

ansible

Is there a way to force commands ansible-playbook, ansible-variable, etc... to be executed with a --limit option (otherwise to deny it) ?

I discovered that, on a cluster it can easily run a playbook to all nodes if you mistakenly run it without limit, I'd like to prevent it from ansible users.

like image 703
Waldo Avatar asked Oct 24 '18 16:10

Waldo


Video Answer


2 Answers

Use the ansible_limit variable (added in ansible 2.5). You can test like this:

tasks:
  - fail:
      msg: "you must use -l or --limit"
    when: ansible_limit is not defined
    run_once: true
like image 190
Victor Roetman Avatar answered Sep 28 '22 16:09

Victor Roetman


It's the opposite of the task I've solved recently. My goal was to detect there is a --limit and to skip some plays.

https://medium.com/opsops/how-to-detect-if-ansible-is-run-with-limit-5ddb8d3bd145

In your case you can check this in the play and fail if it "full run":

- hosts: all
  gather_facts: no
  tasks:
    - set_fact:
         full_run: '{{play_hosts == groups.all}}'
    - fail:
        msg: 'Use --limit, Luke!'
      when: full_run

You can use a different group instead of all, of course (change it in both hosts and set_fact lines).

like image 38
George Shuklin Avatar answered Sep 28 '22 15:09

George Shuklin