阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Ansible中替换模块介绍

2次阅读
没有评论

共计 2770 个字符,预计需要花费 7 分钟才能阅读完成。

一、ansible 替换遇到的问题

ansible 在使用 sed 进行替换时,经常会遇到需要转义的问题,而且 ansible 在遇到特殊符号进行替换时,存在问题,无法正常进行替换。其实在 ansible 自身提供了两个模块---lineinfile 模块和 replace 模块,可以方便的进行替换。这里以使用 ansilble 安装 salt-minion 为例,用如下 playbook 语句执行时,是执行不成功的:

shell: sed -i '/^#id:/a\id: {{ansible_ens192.ipv4.address}}' /etc/salt/minion

由于上面的语名里特殊符号比较多,多次转义测试不成功。同样直接调用 shell 模块时也会遇到这样问题,比如这里有一个将 /etc/hosts 最后一行注释的语句,使用 $s 变量时会不成功,需要通过 \ 进行转义。

ansible all -m shell -a "sed -e'$s/^/#/g'/etc/hosts" // 不成功
ansible all -m shell -a "sed -i'\$s/#//g'/etc/hosts" // 成功 

接下来我们看下 ansilbe 自的替换模块。

二、lineinfile 模块

ansible-doc 帮助信息给我们的示例如下:

- lineinfile: dest=/etc/selinux/config regexp=^SELINUX= line=SELINUX=enforcing 行替换
- lineinfile: dest=/etc/sudoers state=absent regexp="^%wheel" 行删除
- lineinfile: dest=/etc/hosts regexp='^127\.0\.0\.1' line='127.0.0.1 localhost' owner=root group=root mode=0644
- lineinfile: dest=/etc/httpd/conf/httpd.conf regexp="^Listen" insertafter="^#Listen" line="Listen 8080" 将以?#Listen? 开头行的下面的? 以 Listen 开头的行换成
- lineinfile: dest=/etc/services regexp="^# port for http" insertbefore="^www.*80/tcp" line="# port for http by default"
# Add a line to a file if it does not exist, without passing regexp
- lineinfile: dest=/tmp/testfile line="192.168.1.99 foo.lab.net foo"
# Fully quoted because of the ':' on the line. See the Gotchas in the YAML docs.
- lineinfile: "dest=/etc/sudoers state=present regexp='^%wheel'line='%wheel ALL=(ALL) NOPASSWD: ALL'"- lineinfile: dest=/opt/jboss-as/bin/standalone.conf regexp='^(.*)Xms(\d+)m(.*)$'line='\1Xms${xms}m\3' backrefs=yes
# Validate the sudoers file before saving
- lineinfile: dest=/etc/sudoers state=present regexp='^%ADMIN ALL\=' line='%ADMIN ALL=(ALL) NOPASSWD:ALL' validate='visudo -cf %s'
三、replace 模块

该模块有点类似于 sed 命令,主要也是基于正则进行匹配和替换,如下:

- replace: dest=/etc/hosts regexp='(\s+)old\.host\.name(\s+.*)?$' replace='\1new.host.name\2' backup=yes
- replace: dest=/home/jdoe/.ssh/known_hosts regexp='^old\.host\.name[^\n]*\n' owner=jdoe group=jdoe mode=644
- replace: dest=/etc/apache/ports regexp='^(NameVirtualHost|Listen)\s+80\s*$' replace='\1 127.0.0.1:8080' validate='/usr/sbin/apache2ctl -f %s -t'
四、原来的问题

回到原来的问题,使用 ansible 安装 salt-minion 涉及到的替换问题,这里用的 playbook 最后的修改结果如下:

- name: Fetch configuration from all testservers,code from www.361way.com
hosts: all
vars:
ID: "id:"
masterip: "master: 10.211.95.232"
tasks:
- name: copy migu repo to hosts
copy: src=./file/migu_rhel7.repo dest=/etc/yum.repos.d/migumirror.repo
- name: install salt repo
yum: name=https://repo.saltstack.com/yum/redhat/salt-repo-latest-2.el7.noarch.rpm state=present
- name: install salt minion
yum: name=salt-minion state=latest
- name: set the salt master ip
shell: echo {{masterip}} >>/etc/salt/minion
- name: Get config
#shell: sed -i '/^#id:/a\id: {{ansible_ens192.ipv4.address}}' /etc/salt/minionlineinfile: dest=/etc/salt/minion regexp="^#id" line="{{ID}} {{inventory_hostname}}"
- name: enable and start minion
shell: "systemctl enable salt-minion.service && systemctl start salt-minion.service"

阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配

腾讯云新客低至 82 元 / 年,老客户 99 元 / 年

代金券:在阿里云专用满减优惠券

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-09-20发表,共计2770字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中