前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ansible 对文件内容的操作

ansible 对文件内容的操作

原创
作者头像
张琳兮
修改2021-05-13 18:09:47
1.6K0
修改2021-05-13 18:09:47
举报
文章被收录于专栏:首富手记首富手记

ansible 对文件内容的操作

代码语言:txt
复制
作者: 张首富
时间: 2021-05-13
wx: y18163201

ansible lineinfile

简介

lineinfile该模块是操作文件中的每一行内容,他是按照行为单位的,和下面的replace模块并不冲突。

修改匹配行,如果不存在就会添加

代码语言:txt
复制
tasks:
- name: Ensure SELinux is set to enforcing mode
  lineinfile:
    path: /etc/selinux/config
    regexp: '^SELINUX='
    line: SELINUX=enforcing

SELINUX=这个开头的行直接替换成SELINUX=enforcing不管后面是什么,直接替换整行内容。

删除文件中的行

代码语言:txt
复制
- name:  确保sudoers配置中没有wheel组。
  lineinfile:
    path: /etc/sudoers
    state: absent
    regexp: '^%wheel'

在匹配行前添加一行内容,并确保插入成功

代码语言:txt
复制
- name: Ensure the default Apache port is 8080
  lineinfile:
    path: /etc/httpd/conf/httpd.conf
    regexp: '^Listen '						//确保添加完成
    insertafter: '^#Listen '			//要在哪一行前面添加
    line: Listen 8080							//添加的内容

在匹配行后添加一行内容,并确保插入成功

代码语言:txt
复制
- 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'	//添加的内容是什么

修改文件并更改权限

代码语言:txt
复制
- 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'

文件存在就添加一行内容,如果内容存在就不会添加(多次执行不会重复添加)

代码语言:txt
复制
- name: add a line
  lineinfile:
    dest: /etc/hosts
    line: '10.1.1.1 zhangshoufu.com'

ansible replace(非核心模块)

介绍

replace模块可以根据我们指定的正则表达式替换匹配到的字符串,文件中所有被匹配到的字符串都会被替换,和lineinfile不同的地方是replace只会替换正则表达式匹配到的内容,而lineinfile是替换正则表达式匹配到行的内容。

常用参数

  • path: 文件路径,我们要替换那个文件内的内容,必须
  • regexp:正则表达式,必要参数
  • replace: 替换成的内容

替换文件内容

代码语言:txt
复制
tasks:
- name: '替换zsf 字符串为zhangshoufu'
  replace:
    path: /tmp/test.txt
    regexp: 'zsf'
    replace: 'zhangshoufu'

注释 Apache 配置文件/etc/apache2/sites-available/default.confNameVirtualHost [*]行之后的所有内容:

代码语言:txt
复制
- 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行之前的所有内容:

代码语言:txt
复制
- 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>行之间的内容:

代码语言:txt
复制
# 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及之后的空行,同时修改文件属性和权限:

代码语言:txt
复制
- 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文件并校验:

代码语言:txt
复制
- 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'

参考:https://www.ityoudao.cn/posts/ansible-modules-replace/

ansible blockinfile

简介

lineinfile有点类似,他可以帮助我们在文件中插入一段文本。

常用参数

  • path: 要操作的文件名称
  • state:present 确保段落存在,absent 确保段落不存在,默认值为 present,会将指定的一段文本插入到文件中,乳沟文件中已经存在标记的文本,会重新更改;absent 删除对应的段落
  • marker:才操作的段落中添加标记信息。默认值为"# {mark} ANSIBLE MANAGED BLOCK"

我们想要在指定文件中插入一段文本,Ansible 会自动为这段文本添加两个标记,一个开始标记,一个结束标记,默认情况下,开始标记为# BEGIN ANSIBLE MANAGED BLOCK,结束标记为# END ANSIBLE MANAGED BLOCK

{mark}变量会自动被替换成开始标记中的marker_begin和结束标记中的marker_end,如果使用没有{mark}变量的自定义标记,可能会导致重复插入。

  • marker_begin: 设置 marker 参数的开始标记中的 {mark}变量,默认值为“BEGIN”
  • marker_end: 设置 marker 参数的结束标记中的 {mark}变量,默认值为“END”
  • block: 指定一段要操作的文本,如果没有 block 参数或者参数的值为空,则移除文本块,等同于 state=absent.
  • insertafter: 在匹配后添加。

值为EOF或者正则表达式,默认值为EOF,表示End Of File,插入到文件的末尾。

如果设置为正则表达式,默认将文本插入到正则表达式匹配的最后一行之后。

如果设置为正则表达式,但是没有匹配到任何行,则插入到文件末尾。

  • insertbefore: 在匹配前添加

插入段落(state=present)时使用。

值为BOF或者正则表达式,默认值为BOF,表示Begin Of File,插入到文件的开头。

如果设置为正则表达式,默认将文本插入到正则表达式匹配的最后一行之前。

如果设置为正则表达式,但是没有匹配到任何行,则插入到文件开头。

  • backup: 操作前是否备份

在文件最后添加多行

代码语言:txt
复制
tasks:
- name: 'add hosts'
  blockinfile:
    path: /etc/hosts
    block: |
      10.1.1.1 www.zhangshoufu.com
      10.2.2.2 test.zhangshoufu.com

备份/etc/ssh/ssh_config文件,并在文件末尾插入./local/ssh_config文件的内容,最后使用/usr/sbin/sshd -T -f /etc/ssh/ssh_config命令校验:

代码语言:txt
复制
tasks:
- name: Insert/Update configuration using a local file and validate it
  blockinfile:
    block: "{{ lookup('file', './local/ssh_config') }}"
    dest: /etc/ssh/ssh_config
    backup: yes
    validate: /usr/sbin/sshd -T -f %s

在 HTML 文件/var/www/html/index.html"<body>"之后插入自定义标记的内容:

代码语言:txt
复制
tasks:
- name: Insert/Update HTML surrounded by custom markers after <body> line
  blockinfile:
    path: /var/www/html/index.html
    marker: "<!-- {mark} ANSIBLE MANAGED BLOCK -->"
    insertafter: "<body>"
    block: |
      <h1>Welcome to {{ ansible_hostname }}</h1>
      <p>Last updated on {{ ansible_date_time.iso8601 }}</p>

移除在 HTML 文件/var/www/html/index.html中的添加的内容:

代码语言:txt
复制
tasks:
- name: Remove HTML as well as surrounding markers
  blockinfile:
    path: /var/www/html/index.html
    marker: "<!-- {mark} ANSIBLE MANAGED BLOCK -->"
    block: ""

/etc/hosts文件中添加解析记录:

代码语言:txt
复制
tasks:
- name: Add mappings to /etc/hosts
  blockinfile:
    path: /etc/hosts
    block: |
      {{ item.ip }} {{ item.name }}
    marker: "# {mark} ANSIBLE MANAGED BLOCK {{ item.name }}"
  with_items:
    - { name: host1, ip: 10.10.1.10 }
    - { name: host2, ip: 10.10.1.11 }
    - { name: host3, ip: 10.10.1.12 }

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ansible 对文件内容的操作
    • ansible lineinfile
      • 简介
      • 修改匹配行,如果不存在就会添加
      • 删除文件中的行
      • 在匹配行前添加一行内容,并确保插入成功
      • 在匹配行后添加一行内容,并确保插入成功
      • 修改文件并更改权限
      • 文件存在就添加一行内容,如果内容存在就不会添加(多次执行不会重复添加)
    • ansible replace(非核心模块)
      • 介绍
      • 常用参数
      • 替换文件内容
      • 注释 Apache 配置文件/etc/apache2/sites-available/default.conf中NameVirtualHost [*]行之后的所有内容:
      • 注释 Apache 配置文件/etc/apache2/sites-available/default.conf中# live site config行之前的所有内容:
      • 注释 Apache 配置文件/etc/apache2/sites-available/default.conf中<VirtualHost [*]>行和</VirtualHost>行之间的内容:
      • 删除jdoe用户 SSH 的known_hosts文件中的old.host.name及之后的空行,同时修改文件属性和权限:
      • 修改/etc/apache/ports文件并校验:
    • ansible blockinfile
      • 简介
      • 常用参数
      • 在文件最后添加多行
      • 备份/etc/ssh/ssh_config文件,并在文件末尾插入./local/ssh_config文件的内容,最后使用/usr/sbin/sshd -T -f /etc/ssh/ssh_config命令校验:
      • 在 HTML 文件/var/www/html/index.html的"<body>"之后插入自定义标记的内容:
      • 移除在 HTML 文件/var/www/html/index.html中的添加的内容:
      • 在/etc/hosts文件中添加解析记录:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档