All,
I am trying to refactor my playbook to run an ios command across a group of hosts… My challenge is that the command is specific to individual hosts in the group. As it stands, my playbook initial task is ran against all hosts in my group and looping over every item in my list. My intent is to run the ios command that pertains to the host and not have to have several plays or tasks in 1 playbook. Is that possible? Below is my inventory list and playbook. Feedback would be greatly appreciated!
Plays are what map hosts to tasks, there are a couple of other ways
you can single out a host, but a Play is the optimal path to do this.
Having multiple plays in a playbook was always the original intention
(why its named playbook), but if you insist on avoiding an additional
play, here are a couple of options:
- when: inventory_hostname == 'specific host'
or
- run_once: True, and make sure that host is always the first host in
the selection for the play.
Yeah in my scenario I would have 12+ plays for this development environment. I was mainly looking for other options that would shorten the size of the playbook if I had to run a similar command across hundreds of hosts. With the when conditional, I would still need individual plays per host correct? Also, will the run_once action run the first item in the for loop for the first host, then my 2nd item in the for loop for the second host in the group, etc?
Actually after re-reading your post Brian. I can have 1 play with multiple tasks set to run_once or use a when conditional; I could also just use multiple plays to accomplish this. I was hopeful there was a way to use a for loop to loop over specific hosts running the task individually per host; however I am thinking that would make this too complex.I plan to use a role for this and break up my tasks in the tasks/main.yml file.
Thanks for the feedback everyone! Arden, that looks like a solid solution also. Considering this code is in a dev environment, I will play around with the various methods specified.