【问题标题】:Getting the output of just "stdout_lines" or "stdout" when running an ansible ad-hoc command运行 ansible ad-hoc 命令时仅获取 \"stdout_lines\" 或 \"stdout\" 的输出
【发布时间】:2022-09-25 17:12:13
【问题描述】:

我正在尝试运行以下运行 \"status.sh\" 脚本的 ansible ad-hoc 命令:

ansible host -m script -a \'/path/status.sh\' -u root -i inventory

该脚本只是获取目标主机上服务的状态,如下所示:

service_1=$(ls /etc/systemd/system | grep -e jboss | awk -F \' \' \'{print $1}\')

if [ ! -z \"$service_1\" ] //if service exists
then
      systemctl status $service_1
else
      echo \"There is No $Service_1 Here !\"
fi

运行临时命令时输出太多,我只想将输出限制为stdout_linesstdout,有没有办法在不使用debug 或任何创建特定剧本的情况下做到这一点其他模块只需添加一个选项或将输出管道传输到 grep?

  • 因为您的实际示例和用例目前看起来像是 Ansible 的反模式并且应该避免,您能解释一下为什么要寻找 \"...一种无需使用“调试”或任何其他模块创建特定剧本的方法,只需添加一个选项或将输出管道传输到 grep ...\"?
  • Change the ad-hoc command line stout callback plugin to json。使用像jq 这样的工具过滤输出是微不足道的。
  • 感谢@U880D 的输入,我只是想知道是否有办法这样做,我知道可以通过注册输出然后使用“调试”模块打印想要的部分来使用剧本,我\'已经尝试了提供的答案并且它有效。
  • 感谢@Zeitounator 的输入,我只是想知道是否有一种方法可以在不使用剧本或任何插件的情况下仅通过使用本机选项来做到这一点,我已经尝试了提供的答案并且它有效。

标签: linux ansible


【解决方案1】:

除非模块本身(例如 setup 模块)支持,否则 ansible adoc 命令没有内置选项来过滤输出。

如果要在单行中压缩输出,可以使用“-o”选项。

唯一的选择似乎是与 shell 一起过滤输出。我想出了下面的命令,你可以试一试

ansible test_node1 -m script -a "/root/python/test.sh" | grep "\"stdout\":" | tr -d  ','  | echo -e `awk '{print $2}'`

【讨论】:

  • 我已经尝试了建议的方法,并且只修改了最后一部分以按照下面的要求限制输出变量,它可以工作,谢谢。
  • ansible test_node1 -m script -a "/root/python/test.sh" -u root -i inventory | grep "\"stdout\":" | tr -d ',' |回声-eawk '{print $1 $2 $3 $4 $12 $16 $17 $18}'
【解决方案2】:

在我看来,在原始问题中尝试了收集服务状态的方法应该避免,因为该示例似乎是反模式对于 Ansible。但是,有一些选项可以满足报告的要求。

一个更简单的解决方案是

ansible test -m systemd -a 'name=cntlm enabled=true'
test.example.com | SUCCESS => {
    "changed": false,
    "enabled": true,
    "name": "cntlm",
    "status": {
<a lot of output>
    }
}

或者

ansible test -m shell -a 'systemctl status cntlm'
test.example.com | CHANGED | rc=0 >>
● cntlm.service - CNTLM HTTP Accelerator For NTLM Secured Proxies Authenticator
   Loaded: loaded (/usr/lib/systemd/system/cntlm.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2022-09-01 09:00:00 CEST; 1 weeks 4 days ago
 Main PID: 234567 (cntlm)
   CGroup: /system.slice/cntlm.service
           └─234567 /usr/sbin/cntlm -c /etc/cntlm.conf -U cntlm -P /run/cntlm/cntlmd.pid

或几乎要求,启用callback plugin for ad hoc commands

ansible test -m shell -a 'systemctl status cntlm'

PLAY [Ansible Ad-Hoc] **************************************************************************************************
Monday 01 September 2022  09:00:00 +0200 (0:00:00.071)       0:00:00.071 ******

TASK [shell] ***********************************************************************************************************
changed: [test.example.com]

PLAY RECAP *************************************************************************************************************
test.example.com  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Monday 01 September 2022  09:00:00 +0200 (0:00:02.171)       0:00:02.243 ******
===============================================================================
shell ----------------------------------------------------------------------------------------------------------- 2.17s
Playbook run took 0 days, 0 hours, 0 minutes, 2 seconds

配置

[defaults]
bin_ansible_callbacks   = True

进一步问答


最后,如果您现在对更多地自定义输出感兴趣,您可能需要启动Developing plugins 并创建自己的Callback pluginlib/ansible/plugins/callback/default.py 的来源可以是一个好的开始。

【讨论】:

    【解决方案3】:

    有没有一种方法可以做到这一点,而无需通过添加选项或将输出管道输出到 grep 来创建带有调试或任何其他模块的特定剧本?

    我很确定我在评论中的上述提议符合您的期望。唯一的要求是在控制器上安装一个工具来解析 json,以便您可以将 Ansible 输出结果通过管道传输到您的 shell 中。

    对于下面的示例,我使用jq,它在大多数 Linux 发行版存储库中广泛使用和可用。

    这里的诀窍是:

    1. enable loading callbacks for ad-hoc commands
    2. change the stdout callback plugin 将 json 用于临时命令。

      由于您似乎在寻找一种单一且非“破坏性”的解决方案,因此我使用了直接在同一命令行上设置的环境变量。

      长话短说,在你的 shell 中输入:

      ANSIBLE_LOAD_CALLBACK_PLUGINS=1 \
      ANSIBLE_STDOUT_CALLBACK=json \
      ansible localhost -a "echo toto" | jq -r ".plays[].tasks[].hosts[].stdout"
      

      toto
      

    【讨论】:

      猜你喜欢
      • 2020-05-16
      • 1970-01-01
      • 2020-06-08
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2011-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多