日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ansible file模块_Ansible: 检测文件是否存在的逻辑

發布時間:2023/12/19 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ansible file模块_Ansible: 检测文件是否存在的逻辑 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

無論是 Python 還是 Ansible, 最重要的就是實現功能的邏輯。

對于網絡工程師來說,Python 和 Ansible是非常便捷的工具和編程思維實現的橋梁,我們在下面的文章將進行一個常見的案例分享。

Requirements

很簡單,需求如下:

  • 我們需要實現在 Linux 系統中判斷文件是否存在。
  • 如果這個文件存在,在界面打印出來 “File exists.”
  • 如果這個文件不存在,在界面打印出來 “File not found.” 然后創建這個文件。

Background

我們了解 Linux Bash 有類似于 'stat' 的命令,來用作調用文件的狀態。

那么在 Ansible 中, 我們也可以使用 stat 模塊來實現相對應的邏輯功能。

如果目標設備是 Windows,我們可以使用 win_stat 模塊來實現這個功能。

官方文檔鏈接如下,在官方文檔中,還有一些常見的使用場景,非常實用。

https://docs.ansible.com/ansible/latest/modules/stat_module.html?docs.ansible.com

案例分享

STEP01: 首先我們創建一個 task 來檢查這個文件是否存在

- name: Check that the devnet.md existsstat:path: /etc/devnet.mdregister: file_status

然后我們 debug 一下注冊的這個變量 file_status

- name: file_status - display on screendebug:msg: "{{ file_status }}"

這個 task 的輸出如下,我們可以看到輸出了關于這個變量的全部 facts

TASK [create_delete_folder : Check that the devnet.md exists] ************** ok: [127.0.0.1]TASK [create_delete_folder : file_status - display on screen] **************"msg": {"changed": false, "failed": false, "stat": {"atime": 1592494532.591655, "attr_flags": "", "attributes": [], "block_size": 4096, "blocks": 0, "charset": "binary", "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "ctime": 1592494515.0875323, "dev": 64768, "device_type": 0, "executable": false, "exists": true, "gid": 0, "gr_name": "root", "inode": 8390764, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mimetype": "inode/x-empty", "mode": "0644", "mtime": 1592494515.0875323, "nlink": 1, "path": "/etc/devnet.md", "pw_name": "root", "readable": true, "rgrp": true, "roth": true, "rusr": true, "size": 0, "uid": 0, "version": "194095774", "wgrp": false, "woth": false, "writeable": true, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}} } ok: [127.0.0.1] => {

STEP02: 我們創建一個 task 來使用 when 來檢查這個文件是否存在

如果這個文件存在,就打印出 "File exists."

如果這個文件不存在, 就打印出 "File not found."

- name: Check that if the file devnet.md existsdebug:msg: "File exists."when: file_status.stat.exists == True- name: Check that if the file devnet.md not existsdebug:msg: "File not found."when: file_status.stat.exists == False

如果這個文件存在的話,將會輸出 msg。

當然下面的這個 task 就會被忽略, 顯示為 skipping。

TASK [create_delete_folder : Check that if the file devnet.md exists] *********** ok: [127.0.0.1] => {"msg": "File exists." }TASK [create_delete_folder : Check that if the file devnet.md not exists] ******* skipping: [127.0.0.1]

STEP03: 我們創建一個 task 來使用創建這個文件 devnet.md

- name: Create the file, if it doesnt exist alreadyfile:path: /etc/devnet.mdstate: touchwhen: file_status.stat.exists == False

運行這個 task 的時候如果沒有相對應的文件,就會創建文件。

如果這個文件已經存在,這個 task 就會被忽略, 顯示為 skipping。

TASK [create_delete_folder : Create the file, if it doesnt exist already] ***** skipping: [127.0.0.1]

綜上所述,我們一共使用 ansible 這個工具通過簡單的三個步驟就實現了這個邏輯。


歡迎關注,如果大家有一些 ansible 邏輯點子也歡迎私信給我,讓我們共同學習,一起分享。

總結

以上是生活随笔為你收集整理的ansible file模块_Ansible: 检测文件是否存在的逻辑的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。