一、ansible替换遇到的问题

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

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

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

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

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

二、lineinfile模块

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

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

三、replace模块

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

1- replace: dest=/etc/hosts regexp='(\s+)old\.host\.name(\s+.*)?$' replace='\1new.host.name\2' backup=yes
2- replace: dest=/home/jdoe/.ssh/known_hosts regexp='^old\.host\.name[^\n]*\n' owner=jdoe group=jdoe mode=644
3- 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最后的修改结果如下:

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