# How to pass ansible variables to powershell cmdlet using win\_shell module

**URL:** <https://forum.ansible.com/t/how-to-pass-ansible-variables-to-powershell-cmdlet-using-win-shell-module/30617>\
**Category:** Ansible Developer\
**Created:** [June 20, 2019, 5:16pm UTC](https://forum.ansible.com/t/how-to-pass-ansible-variables-to-powershell-cmdlet-using-win-shell-module/30617 "2019-06-20T17:16:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stefano\_Leandro](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@Stefano\_Leandro](https://forum.ansible.com/u/Stefano_Leandro)\
**Post date:** [June 20, 2019, 5:16pm UTC](https://forum.ansible.com/t/how-to-pass-ansible-variables-to-powershell-cmdlet-using-win-shell-module/30617/1 "2019-06-20T17:16:49Z")

</div>

Hi guys,  
Someone could help me ?

I cannot understand how to pass an ansible variable to powershell cmdlet via win\_shell command .  
Below my code :

tasks:

- set\_fact:  
target\_ip: “lookup(‘dig’, inventory\_hostname)”
- win\_shell: |  
“Get-DHCPServerV4Lease -IPAddress "{{ target\_ip }}"| Add-DhcpServerv4Reservation”

This return me also this one below :

changed: [host1] =\> {“changed”: true, “cmd”: “"Get-DHCPServerV4Lease -IPAddress \"lookup(‘dig’, inventory\_hostname)\"| Add-DhcpServerv4Reservation"”, “delta”: “0:00:00.546871”, “end”: “2019-06-20 04:18:36.787461”, “rc”: 0, “start”: “2019-06-20 04:18:36.240590”, “stderr”: “At line:1 char:101\r\n+ … e -IPAddress \"lookup(‘dig’, inventory\_hostname)\"| \r\nAdd-DhcpServerv4Reservation"\r\n+ ~~~~~~\r\nUnexpected token ‘lookup’ in expression or statement.\r\nAt line:1 char:114\r\n+ … \"lookup(‘dig’, inventory\_hostname)\"| Add-DhcpServerv4Reservation"\r\n+ ~\r\nMissing expression after ‘,’.\r\nAt line:1 char:115\r\n+ … "lookup(‘dig’, inventory\_hostname)\"| Add-DhcpServerv4Reservation"\r\n+~~~~~~~~~~~~~~~~~~\r\nUnexpected token ‘inventory\_hostname’ in expression or statement.\r\nAt line:1 char:114\r\n+ … \"lookup(‘dig’, inventory\_hostname)\"| Add-DhcpServerv4Reservation"\r\n+ ~\r\nMissing closing ‘)’ in expression.\r\nAt line:1 char:133\r\n+ … entory\_hostname)\"| Add-DhcpServerv4Reservation"\r\n+ ~\r\nUnexpected token ‘)’ in expression or statement.\r\n + CategoryInfo : ParserError: (🙂 , ParentContainsErrorRecordEx \r\n ception\r\n + FullyQualifiedErrorId : UnexpectedToken”, “stderr\_lines”: [“At line:1 char:101”, "+ … e -IPAddress \"lookup(‘dig’, inventory\_hostname)\"| “, “Add-DhcpServerv4Reservation"”, “+ ~~~~~~”, “Unexpected token ‘lookup’ in expression or statement.”, “At line:1 char:114”, “+ … \"lookup(‘dig’, inventory\_hostname)\"| Add-DhcpServerv4Reservation"”, “+ ~”, “Missing expression after ‘,’.”, “At line:1 char:115”, “+ … "lookup(‘dig’, inventory\_hostname)\"| Add-DhcpServerv4Reservation"”, “+~~~~~~~~~~~~~~~~~~”, “Unexpected token ‘inventory\_hostname’ in expression or statement.”, “At line:1 char:114”, “+ … \"lookup(‘dig’, inventory\_hostname)\"| Add-DhcpServerv4Reservation"”, “+ ~”, “Missing closing ‘)’ in expression.”, “At line:1 char:133”, “+ … entory\_hostname)\"| Add-DhcpServerv4Reservation"”, “+ ~”, “Unexpected token ‘)’ in expression or statement.”, " + CategoryInfo : ParserError: (🙂 , ParentContainsErrorRecordEx “, " ception”, " + FullyQualifiedErrorId : UnexpectedToken”], “stdout”: “”, “stdout\_lines”: }

I

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [June 20, 2019, 7:33pm UTC](https://forum.ansible.com/t/how-to-pass-ansible-variables-to-powershell-cmdlet-using-win-shell-module/30617/2 "2019-06-20T19:33:40Z")

</div>

Your set\_fact task is setting ‘lookup(‘dig’, inventory\_hostname)’ as a literal string, you will to use braces so it actually interprets the lookup, e.g.

`

- set\_fact:  
target\_ip: ‘{{ lookup(“dig”, inventory\_hostname) }}’

`

In the future you are better off posting in the Ansible Project mailing like, Ansible Development is for Ansible engine and plugin development questions not using Ansible.

Thanks

Jordan

---

<div class="post-metadata">

**Author:** ![Stefano\_Leandro](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@Stefano\_Leandro](https://forum.ansible.com/u/Stefano_Leandro)\
**Post date:** [June 20, 2019, 8:49pm UTC](https://forum.ansible.com/t/how-to-pass-ansible-variables-to-powershell-cmdlet-using-win-shell-module/30617/3 "2019-06-20T20:49:35Z")

</div>

Yes Sir,  
many thanks.

Now, it seems work proprerly:

- set\_fact:  
target\_ip: ‘{{ lookup(“dig”, inventory\_hostname) }}’
- win\_shell: |  
Get-DHCPServerV4Lease -IPAddress {{ target\_ip }} | Add-DhcpServerv4Reservation
