Refactor Playbook to loop over hosts individually

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!

[ios]
Til_INET_EDC-2-WDC_7kb_Core.xxx.org
Til-Site-RTR-1
Til-Site-RTR-2
Til-Site-RTR-3
Til-Site-SW1
Til-Site-SW2
Til-Stratix-5700
Til-Stratix-8000
Til-Stratix-8300
NIS-Til-4506
Til-WDC-Core
Til_INET_WDC-2-WDC_7ka_Core.xxx.org

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.

Couldn’t you set what you needed as a host specific variable in your inventory file and simply call that variable in the appropriate place?

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?

No, i misread and didn't realize the 'per item/host' relation ship,
using a variable on a task as Arden suggests would work better in that
case.

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.

If I understand correctly, you would like to run the fist command on fist host, the second command on the second host and so on?

If so you could do something like this

     - name: Restore base configs
       ios_command:
         commands:
           - command: 'configure replace {{ item.command }}'
             prompt: '[no]'
             answer: y
       when: intentory_hostanme == item.host
       with_items:
         - command: nvram:Til-EDC-INET_base
           host: Til_INET_EDC-2-WDC_7kb_Core.xxx.org
         - command: nvram:Til-Site-RTR-1_base
           host: Til-Site-RTR-1
           ...
       register: result
       tags: ios

Yes that is exactly what I was looking for Kai. Would I still specify the entire group ‘ios’ in the hosts section of the playbook?

Yes.

Just for the sake of actually getting the code I had in mind out, incase anyone else might find this solution helpful.

I’m showing two

Inventory
`
[ios]

Til_INET_EDC-2-WDC_7kb_Core.xxx.org host_command=nvram:Til-EDC-INET_base
Til-Site-RTR-1 host_command=nvram:Til-Site-RTR-1_base

`

Playbook

`

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.