ansible lineinfile
简介
lineinfile该模块是操作文件中的每一行内容,他是按照行为单位的,和下面的replace模块并不冲突。 修改匹配行,如果不存在就会添加
tasks:
- name: Ensure SELinux is set to enforcing mode
lineinfile:
path: /etc/selinux/config
regexp: '^SELINUX='
line: SELINUX=enforcing 把 SELINUX=这个开头的行直接替换成SELINUX=enforcing不管后面是什么,直接替换整行内容。 删除文件中的行
- name: 确保sudoers配置中没有wheel组。
lineinfile:
path: /etc/sudoers
state: absent
regexp: '^%wheel' 在匹配行前添加一行内容,并确保插入成功
- name: Ensure the default Apache port is 8080
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen ' //确保添加完成
insertafter: '^#Listen ' //要在哪一行前面添加
line: Listen 8080 //添加的内容 在匹配行后添加一行内容,并确保插入成功
- 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' //添加的内容是什么 修改文件并更改权限
- 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' 文件存在就添加一行内容,如果内容存在就不会添加(多次执行不会重复添加)
- name: add a line
lineinfile:
dest: /etc/hosts
line: '10.1.1.1 zhangshoufu.com' ansible replace(非核心模块)
介绍
replace模块可以根据我们指定的正则表达式替换匹配到的字符串,文件中所有被匹配到的字符串都会被替换,和lineinfile不同的地方是replace只会替换正则表达式匹配到的内容,而lineinfile是替换正则表达式匹配到行的内容。 常用参数
path: 文件路径,我们要替换那个文件内的内容,必须
regexp:正则表达式,必要参数
replace: 替换成的内容
替换文件内容
tasks:
- name: '替换zsf 字符串为zhangshoufu'
replace:
path: /tmp/test.txt
regexp: 'zsf'
replace: 'zhangshoufu' 注释 Apache 配置文件/etc/apache2/sites-available/default.conf中NameVirtualHost [*]行之后的所有内容:
- 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 [*]'
regexp: '^(.+)$'
replace: '# \1' 注释 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' 注释 Apache 配置文件/etc/apache2/sites-available/default.conf中<VirtualHost [*]>行和</VirtualHost>行之间的内容:
# Prior to Ansible 2.7.10, using before and after in combination did the opposite of what was intended.
# see https://github.com/ansible/ansible/issues/31354 for details.
- name: Replace between the expressions (requires Ansible >= 2.4)
replace:
path: /etc/apache2/sites-available/default.conf
after: '<VirtualHost [*]>'
before: '</VirtualHost>'
regexp: '^(.+)$'
replace: '# \1' 删除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' 修改/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'

  

最新文章

  1. 0032 Java学习笔记-类加载机制-初步
  2. [原创]Centos7 内部常用软件升级计划
  3. mybatis中的#{}和${}
  4. 状态栏 a.getBoolean(1, false) 报错
  5. IHttpModule
  6. mysql show命令集合
  7. 李洪强漫谈iOS开发[C语言-021]-运算符
  8. careercup-数组和字符串1.3
  9. Adobe Photoshop CS或者CC卸载不了怎么办?
  10. HTTP协议之状态码详解
  11. 加速ssh连接
  12. PHP截取汉字乱码问题解决方法mb_substr函数的应用
  13. jquery结合highcharts插件显示实时数据动态曲线图
  14. JS - dateFormat
  15. vue学习心得
  16. MVC系列 引入MVC
  17. JVM-01:类的加载机制
  18. 测试工作常用SQL查询语句
  19. Codeforces 1089E - Easy Chess - [DFS+特判][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem E]
  20. C# Json数组序列化和反序列总结

热门文章

  1. [python][flask] Jinja 模板入门
  2. Zookeeper启动问题记录——ZooKeeper audit is enabled.
  3. Markdown练习
  4. JavaScript学习④
  5. Sql语言-操作表
  6. 2021.08.16 P1078 文化之旅(最短路)
  7. python基础练习题(九九乘法表)
  8. 『现学现忘』Git基础 — 13、Git的基础操作
  9. ValidForm5.3.2 忽略表单项校验详解
  10. UML 类之间的关系