![Ansible 注册变量到日志文件](https://linux22.com/image/747261/Ansible%20%E6%B3%A8%E5%86%8C%E5%8F%98%E9%87%8F%E5%88%B0%E6%97%A5%E5%BF%97%E6%96%87%E4%BB%B6.png)
我正在尝试运行 yum list updates 命令并将结果输出到本地服务器上的文件中。这将扩展到在多台服务器上运行。我找到了 local_action 模块,但这会导致文件无法读取。我发现 -debug: var: result 命令可以以良好的格式显示必要的信息,但似乎没有办法将其转储到文件中。
以下是剧本:
---
- hosts: localhost
tasks:
- name: List all available updates
yum:
list: updates
register: result
- debug:
var: result
- local_action:
module: copy
content: "{{ result.results }}"
dest: "/root/yumlist.yml"
调试任务返回如下结果:*
ok: [localhost] => {
"result": {
"changed": false,
"failed": false,
"results": [
{
"arch": "x86_64",
"envra": "0:firefox-68.4.1-1.el7_7.x86_64",
"epoch": "0",
"name": "firefox",
"release": "1.el7_7",
"repo": "rhel-7-server-rpms",
"version": "68.4.1",
"yumstate": "available"
},
{
"arch": "x86_64",
"envra": "0:fribidi-1.0.2-1.el7_7.1.x86_64",
"epoch": "0",
"name": "fribidi",
"release": "1.el7_7.1",
"repo": "rhel-7-server-rpms",
"version": "1.0.2",
"yumstate": "available"
},
并且 local_action 给了我这个......
[{“envra”:“0:firefox-68.4.1-1.el7_7.x86_64”,“名称”:“firefox”,“repo”:“rhel-7-server-rpms”,“epoch”:“0”,“版本”:“68.4.1”,“发布”:“1.el7_7”,“yumstate”:“可用”,“arch”:“x86_64”},{“envra”:“0:fribidi-1.0.2-1.el7_7.1.x86_64”,“名称”:
有任何想法吗?
答案1
如果您不介意 json 或 yaml 输出而只是希望看起来更漂亮,您可以尝试几个过滤器(to_nice_json 或 to_nice_yaml)参考。
- local_action:
module: copy
content: "{{ result.results |to_nice_json }}"
dest: "/root/yumlist.yml"
或者
- local_action:
module: copy
content: "{{ result.results |to_nice_yaml }}"
dest: "/root/yumlist.yml"