Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

jinja2 - 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.

question from:https://stackoverflow.com/questions/41610207/how-to-get-the-first-element-of-a-list-from-the-output-of-setup-module-in-ansibl

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To get first item of the list:

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

Or:

- debug:
    msg: "First item: {{ ansible_processor | first }}"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...