Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the first element of a list from the output of setup module in Ansible?

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.

like image 696
thinkingmonster Avatar asked Jan 12 '17 10:01

thinkingmonster


People also ask

What is Hostvars Ansible?

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.

How do you use Ansible gather 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.

What is gather facts in Ansible 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.

What is map in Ansible?

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.


1 Answers

To get first item of the list:

- debug:     msg: "First item: {{ ansible_processor[0] }}" 

Or:

- debug:     msg: "First item: {{ ansible_processor | first }}" 
like image 132
techraf Avatar answered Sep 23 '22 08:09

techraf