I received the following data from the setup module:
"ansible_nodename": "3d734bc2a391", "ansible_os_family": "RedHat", "ansible_pkg_mgr": "yum", "ansible_processor": [ "AuthenticAMD", "AMD PRO A10-8700B R6, 10 Compute Cores 4C+6G" ], "ansible_processor_cores": 1, "ansible_processor_count": 1, "ansible_processor_threads_per_core": 1,
I want to retrieve the 1st value of ansible_processor
and use it in a Jinja2 template.
If I use {{ ansible_processor }}
, it's giving me both values:
"AuthenticAMD", "AMD PRO A10-8700B R6, 10 Compute Cores 4C+6G"
But I want only the first one.
With hostvars , you can access variables defined for any host in the play, at any point in a playbook. You can access Ansible facts using the hostvars variable too, but only after you have gathered (or cached) facts.
Using the Ansible playbookThe gather_facts module from the Ansible playbook runs the setup module by default at the start of each playbook to gather the facts about remote hosts. Fetch the Ansible facts and display them using a playbook. Fetching the Ansible facts, filtering them, and displaying them using a playbook.
This module is automatically called by playbooks to gather useful variables about remote hosts that can be used in playbooks. It can also be executed directly by /usr/bin/ansible to check what variables are available to a host. Ansible provides many facts about the system, automatically.
the map is actually a Jinja2 filter and more information can be found per the Jinja2 official documentation: map() Applies a filter on a sequence of objects or looks up an attribute. This is useful when dealing with lists of objects but you are really only interested in a certain value of it.
To get first item of the list:
- debug: msg: "First item: {{ ansible_processor[0] }}"
Or:
- debug: msg: "First item: {{ ansible_processor | first }}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With