无论是 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: 检测文件是否存在的逻辑相关推荐

  1. ansible file 模块

    ansible file 模块 概要 设置文件.符号链接或目录的属性. 或者,删除文件.符号链接或目录. 许多其他模块支持与该模块相同的选项file- 包括复制.模板和组装. 对于 Windows 目 ...

  2. ansible当中模块的使用

    1.command,shell,raw,script模块的作用和区别    command 模块的使用: 去执行一个脚本文件command.sh, command.sh文件的功能是echo " ...

  3. ansible常用模块应用

    ansible常用模块应用 1.shell 和command command和shell模块的区别 command模块的命令不启动shell,是通过ssh执行命令的 command不支持bash特性, ...

  4. python ansible模块_ansible常用模块

    一.ansible常用模块 模块是Ansible执行的最小单位,可以是由Python编写,也可以是Shell编写,也可以是由其他语言编写. 一.ping模块 测试连接可通性,没有参数.通的话返回pon ...

  5. 03 ansible核心模块 之 文件类型模块

    补充说明:ansible软件输出颜色说明 01.绿色信息:查看主机颜色/对主机未做改动 02.黄色信息:对主机数据信息做了修改 03.红色信息:命令执行出错了 04.粉色信息:忠告信息 05.蓝色信息 ...

  6. ansible常用模块(command、copy、file、yum、service、firewalld)

    前言: 本篇博客的内容在上一篇博客ansible搭建的基础上完成: 自动化运维工具Ansible的搭建 一.Ansible的command默认模块的简单使用 1.在server1上执行 [devops ...

  7. 自动化运维---ansible常用模块之文件操作(findreplace模块)

    自动化运维-ansible常用模块之文件操作(find&replace模块) 文章目录 自动化运维---ansible常用模块之文件操作(find&replace模块) 1.find模 ...

  8. ansible常用模块之 -- template模块 – 将文件模板输出到远程服务器

    ansible常用模块之 -- template模块 – 将文件模板输出到远程服务器 template模块 – 将文件模板输出到远程服务器 一.摘要 二.参数 三.示例 template模块 – 将文 ...

  9. python open方法下file模块_python 文件操作

    一.基本概述 基本的文件操作也就常见的几种,创建.打开.读取.写入和关闭文件等.Python中有几个内置模块和方法来处理文件.这些方法在例如os,os.path,shutil和pathlib等等几个模 ...

最新文章

  1. 零基础能学好UI设计吗
  2. C++回顾day01---C++对C的增强
  3. XML 新手最佳入门教程
  4. JS判断请求来自Android手机还是iPhone手机,根据不同的手机跳转到不同的链接。...
  5. log4net 在asp.net WEB应用程序中的配置
  6. Hibernate中把Session和线程绑定的配置
  7. 性能测试分析与调优原理
  8. 微服务的一种开源实现方式——dubbo+zookeeper
  9. Linux 拷贝文件
  10. Arcface 总结
  11. 音频,视频合并软件——ffmpeg下载及使用
  12. java60秒倒计时
  13. 【安全】被黑客要挟的一天,All your data is a backed up. You must pay 0.25BTC
  14. 范渊免职——网安界其人曾异军突起又将“落幕”?
  15. 为什么128KB的魂斗罗可以塞下这么长的剧情?
  16. 多租户数据隔离的三种方案
  17. vue项目 设置scrollTop不起作用(解决方法及原因)
  18. vue组件中的data为什么是一个函数
  19. (Talking face) EVP
  20. 攻防世界- CRYPTO -练习区12题解

热门文章

  1. python观察日志(part10)--__future__ 模块
  2. python高级教程html文件_Python之html转docx文件高级用法
  3. Python 开发桌面小工具,让代码替我们干重复的工作!
  4. 如何修改 SAP Spartacus CMS API 默认的 endpoint
  5. 使用Angular reactive form发送HTTP请求的一个简单例子
  6. Chrome开发者工具Element style里的Computed标签页
  7. Comments on task “Smart Service II: Wrap up and make it ready for Demo“
  8. SAP Spartacus PagelayoutComponent里的template
  9. Scala里的控制台输出print实现原理
  10. 如何将csv包含的数据导入SAP Cloud Platform HANA MDC里