如何使用Ansible将已注册的变量保存到文件中?
目标:
-
我想收集有关系统中所有PCI总线和设备的详细信息,并将结果保存在某处(例如,使用lspci . 理想情况下,我应该在本地计算机中获得命令结果以供进一步分析) .
-
使用给定标准也可以保存结果 .
我的剧本看起来像这样:
tasks:
- name: lspci Debian
command: /usr/bin/lspci
when: ansible_os_family == "Debian"
register: lspcideb
- name: lspci RedHat
command: /usr/sbin/lspci
when: ansible_os_family == "RedHat"
register: lspciredhat
- name: copy content
local_action: copy content="{{ item }}" dest="/path/to/destination/file-{{ item }}-{{ ansible_date_time.date }}-{{ ansible_hostname }}.log"
with_items:
- lspcideb
- aptlist
- lspciredhat
但只保存 item_name
保存1变量的好问答 - Ansible - Save registered variable to file .
- local_action: copy content={{ foo_result }} dest=/path/to/destination/file
我的问题:
如何保存多个变量并将stdout传输到本地计算机?
1 回答
说明:
您必须在Jinja2表达式中嵌入变量名称以引用它们的值,否则您将传递字符串 . 所以:
这是Ansible的普遍规则 . 出于同样的原因,您使用
{{ item }}
而不是item
,而{{ foo_result }}
不是foo_result
.但是你也使用
{{ item }}
作为文件名,这可能会造成混乱 .因此,您可以使用以下参数来引用变量值:
{{ vars[item] }}
.另一种方法是定义字典: