Ansible and --limit option

I am able to successfully execute the ansible script through powershell but I can’t quite figure out how I can target a specific host automatically without having to manually modify the script. If I don’t include the --limit option, the script works without any issues but will run against all hosts since these are being defined inside the playbook using hosts: all.

##Script for all hosts
Invoke-VMScript -VM "VM1" -ScriptText "echo ******** | sudo -S  ansible-playbook /home/ansible/ansible-workspace/simple_playbook.yaml -u ansible" -GuestUser $GuestUsername -GuestPassword $GuestPassword  -ScriptType Bash

How can I target a specific host using the script above? I know I can include the --limit option but have to manually specify the host. I’m also unable to specify a variable as the host name since this will not be interpreted by the ansible playbook. Any help would be greatly appreciated.

##Script for a single host
Invoke-VMScript -VM "VM1" -ScriptText "echo ******  | sudo -S  ansible-playbook /home/ansible/ansible-workspace/simple_playbook.yaml -u ansible --limit 10.10.10.10" -GuestUser $GuestUsername -GuestPassword $GuestPassword  -ScriptType Bash

How can I target a host as a variable using the above script so that I don’t have to manually specify it? Thank you!

Ansible version:
ansible [core 2.15.8]
config file = /etc/ansible/ansible.cfg
configured module search path = [‘/root/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python3/dist-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (/usr/bin/python3)
jinja version = 3.0.3
libyaml = True

Hi,

How can I target a host as a variable using the above script so that I don’t have to manually specify it?

Depends. Where would you like to define your hosts in the first place ?

But yeah, sure, it’s a command line parameter which will be ran from bash apparently; I don’t see why it wouldn’t work. Just be mindful about this var expansion from your shell, depending on how you pass it to your command.

Another solution would be to ship a lightweight inventory with it you could target from either your playbook’s play block or through ansible-playbook’s --inventory parameter. Really depends on what you’re trying to achieve.

Hi Pierre,

So if I use this variable $server to define the host I want to target, I get the error below.

Here’s the line of code where I am using the $server variable.

#Target or limit to a host using a variable
$server = "VM2"
Invoke-VMScript -VM "VM1" -ScriptText "echo ***** | sudo -S  ansible-playbook /home/ansible/ansible-workspace/simple_playbook.yaml -u ansible --limit '$server'" -GuestUser $GuestUsername -GuestPassword $GuestPassword  -ScriptType Bash

Here’s the error that I get when using the $server variable instead of the actual hostname or IP.

##Error##
|  [sudo] password for ansible: [WARNING]: Could not match supplied host pattern, ignoring: VM2
|  ERROR! Specified inventory, host pattern and/or --limit leaves us with no hosts to target.

However, if I execute the script below by manually specifying the host, then it works as expected. However, this defeats the purpose of the automated routine as I would have to always manually update the IP for all new builds.

#Target or limit to a host by manually specifying it
Invoke-VMScript -VM "VM1" -ScriptText "echo ***** | sudo -S  ansible-playbook /home/ansible/ansible-workspace/simple_playbook.yaml -u ansible --limit 10.10.10.11" -GuestUser $GuestUsername -GuestPassword $GuestPassword  -ScriptType Bash
ScriptOutput
-----------------------------------------------------------------------------------------------------------------------|  [sudo] password for ansible: 
|  PLAY [all] *********************************************************************
  PLAY RECAP *********************************************************************
|  10.10.10.11               : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Thanks again!

[WARNING]: Could not match supplied host pattern, ignoring: VM2

This means ‘VM2’ host is not present in your inventory.ies. In your functioning example, you go for an IP address, which in turn would be in your inventory.ies.

Inventories are not like /etc/hosts file, and there is no DNS resolution in play at this point; hosts you’re passing to --limit should match an host present in your inventories.

Just run ansible -i <yourInventoryFilesPaths> --list-hosts all and pick one of those, or alias them.

Hope this makes sense. If not, please tell and I’ll try to be more specific.

3 Likes

Hi Pierre,

You were spot on your suggestion. Once I changed the variable to the IP address of the host, the script worked as expected. Thanks for your help!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.