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)

大家都在看

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