Ansible:是否可以在播放剧本而不是调试时“cat 文件”并将其输出导出到屏幕?

Ita*_*not 24 debug ansible ansible-playbook

我写了一个剧本,为每个用户安装和配置 Google Authenticator。

我想要catgoogle_authenticator 配置文件的剧本的最后一步。

使用“调试”模块,我能够将数据显示在屏幕上,但只能作为调试消息:

TASK: [debug var=details.stdout_lines] ****************************************
ok: [localhost] => {
    "details.stdout_lines": [
        "ZKMFTE2ADYA2OYCH",
        "\"RATE_LIMIT 3 30",
        "\" DISALLOW_REUSE",
        "\" TOTP_AUTH",
        "12920994",
        "88224784",
        "69464205",
        "38144121",
        "45634120"
    ]
}
Run Code Online (Sandbox Code Playgroud)

我在网上读到我可以做这样的事情:

  - name: Print to screen google authenticator details
    command: /bin/cat {{ google_authenticator_secret_file_location }}
    register: details
    tags: google_2fa_user

  - debug: msg="{{ details.stdout_lines }}"
Run Code Online (Sandbox Code Playgroud)

但是当我运行它时出现错误:

TASK: [Print to screen google authenticator details] **************************
changed: [localhost]

TASK: [debug msg="{{details.stdout_lines}}"] **********************************
fatal: [localhost] => Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 532, in _executor
    exec_rc = self._executor_internal(host, new_stdin)
  File "/usr/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 629, in _executor_internal
    return self._executor_internal_inner(host, self.module_name, self.module_args, inject, port, complex_args=complex_args)
  File "/usr/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 815, in _executor_internal_inner
    result = handler.run(conn, tmp, module_name, module_args, inject, complex_args)
  File "/usr/lib/python2.7/dist-packages/ansible/runner/action_plugins/debug.py", line 41, in run
    kv = utils.parse_kv(module_args)
  File "/usr/lib/python2.7/dist-packages/ansible/utils/__init__.py", line 526, in parse_kv
    vargs = [x.decode('utf-8') for x in shlex.split(args, posix=True)]
  File "/usr/lib/python2.7/shlex.py", line 279, in split
    return list(lex)
  File "/usr/lib/python2.7/shlex.py", line 269, in next
    token = self.get_token()
  File "/usr/lib/python2.7/shlex.py", line 96, in get_token
    raw = self.read_token()
  File "/usr/lib/python2.7/shlex.py", line 172, in read_token
    raise ValueError, "No closing quotation"
ValueError: No closing quotation


FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************
Run Code Online (Sandbox Code Playgroud)

错误说:“没有结束语”虽然它被引用了。还试过:

 - debug: msg= "{{ details.stdout_lines }}"
Run Code Online (Sandbox Code Playgroud)

知道可能是什么问题吗?

Hen*_*gel 5

报价神社过滤器就可以解决这个问题引用。像这样使用它:

  - debug: msg="{{ details.stdout_lines | quote }}"
Run Code Online (Sandbox Code Playgroud)

对于另一个问题,我不知道有一个模块可以打印debug模块以外的语句。您可能想检查是否可以选择将注册的变量保存到文件。如果您想在控制器主机上存储 Ansible 变量,可以执行以下操作:

- local_action: copy content={{ details.stdout_lines }} dest=/path/to/destination/file
Run Code Online (Sandbox Code Playgroud)

编辑我需要纠正自己一点。看看这个 serverfault 问题。您可以使用该callback.display函数调整 Ansible 输出。我建议阅读链接的博客文章


Ita*_*not 1

我在互联网上进行了深入研究,并咨询了一些 Ansible 专业人士。

据我了解,Ansible 1.8 中没有这样的选项可以将命令的输出重定向到屏幕作为正常输出而不是调试输出。

  • 对于 Ansible 2.2,除了使用调试之外,仍然没有其他选项可以打印到屏幕。 (2认同)