Ansible 事实在一行中输出

Ansible 事实在一行中输出

我需要来自 Ansible 事实的一些信息,因此在 YAML 下创建。但它给出了错误。我的要求是在一行中获得输出。这样我们就可以使用 CSV 或电子表格对其进行过滤。

---
- hosts: all
  become: yes
  tasks:
  - name: Get content of remote server
    shell: echo system {{ inventory_hostname }} {{ crashkernel }} {{ ansible_os_family }}

错误:

+++++++++++++++++
TASK [Get content of remote server] ********************************************************************************************************************************************************************************************
fatal: [ip]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'crashkernel' is undefined\n\nThe error appears to be in 'status.yaml': line 5, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n  - name: Get content of remote server\n    ^ here\n"}

++++++++++++++++

还尝试过yamllint

$ yamllint status.yaml
status.yaml
  3:11      warning  truthy value should be one of [false, true]  (truthy)
  6:81      error    line too long (89 > 80 characters)  (line-length)

答案1

crashkernel本身不是事实,它是ansible_proc_cmdline事实的子项,所以使用

---
- hosts: all
  become: yes
  tasks:
  - name: Get content of remote server
    shell: echo system {{ inventory_hostname }} {{ ansible_proc_cmdline['crashkernel'] }} {{ ansible_os_family }}

请注意,您可以使用 ansible调试echo打印消息而不是在远程端传递消息的模块:

---
- hosts: all
  become: yes
  tasks:
  - name: Get content of remote server
    debug: 
      msg: "system {{ inventory_hostname }} {{ ansible_proc_cmdline['crashkernel'] }} {{ ansible_os_family }}"

另请注意,您可以使用收集事实用于将有关主机的事实收集到包含 JSON 数据的文件中的模块:

ansible localhost -m gather_facts --tree /tmp/facts

然后使用您选择的编程语言或类似的工具杰克提取您想要的信息:

jq '.ansible_facts.ansible_proc_cmdline.crashkernel' /tmp/facts/localhost

相关内容