# Ansible and --limit option

**URL:** <https://forum.ansible.com/t/ansible-and-limit-option/3139>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [January 3, 2024, 5:13pm UTC](https://forum.ansible.com/t/ansible-and-limit-option/3139 "2024-01-03T17:13:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ansible007](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@ansible007](https://forum.ansible.com/u/ansible007)\
**Post date:** [January 3, 2024, 5:13pm UTC](https://forum.ansible.com/t/ansible-and-limit-option/3139/1 "2024-01-03T17:13:20Z")

</div>

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.

```auto
##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.

```auto
##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

---

<div class="post-metadata">

**Author:** ![ptn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ptn/32/481_2.png) [@ptn](https://forum.ansible.com/u/ptn)\
**Post date:** [January 3, 2024, 5:57pm UTC](https://forum.ansible.com/t/ansible-and-limit-option/3139/2 "2024-01-03T17:57:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ansible007](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@ansible007](https://forum.ansible.com/u/ansible007)\
**Post date:** [January 3, 2024, 8:19pm UTC](https://forum.ansible.com/t/ansible-and-limit-option/3139/3 "2024-01-03T20:19:02Z")

</div>

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.

```auto
#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.

```auto
##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.

```auto
#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

```

```auto
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!

---

<div class="post-metadata">

**Author:** ![ptn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ptn/32/481_2.png) [@ptn](https://forum.ansible.com/u/ptn)\
**Post date:** [January 3, 2024, 9:10pm UTC](https://forum.ansible.com/t/ansible-and-limit-option/3139/4 "2024-01-03T21:10:49Z")

</div>

> [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](https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#inventory-aliases).

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

---

<div class="post-metadata">

**Author:** ![ansible007](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@ansible007](https://forum.ansible.com/u/ansible007)\
**Post date:** [January 7, 2024, 3:13am UTC](https://forum.ansible.com/t/ansible-and-limit-option/3139/5 "2024-01-07T03:13:24Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [February 6, 2024, 3:13am UTC](https://forum.ansible.com/t/ansible-and-limit-option/3139/6 "2024-02-06T03:13:42Z")

</div>

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