How to pass ansible variables to powershell cmdlet using win_shell module

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: (:slight_smile: , 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: (:slight_smile: , ParentContainsErrorRecordEx “, " ception”, " + FullyQualifiedErrorId : UnexpectedToken”], “stdout”: “”, “stdout_lines”: }

I

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

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