Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible, don't specify hosts in playbook and specify hosts via commandline

Tags:

ansible

Problem

I have a playbook to restore data from a backup. This is a destructive operation that can potentially run on hosts: all. So currently my playbook has hosts: all in it. I'd like to force the user to specify the hosts it wants to restore the backup for so that if the playbook is run without --limit option it doesn't work.

Question

Is there any way to protect a playbook so that you have to manually specify which hosts to target as opposed to all?

like image 955
Shoe Diamente Avatar asked Oct 30 '25 04:10

Shoe Diamente


1 Answers

Sure.

Just write in your playbook (example.yml)

---
 - name: Limited playbook
   hosts: "{{ target }}"
   tasks:
    - debug:
      msg: "Running on {{ inventory_hostname }}"

Running the playbook would be like

ansible example.yml -e target=host1

You can also add a default value for target if you want.

like image 188
dgw Avatar answered Nov 02 '25 23:11

dgw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!