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: 检测文件是否存在的逻辑的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nature|GPT-4 被吹爆,科学家
- 下一篇: 问界门店员工 不让叫华为问界了:余承东凌