12.ansible lineinfile模块 replace内容替换模块

一.lineinfile模块,功能有点类似sed

常用功能:对文件的行替换、插入、删除

PS:

替换/插入:如果有重复的,都是匹配最后一个 ,
如果不加backrefs项, 替换/插入如无匹配者,则将line所指定的行插入到文件的末尾
删除:如果有重复的,全部删除

常用参数:

path/dest:  目标文件绝对路径+文件名,必须参数
line:        替换/插入的内容
regexp:      待匹配内容
insertbefore:   匹配行前面插入 
insertafter:    匹配行面插入 
state:    删除匹配行,需要将值设为absent,默认值present。 
backup:   是否在修改文件之前对文件进行备份。 yes/no
create:   当要操作的文件并不存在时,是否创建对应的文件。yes/no
backrefs:yes/no
1.backrefs为no时,如果没有匹配,则添加一行line。如果匹配了,则把匹配内容替被换为line内容。
2.backrefs为yes时,如果没有匹配,则文件保持不变。如果匹配了,把匹配内容替被换为line内容。

举个栗子:

vim lineinfile.yaml
- hosts: node
  gather_facts: no
  become: yes
  become_method: sudo
  
  tasks

1、备份文件

- name: 'backup file'
  lineinfile:
    path: /root/config
    backup: yes
    line: state=present

2、替换整行

- name: '替换'
  lineinfile:
    path: /root/config
    regexp: '^SELINUX='
    line: '此行已被我替换了'

3、在匹配行前面插入

- name: '在匹配行前面插入'
  lineinfile:
    path: /root/config
    insertbefore: 'SELINUXTYPE=targeted'
    line: '成功在匹配行前面插入一行'

4、在匹配行后面插入

- name: "在匹配行后面插入"
  lineinfile:
    dest: /root/config
    insertafter: 'minimum - Modification of targeted policy.'
    line: 成功在匹配行后面插入一行'

5、删除匹配行

- name: '删除匹配行'
  lineinfile:
    dest: /root/config
    regexp: 'J\+sZc\_j\!s\@d\#x'
    state: absent

6、添加

- name: 'add a line'
  lineinfile:
    dest: /root/config
    regexp: 'add a line!'
    line: '我出现代表没有匹配项'

7、没有匹配,保持文件不变

- name: 'backrefs'
  lineinfile:
    dest: /root/config
    backrefs: yes
    regexp: 'add a line~~~'
    line: '没有匹配项我也不会出现在文件中'

8、新建

- name: 'create'
  lineinfile:
    dest: /root/test.txt
    create: yes
    line: state=present

9.insertafter insertbeforce也支持正则

- name: Modify Zabbix Super User
  lineinfile:
    path: /etc/sudoers
    insertafter: '^root'
    line: 'zabbix ALL = NOPASSWD: ALL'

二.replace模块用于在文件中根据指定的正则表达式替换匹配的内容

replace模块常用参数

• path:必须参数,指定要修改的文件,2.3版本之前,这个参数叫dest、destfile、name;现在这三个名称是path参数的别名
• regexp:必须参数,指定一个正则表达式,可以是python正则
• replace:替换regexp参数匹配到的字符串,
• owner:结果文件或目录的所属用户名,相当于chown命令修改
• group:结果文件或目录的所属组名,相当于chown命令修改
• mode:结果文件或目录的权限,与chmod命令不一致的是,replace模块的mode参数需要添加前导零,以便ansible的YAML解析器知道它是八进制;1.8版本后可以设置为符号模式(u+rwx或u=rw),2.6版本后可以是特殊字符串(preserve),当设置为’preserve’时,文件将被赋予与源文件相同的权限。
• others:可以指定file模块的所有参数
• encoding:用于读取和写入文件的字符编码
• before:如果指定,则仅替换/删除此匹配之前的内容,可以和after参数结合使用
• after:如果指定,则仅替换/删除此匹配之后的内容,可以和before参数结合使用
• attributes:结果文件或目录的特殊属性,相当chattr,默认使用=运算符,指定文件或目录的某项属性
• backup:修改源文件前创建一个包含时间戳信息的备份文件

replace模块doc文档示例

- name: Before Ansible 2.3, option 'dest', 'destfile' or 'name' was used instead of 'path'
  replace:
    path: /etc/hosts
    regexp: '(\s+)old\.host\.name(\s+.*)?$'
    replace: '\1new.host.name\2'

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

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

# 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/hosts
    after: '<VirtualHost [*]>'
    before: '</VirtualHost>'
    regexp: '^(.+)$'
    replace: '# \1'

- name: Supports common file attributes
  replace:
    path: /home/jdoe/.ssh/known_hosts
    regexp: '^old\.host\.name[^\n]*\n'
    owner: jdoe
    group: jdoe
    mode: '0644'

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

- name: Short form task (in ansible 2+) necessitates backslash-escaped sequences
  replace: path=/etc/hosts regexp='\\b(localhost)(\\d*)\\b' replace='\\1\\2.localdomain\\2 \\1\\2'

- name: Long form task does not
  replace:
    path: /etc/hosts
    regexp: '\b(localhost)(\d*)\b'
    replace: '\1\2.localdomain\2 \1\2'

- name: Explicitly specifying positional matched groups in replacement
  replace:
    path: /etc/ssh/sshd_config
    regexp: '^(ListenAddress[ ]+)[^\n]+$'
    replace: '\g<1>0.0.0.0'

- name: Explicitly specifying named matched groups
  replace:
    path: /etc/ssh/sshd_config
    regexp: '^(?P<dctv>ListenAddress[ ]+)(?P<host>[^\n]+)$'
    replace: '#\g<dctv>\g<host>\n\g<dctv>0.0.0.0'

replace模块示例

复制 
- name: replace hostname
  replace: 
    path: /root/hwreport.txt
    regexp: "inventoryhostname"
    replace: "{{ ansible_hostnam }}"
- name: replace disk vdb
  replace: 
    path: /root/hwreport.txt
    regexp: "disk_vdb_size"
    replace: "{{ ansible_device.vdb.size }}"
  when: "'vdb' in ansible_devices"
- name: replace disk vdb
  replace: 
    path: /root/hwreport.txt
	regexp: "disk_vdb_size"
    replace: "NONE"
  when: "'vdb' not in ansible_devices"
posted @ 2022-09-19 11:20  老夫聊发少年狂88  阅读(1108)  评论(0编辑  收藏  举报