Ansible 2.8.7 API 使用异常4:playbook执行powershell脚本返回值“\r\n”,原因分析及解决

Q1: Ansible2.8.7和2.7.9通过Playbook执行powershell脚本返回值问题, 系统环境一样

Q2: 如果get_w_sysinfo.ps1里直接写内置命令,如ipconfig,又能正常返回输出,原因不详

#1. get_w_sysinfo.ps1

get-wmiobject win32_computersystem |Select-Object Name,Domain,SystemType,Model

#2.get_w_sysinfo.yml

- name: test script module
  hosts: winclient
  gather_facts: F
  tasks:
    - name: run test script
      script: /etc/ansible/scripts/windows/get_w_sysinfo.ps1
      register: ps_out
    - name: show
      debug: var=ps_out.stdout verbosity=0

#3.执行

[root@CENTOS768-1810 ~]#  ansible-playbook playbook/get_w_sysinfo.yml

#4.Ansible2.8.7执行结果

[root@CENTOS768-1810 ~]# ansible-playbook playbook/get_w_sysinfo.yml -vvvvv

    PLAY [test script module] *********************************************************************************************************************************************************************
    
    TASK [run test script] ************************************************************************************************************************************************************************
    changed: [BJ-s110.xx.xx.com]
    
    TASK [show] ***********************************************************************************************************************************************************************************
    ok: [BJ-s110.xx.xx.com] => {
        "ps_out.stdout": "\r\n"
    }
    
    PLAY RECAP ************************************************************************************************************************************************************************************
    BJ-s110.xx.xx.com              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

#5. Ansible2.7.9执行结果

[root@CENTOS768-1810 ~]#  ansible-playbook playbook/get_w_sysinfo.yml -vvvvv

    PLAY [test script module] ******************************************************
    
    TASK [run test script] *********************************************************
    changed: [BJ-s110.xx.xx.com]
    
    TASK [show] ********************************************************************
    ok: [BG-S110.xx.xx.com] => {
        "ps_out.stdout": "\r\n\r\nName                      : BG-S110\r\nDomain                    : xx.xx.com\r\nSystemType                : x64-based PC\r\nModel                     : To be filled by O.E.M.\r\nHypervisorPresent         : True\r\nInstallDate               : \r\nNumberOfLogicalProcessors : 2\r\nNumberOfProcessors        : 1\r\nProductID                 : \r\n\r\n\r\n\r\n"
    }
    
    PLAY RECAP *********************************************************************
    BJ-s110.xx.xx.com : ok=2    changed=1    unreachable=0    failed=0   

#6. Ansible2.8.7解决方法

# 经观摩官方.ps1文件,发现返回为JSON数据,所以尝试以下:
#!powershell
$result = get-wmiobject win32_computersystem |Select-Object Name,Domain,UserName,SystemType,Model,HypervisorPresent,InstallDate
$result | ConvertTo-Json  -Depth 4

你可能感兴趣的:(Ansible2.8.7)