ansible对文件内容操作

bash;gutter:true; ansible lineinfile 简介 lineinfile该模块是操作文件中的每一行内容,他是按照行为单位的,和下面的replace模块并不冲突。</p> <p>修改匹配行,如果不存在就会添加 tasks: - name: Ensure SELinux is set to enforcing mode lineinfile: path: /etc/selinux/config regexp: '^SELINUX=' line: SELINUX=enforcing</p> <p>把 SELINUX=这个开头的行直接替换成SELINUX=enforcing不管后面是什么,直接替换整行内容。</p> <p>删除文件中的行 - name: 确保sudoers配置中没有wheel组。 lineinfile: path: /etc/sudoers state: absent regexp: '^%wheel'</p> <p>在匹配行前添加一行内容,并确保插入成功 - name: Ensure the default Apache port is 8080 lineinfile: path: /etc/httpd/conf/httpd.conf regexp: '^Listen ' //确保添加完成 insertafter: '^#Listen ' //要在哪一行前面添加 line: Listen 8080 //添加的内容</p> <p>在匹配行后添加一行内容,并确保插入成功 - name: Ensure we have our own comment added to /etc/services lineinfile: path: /etc/services regexp: '^# port for http' //确保添加完成 insertbefore: '^www.*80/tcp' //要在哪一行后面添加 line: '# port for http by default' //添加的内容是什么</p> <p>修改文件并更改权限 - name: Replace a localhost entry with our own lineinfile: path: /etc/hosts regexp: '^127.0.0.1' line: 127.0.0.1 localhost owner: root group: root mode: '0644'</p> <p>文件存在就添加一行内容,如果内容存在就不会添加(多次执行不会重复添加) - name: add a line lineinfile: dest: /etc/hosts line: '10.1.1.1 zhangshoufu.com'</p> <p>ansible replace(非核心模块) 介绍 replace模块可以根据我们指定的正则表达式替换匹配到的字符串,文件中所有被匹配到的字符串都会被替换,和lineinfile不同的地方是replace只会替换正则表达式匹配到的内容,而lineinfile是替换正则表达式匹配到行的内容。</p> <p>常用参数 path: 文件路径,我们要替换那个文件内的内容,必须 regexp:正则表达式,必要参数 replace: 替换成的内容 替换文件内容 tasks: - name: '替换zsf 字符串为zhangshoufu' replace: path: /tmp/test.txt regexp: 'zsf' replace: 'zhangshoufu'</p> <p>注释 Apache 配置文件/etc/apache2/sites-available/default.conf中NameVirtualHost [<em>]行之后的所有内容: - name: Replace after the expression till the end of the file (requires Ansible >= 2.4) replace: path: /etc/apache2/sites-available/default.conf after: 'NameVirtualHost [</em>]' regexp: '^(.+)$' replace: '# \1'</p> <p>注释 Apache 配置文件/etc/apache2/sites-available/default.conf中# live site config行之前的所有内容: - name: Replace before the expression till the begin of the file (requires Ansible >= 2.4) replace: path: /etc/apache2/sites-available/default.conf before: '# live site config' regexp: '^(.+)$' replace: '# \1'</p> <p>注释 Apache 配置文件/etc/apache2/sites-available/default.conf中行和行之间的内容: Prior to Ansible 2.7.10, using before and after in combination did the opposite of what was intended.</p> <p>see https://github.com/ansible/ansible/issues/31354 for details.</p> <ul> <li>name: Replace between the expressions (requires Ansible >= 2.4) replace: path: /etc/apache2/sites-available/default.conf after: '' before: '' regexp: '^(.+)$' replace: '# \1'</li> </ul> <p>删除jdoe用户 SSH 的known_hosts文件中的old.host.name及之后的空行,同时修改文件属性和权限: - name: Supports common file attributes replace: path: /home/jdoe/.ssh/known_hosts regexp: '^old.host.name[^\n]*\n' owner: jdoe group: jdoe mode: '0644'</p> <p>修改/etc/apache/ports文件并校验: - name: Supports a validate command replace: path: /etc/apache/ports regexp: '^(NameVirtualHost|Listen)\s+80\s*$' replace: '\1 127.0.0.1:8080' validate: '/usr/sbin/apache2ctl -f %s -t'

Original: https://www.cnblogs.com/The-day-of-the-wind/p/16265729.html
Author: MlxgzZ
Title: ansible对文件内容操作

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/582063/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 代码审计-Typecho反序列化getshell

    0x01 漏洞代码 install.php&#xFF1A; php $config = unserialize(base64_decode(Typecho_Cookie::…

    Linux 2023年5月28日
    093
  • Conky配置(中文备注)

    conkyrc地址:~/.conkyrc 需要注意的是,因为每个人的网卡都不同,所以在网络部分,例如 downspeed wlp0s20f3 ,后面的 wlp0s20f3 每个人都…

    Linux 2023年6月7日
    069
  • 【Example】C++运算符重载

    首先,阅读之前要先搞清楚什么是运算符、函数重载。函数重载就是在一个范围内为一个函数声明多个实现方式,函数名必须一致。 那么C++运算符是否可以重载呢?可以!先弄清什么时候需要进行运…

    Linux 2023年6月13日
    0110
  • SlugRelatedField字段

    该字段用于外键字段该字段在序列化的时候多用于反向查询,在反序列化的时候用于接收关联表的唯一字段来生成该关联对象eg: 序列化 class PublishListSerializer…

    Linux 2023年6月14日
    0102
  • UWP graphql-dotnet新版数据查询客户端的实现

    之前写过一篇文章 UWP GraphQL数据查询客户端的实现,这次的标题基本差不多,只不过微软这个graphql-dotnet repo从1.x升级到了3.x,跨度有点大。 如果你…

    Linux 2023年6月13日
    090
  • [ Python ] PyQt5 PySide2 笔记

    https://www.cnblogs.com/yeungchie/ PyQt5 from PyQt5.QtWidgets import * from PyQt5.QtCore i…

    Linux 2023年6月7日
    098
  • Lua集成Redis及Nginx

    1 Lua介绍 Lua是一门以其性能著称的脚本语言,被广泛应用在很多方面。Lua一般用于嵌入式应用,现在越来越多应用于游戏 当中,魔兽世界,愤怒的小鸟都有用到。优势 Lua极易嵌入…

    Linux 2023年6月13日
    079
  • MybatisPlus——全网配置最全的代码生成器

    MybatisPlus代码生成器 这里讲解的是新版 (mybatis-plus 3.5.1+版本),旧版不兼容 官方文档:https://baomidou.com/(建议多看看官方…

    Linux 2023年6月7日
    0113
  • Conda虚拟环境中的pip,python 等路径是base环境而非本虚拟环境

    现象 一次运行项目发现,原本可以正常运行的项目,突然提示有个包不存在,但是经过 pip list 发现在我的虚拟环境中是存在这个包的,并且此时我是正常的位于我的虚拟环境中。 报错:…

    Linux 2023年6月7日
    097
  • redis用法分析

    redis也是一个内存非关系型数据库,它拥有memcache在数据存储上的全部优点,而且在memcache的基础上增加了数据持久性功能,redis用rdb和aof两种方式实现数据持…

    Linux 2023年5月28日
    085
  • 多级缓存-redis缓存预热

    冷启动:服务刚刚启动时,Redis中并没有缓存,如果所有商品数据都在第一次查询时添加缓存,可能会给数据库带来较大压力。 缓存预热:在实际开发中,我们可以利用大数据统计用户访问的热点…

    Linux 2023年5月28日
    090
  • jenkins自动触发构建

    bash;gutter:true; 1. 安装jenkins cat /etc/yum.repos.d/jenkins.repo [jenkins] name=Jenkins ba…

    Linux 2023年6月7日
    077
  • 启动springboot项目很慢的解决方案-InetAddress.getLocalHost().getHostName()

    https://blog.csdn.net/qq_39595769/article/details/119573111 Original: https://www.cnblogs….

    Linux 2023年6月13日
    097
  • GCC 内联汇编基础

    GCC 内联汇编 在 MIT6.828的实验中,有几处用到了很底层的函数,都以内联汇编的形式存在,例如 static inline uint32_t read_esp(void) …

    Linux 2023年6月8日
    087
  • 网络设备配置–9、利用ppp协议实现点对点认证

    一、前言 同系列前几篇:网络设备配置–1、配置交换机enable、console、telnet密码网络设备配置–2、通过交换机划分vlan网络设备配置&#8…

    Linux 2023年6月8日
    090
  • CentOS导入CA证书

    把CA证书放到如下目录 /etc/pki/ca-trust/source/anchors 再命令行运行 /bin/update-ca-trust 如下所示的操作步骤 删除证书只需要…

    Linux 2023年6月6日
    096
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球