0

I need some information from Ansible facts, hence created below YAML. but its giving error. My requirement is to get the output in one line. So that we can filter it using CSV or spreadsheet.

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

Error:

+++++++++++++++++
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"}

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

Also tried 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 Answer 1

1

crashkernel is not a fact itself, it's a subkey of the ansible_proc_cmdline fact, so use

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

Note that you can use the ansible debug module to print messages instead of going through echo on the remote side:

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

Note also that you can use the gather_facts module to collect facts about a host into a file containing JSON data:

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

and then use your programming language of choice or a tool like jq to extract the information you want:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .