0

I am trying to play a yml on other host than configured, using -l option.. but skipping: no hosts matched . The scenario is where a host associated playbook is needed to be exceptionally used for some other host. (and for safety reasons, the playbook cannot have hosts:all and be left to the admin to limit the target(s))
What is the correct way to do this (if there is any)?

L.E. So, in the end, the answer of @mdaniel gave me the idea o a bash wrapper that creates a temp yml where the host: field is replaced with the argument.. it's not pretty but it works. (same works for a dynamical generation of a playbook from a series of tasks) and the proper ansible way to do it i just found it here: https://stackoverflow.com/a/18195217/624734

Adrian Sevcenco
  • 311
  • 1
  • 8

2 Answers2

1

What is the correct way to do this?

Use all as the target of the playbook, and then constrain the hosts it applies to via the inventory, or the --limit that you mentioned

- hosts: all
  # and now the rest of your playbook
mdaniel
  • 31,240
  • 5
  • 55
  • 58
  • oh, yeah, thats could work, thanks!! but my intent was more alongside: "this playbook is for host X and i really do not want to play it on some other host unless when by exception i do want that" (a scenario being when one might want to quickly test on a vm the configuration of a specific production server). i searched also for a way to run tasks directly in cli but i did not find a way to do it... – Adrian Sevcenco Jun 22 '20 at 19:32
  • 1
    Please [edit your question](https://stackoverflow.com/posts/62516204/edit) and describe that scenario in more detail than the _one sentence_ you used originally if you are looking for a more complicated answer – mdaniel Jun 22 '20 at 20:11
  • Thanks! i specified the scenario in the question. – Adrian Sevcenco Jun 22 '20 at 21:20
0

You can try the below approach if you want to restrict hosts:all in your ansible script.

- hosts: "{{ host_group }}"
  # Rest of your playbook

And you can have a specific group in your hosts file which you can use for testing. For example,

[test]
192.168.1.1 # Test host
# Rest of your inventory file

And trigger the ansible playbook in the following order,

ansible-playbook -i hosts main.yml -e host_group="test"
Shubham Vaishnav
  • 1,637
  • 6
  • 18