Execute playbook against ansible_host without using hostname - AWX

Hi All,

I am using ansible awx and I have a requirement like this. I have a set of servers imported using the VM name, and inside of the variables, I have defined ansible_host: VM IP address. How can I target ansible_host(IP address of the VM) instead of the VM name. Because most of the ppl in our org only visible the IP address, not the VM name. I tried to define the IP address in the limit filed but it’s failing with the message “Could not match supplied host pattern ignoring : 10.x.x.x”

Might try using aliases. Add ansible_host: <ip address> as a host variable in your inventory.
https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#inventory-aliases

Hope that helps.

Hello, thank you for the quick reply however I already defined ansible_host as a variable inside the host but when I try to limit the IP in the AWX template I get the “Could not match supplied host pattern ignoring : 10.x.x.x” error.

can you use the way we did in the ‘good old days’ by using /etc/hosts ?

Did a little bit of research, and it looks like it will not be possible to use the IP address to refer to a host with the dynamic inventory that you’re using. According to the Limitation of patterns section of the Ansible documentation, if an alias is defined (host1 in the example) then the IP address cannot be used to refer to a host in the inventory. An inventory of only IP addresses would need to be created to be able to do want you want.

1 Like

Thank you everyone for the replies. @TheSpy0 Yeah, it seems to be the only option to go with the IP address.

1 Like

Maybe I’m missing something, but outside of AWX, if I create this one-line inventory source called invx.ini:

host_192.168.254.246 ansible_host=192.168.254.246

then I can run

$ ansible host_192.168.254.246 -i invx.ini -m setup

So, if you get “a set of servers imported” (into AWX?) and have their IP addresses, I believe you could take that set and create/update, with a playbook, an inventory source that’s known to AWX. Then the people in your organization who have the IP address of a relevant host could target “host_<ip address>”. There’s a little work up front to keep the host_-prefixed inventory source up-to-date, but maybe that’s okay?

1 Like

Hey, thanks for the reply. I was thinking the same thing, but if we are targeting 100+ hosts or more than that at the same time, then it will be troublesome to generate this pattern for all the hosts, and if someone makes a mistake when generating a pattern, the playbook will execute against wrong targets.

If you define a bash function like this:

limit_by_ip ()
{ 
    local -a hosts_by_ip=($(for h in "$@"; do echo "host_$h"; done));
    ( IFS=,;
    printf " --limit=%s\n" "${hosts_by_ip[*]}" )
}

then this command

ansible-playbook foo.yml $(limit_by_ip 12.23.34.45 56.34.56.34 34.23.12.34) -v -bK

expands to this:

ansible-playbook foo.yml --limit=host_12.23.34.45,host_56.34.56.34,host_34.23.12.34 -v -bK

But if you’ve got a situation that requires people to make no mistakes when entering a hundred or more IP addresses — regardless of the format — then you need to change that situation.

Since you’d be creating an inventory source file in advance, surly you can concoct some reasonable groups to put these hosts into, groups with names that make more sense than their VM names (your original problem) and much easier to enter without mistakes than IP addresses (your more recent, and in my opinion, very real, problem).