我制作了一个脚本mod来使远程计算机工作,但是有些计算机工作不同

时间:2019-01-23 06:36:20

标签: ansible

当我使用外壳程序脚本运行ansible剧本脚本mod时,我不想在stdout_lines第一个元素中得到答案“ \ r \ n”。

某些计算机先返回\ r \ n

shell脚本:


#!/bin/bash
echo "Hi"

剧本:


---
- hosts: test
  tasks: 
    - script: echoTest.sh
      register: out
    - debug:
        var: out

most are the expected answer:
    "out": {
        "changed": true, 
        "failed": false, 
        "rc": 0, 
        "stderr": "Shared connection to 192.168.184.141 closed.\r\n", 
        "stderr_lines": [
            "Shared connection to 192.168.184.141 closed."
        ], 
        "stdout": "Hi\r\n", 
        "stdout_lines": [
            "Hi"
        ]
    }

但是有一些意外的答案:


    "out": {
        "changed": true, 
        "failed": false, 
        "rc": 0, 
        "stderr": "Shared connection to 192.168.184.141 closed.\r\n", 
        "stderr_lines": [
            "Shared connection to 192.168.184.141 closed."
        ], 
        "stdout": "\r\nHi\r\n", 
        "stdout_lines": [
            "", 
            "Hi"
        ]
    }

我不希望stdout_lines中的元素“”,如何计算?

2 个答案:

答案 0 :(得分:0)

为帮助调试,我建议您检查“某些计算机”“ echo”命令,例如, 手动登录并运行“ echo”(检查“哪个回声”和“别名回声”)和/或硬编码哪个回声在脚本中运行:

#!/bin/bash
/bin/echo "Hi"

答案 1 :(得分:0)

真正的剧本是:


- hosts: test
  become_user: root
  become: yes
  tasks: 
    - script: echoTest.sh
      register: out
    - debug:
        var: out

如果我在任务之外使用become: yes,则某些计算机将收到错误答案, buf如果我在任务中使用become: yes,就可以了,就像下面这样:


- hosts: test
  become_user: root
  tasks: 
    - become: yes
      script: echoTest.sh
      register: out
    - debug:
        var: out

哦,我不确定这是一个错误,我找不到其他更好的方法来解决它。