ansible对文件内容操作
阅读原文时间:2023年07月09日阅读:1

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中行和行之间的内容:

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: '' before: ''
    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'