如何从Ansible中设置模块的输出中获取列表的第一个元素?
我从设置模块收到以下数据:如何从Ansible中设置模块的输出中获取列表的第一个元素?,ansible,jinja2,ansible-facts,ansible-template,Ansible,Jinja2,Ansible Facts,Ansible Template,我从设置模块收到以下数据: "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_c
"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,
我想检索ansible\u processor
的第一个值,并在Jinja2模板中使用它
如果我使用{{ansible\u processor}}
,它会给我两个值:
"AuthenticAMD",
"AMD PRO A10-8700B R6, 10 Compute Cores 4C+6G"
但我只想要第一个。要获取列表的第一项:
- debug:
msg: "First item: {{ ansible_processor[0] }}"
或:
尝试以下常见方法来处理此情况: 参考:
- debug:
msg: "First item: {{ ansible_processor | first }}"
# from list
- debug:
msg: "First item: {{ ansible_processor[0] }}"
# from output, like 'https://xxx.xx/xxx/xxx.git'
- debug:
msg: "git repo's name: {{ (item| urlsplit('path')| basename | splitext)[0] }}"