win_shell module with multiple lines not working

1,113 views
Skip to first unread message

Yehuda Pinhas

unread,
Feb 23, 2020, 6:45:56 AM2/23/20
to Ansible Project
Hi guys.

This is my code:
---
  - nameMulti-line shell
    win_shell:
    ## Creating Scopes
      Add-DhcpServerv4Scope -Name "{{ hebrew_site_name }}" -StartRange "{{ DHCP_computers_segment }}.61" -EndRange "{{ DHCP_computers_segment }}.243" -SubnetMask "255.255.255.0" -LeaseDuration "8.00:00:00"
      Add-DhcpServerv4Scope -Name "{{ hebrew_site_name }} טלפוניה" -StartRange "{{ DHCP_telephony_segment }}.61" -EndRange "{{ DHCP_telephony_segment }}.243" -SubnetMask "255.255.255.0" -LeaseDuration "8.00:00:00"

Error:
2020-02-23 13:33:51,237 p=54513 u=ansible n=ansible | PLAY [---------------   Creating VLAN 2,5 IP pools on dhcp1   ---------------] ********************
2020-02-23 13:33:51,251 p=54513 u=ansible n=ansible | TASK [set_fact] ***********************************************************************************
2020-02-23 13:33:51,270 p=54513 u=ansible n=ansible | ok: [dhcp-test]
2020-02-23 13:33:51,275 p=54513 u=ansible n=ansible | TASK [include_vars] *******************************************************************************
2020-02-23 13:33:51,293 p=54513 u=ansible n=ansible | ok: [dhcp-test]
2020-02-23 13:33:51,297 p=54513 u=ansible n=ansible | TASK [include_role : new_branch] ******************************************************************
2020-02-23 13:33:51,612 p=54513 u=ansible n=ansible | TASK [new_branch : Multi-line shell] **************************************************************
2020-02-23 13:33:51,742 p=54528 u=ansible n=requests.packages.urllib3.connectionpool | Starting new HTTPS connection (1): dhcp-test
2020-02-23 13:33:51,767 p=54528 u=ansible n=requests.packages.urllib3.connectionpool | "POST /wsman HTTP/1.1" 401 0
2020-02-23 13:33:51,770 p=54528 u=ansible n=requests.packages.urllib3.connectionpool | Resetting dropped connection: dhcp-test
2020-02-23 13:33:51,794 p=54528 u=ansible n=requests.packages.urllib3.connectionpool | "POST /wsman HTTP/1.1" 401 0
2020-02-23 13:33:51,872 p=54528 u=ansible n=requests.packages.urllib3.connectionpool | "POST /wsman HTTP/1.1" 200 1629
2020-02-23 13:33:51,884 p=54528 u=ansible n=requests.packages.urllib3.connectionpool | "POST /wsman HTTP/1.1" 200 847
2020-02-23 13:33:52,443 p=54528 u=ansible n=requests.packages.urllib3.connectionpool | "POST /wsman HTTP/1.1" 200 753
2020-02-23 13:33:53,177 p=54528 u=ansible n=requests.packages.urllib3.connectionpool | "POST /wsman HTTP/1.1" 200 998
2020-02-23 13:33:57,774 p=54528 u=ansible n=requests.packages.urllib3.connectionpool | "POST /wsman HTTP/1.1" 200 3575
2020-02-23 13:33:57,779 p=54528 u=ansible n=requests.packages.urllib3.connectionpool | "POST /wsman HTTP/1.1" 200 757
2020-02-23 13:33:57,784 p=54528 u=ansible n=requests.packages.urllib3.connectionpool | "POST /wsman HTTP/1.1" 200 602
2020-02-23 13:33:57,789 p=54513 u=ansible n=ansible | fatal: [dhcp-test]: FAILED! => {"changed"true"cmd": "Add-DhcpServerv4Scope 
-Name \"אור-יהודה-מומה\" -StartRange \"10.90.88.61\" -EndRange \"10.90.88.243\" -SubnetMask \"255.255.255.0\" -LeaseDuration \"8.00:00:00\" 
Add-DhcpServerv4Scope -Name \"אור-יהודה-מומה טלפוניה\" -StartRange \"10.95.88.61\" -EndRange \"10.95.88.243\" -SubnetMask \"255.255.255.0\" 
-LeaseDuration \"8.00:00:00\"", "delta": "0:00:03.656283", "end": "2020-02-23 11:33:57.948151", "msg": "non-zero return code", "rc": 1
"start""2020-02-23 11:33:54.291867""stderr": "Add-DhcpServerv4Scope : Cannot bind parameter because parameter 'Name' is specified more than
 once. To provide \r\nmultiple values to parameters that can accept multiple values, use the array syntax. For example, 
 \"-parameter \r\nvalue1,value2,value3\".\r\nAt line:1 char:239\r\n+ ... 55.0\" -LeaseDuration \"8.00:00:00\" Add-DhcpServerv4Scope 
 -Name \"אור-יה ...\r\n+                                                             ~~~~~\r\n    + CategoryInfo          : 
 InvalidArgument: (:) [Add-DhcpServerv4Scope], ParameterBindingException\r\n    + FullyQualifiedErrorId : 
 ParameterAlreadyBound,Add-DhcpServerv4Scope", "stderr_lines": ["Add-DhcpServerv4Scope : Cannot bind parameter because parameter 'Name' is 
 specified more than once. To provide ", "multiple values to parameters that can accept multiple values, use the array syntax. 
 For example, \"-parameter ""value1,value2,value3\".", "At line:1 char:239", "+ ... 55.0\" -LeaseDuration \"8.00:00:00\" 
 Add-DhcpServerv4Scope -Name \"אור-יה ...""+                                                             ~~~~~", "    + 
 CategoryInfo          : InvalidArgument: (:) [Add-DhcpServerv4Scope], ParameterBindingException", "    + FullyQualifiedErrorId : 
 ParameterAlreadyBound,Add-DhcpServerv4Scope"], "stdout": "", "stdout_lines": []}
2020-02-23 13:33:57,791 p=54513 u=ansible n=ansible | PLAY RECAP ****************************************************************************************
2020-02-23 13:33:57,791 p=54513 u=ansible n=ansible | dhcp-test                  : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

If i'm commenting any one of the add-scope commands they work perfectly. But for some reason when Ansible push both in one-line instead multiple lines.



To try and fix that I changed my code to this:
---
  - nameMulti-line shell
    win_shell: |
    ## Creating Scopes
      Add-DhcpServerv4Scope -Name "{{ hebrew_site_name }}" -StartRange "{{ DHCP_computers_segment }}.61" -EndRange "{{ DHCP_computers_segment }}.243" -SubnetMask "255.255.255.0" -LeaseDuration "8.00:00:00"
      Add-DhcpServerv4Scope -Name "{{ hebrew_site_name }} טלפוניה" -StartRange "{{ DHCP_telephony_segment }}.61" -EndRange "{{ DHCP_telephony_segment }}.243" -SubnetMask "255.255.255.0" -LeaseDuration "8.00:00:00"

But then I received this error:
2020-02-23 13:43:32,770 p=54678 u=ansible n=ansible | PLAY [---------------   Creating VLAN 2,5 IP pools on dhcp1   ---------------] ********************
2020-02-23 13:43:32,784 p=54678 u=ansible n=ansible | TASK [set_fact] ***********************************************************************************
2020-02-23 13:43:32,802 p=54678 u=ansible n=ansible | ok: [dhcp-test]
2020-02-23 13:43:32,807 p=54678 u=ansible n=ansible | TASK [include_vars] *******************************************************************************
2020-02-23 13:43:32,826 p=54678 u=ansible n=ansible | ok: [dhcp-test]
2020-02-23 13:43:32,830 p=54678 u=ansible n=ansible | TASK [include_role : new_branch] ******************************************************************
2020-02-23 13:43:32,852 p=54678 u=ansible n=ansible | ERROR! Syntax Error while loading YAML.
  did not find expected key

The error appears to be in '/etc/ansible/roles/new_branch/tasks/dhcp1_add_pools.yml': line 5, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

    ## Creating Scopes
      Add-DhcpServerv4Scope -Name "{{ hebrew_site_name }}" -StartRange "{{ DHCP_computers_segment }}.61" -EndRange "{{ DHCP_computers_segment }}.243" -SubnetMask "255.255.255.0" -LeaseDuration "8.00:00:00"
      ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

2020-02-23 13:43:32,853 p=54678 u=ansible n=ansible | PLAY RECAP ****************************************************************************************
2020-02-23 13:43:32,853 p=54678 u=ansible n=ansible | dhcp-test                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Any suggestions? Why is this happening? it should be pretty straight forward.

Dick Visser

unread,
Feb 23, 2020, 9:06:45 AM2/23/20
to ansible...@googlegroups.com
Hii

You are trying to execute multiple commands with the win_shell module,
but according to the docs
(https://docs.ansible.com/ansible/latest/modules/win_shell_module.html#synopsis)
that support one at a time:

"The win_shell module takes the command name followed by a list of
space-delimited arguments."

Simple solution: use multiple win_shell tasks.

Dick
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/24ab4a52-1113-41f2-bf33-aa3e1ccb4b6a%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Jordan Borean

unread,
Feb 23, 2020, 1:57:56 PM2/23/20
to Ansible Project
It’s totally possible you just need to learn how to do multi lined yaml values. Have a look at the 5th example at https://docs.ansible.com/ansible/latest/modules/win_shell_module.html#examples.

https://yaml-multiline.info/ is a great tool that can help you understand how multi lined strings work in yaml and the difference between folded and literal block style symbols are.
Reply all
Reply to author
Forward
0 new messages