Redhat

Ansible 將變數註冊到日誌文件

  • January 14, 2020

我正在嘗試執行 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”,“名稱”:

有任何想法嗎?

如果您不介意 json 或 yaml 輸出並且只想看起來更好,那麼您可以嘗試使用幾個過濾器 (to_nice_json, or to_nice_yaml) Ref

 - 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"

引用自:https://serverfault.com/questions/998979