打印不带引号/转义符的 Ansible 调试输出

打印不带引号/转义符的 Ansible 调试输出

我需要知道是否有一种方法可以从 ansible playbook 打印信息,而不会导致 ansible 自动引用和转义字符串。例如,我正在使用 ansible 构建一个应该从另一台机器单独运行的命令(我知道这很讽刺,但无论如何都是必要的)。目前输出看起来像...

"command" = "run_me.sh \"with this argument\""

我需要的是类似这样的东西......

"command" = run_me.sh "with this argument"

要不就...

run_me.sh "with this argument" 

如果可能的话,但我猜这要求太高了。

我目前正在使用 set_fact 来构建命令并进行调试以打印它。

答案1

您可以编写自己的 stdout 回调插件或者使用以下一些技巧:

---
- hosts: localhost
  gather_facts: no
  tasks:

    # Print as loop item
    - name: Print command as loop item
      set_fact:
        dummy: value # Just to make some task without output
      with_items:
        - 'Execute this: run_me.sh "with this argument"'

    # Print as task title
    # Not suitable for different commands per host, because task title is common for all hosts
    - name: 'Execute this: run_me.sh "with this argument"'
      debug:
        msg: Execute command from task title

    # Print as pause statement
    # Not suitable for different commands per host, because pause task skips host loop (forced run_once:yes)
    - name: Print command as pause statment
      pause:
        prompt: 'Execute this and press enter: run_me.sh "with this argument"'

输出:

TASK [Print command as loop item] **********************************************
ok: [localhost] => (item=Execute this: run_me.sh "with this argument")

TASK [Execute this: run_me.sh "with this argument"] ****************************
ok: [localhost] => {
    "msg": "Execute command from task title"
}

TASK [Print command as pause statment] *****************************************
[Print command as pause statment]
Execute this and press enter: run_me.sh "with this argument":
ok: [localhost]

相关内容