
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", "name": "firefox", "repo": "rhel-7-server-rpms", "epoch": "0", "version": "68.4.1", "release": "1.el7_7", "yumstate": "available", "arch" : "x86_64"}, {"envra": "0:fribidi-1.0.2-1.el7_7.1.x86_64", "name":
何か案は?
答え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"