ansible在使用sed进行替换时,经常会遇到需要转义的问题,而且ansible在遇到特殊符号进行替换时,存在问题,无法正常进行替换 。其实在ansible自身提供了两个模块---lineinfile模块和replace模块,可以方便的进行替换 。这里以使用ansilble安装salt-minion为例,用如下playbook语句执行时,是执行不成功的:
: 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自的替换模块。
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'
该模块有点类似于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=//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/minion lineinfile: 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"
原文来自:
本文地址://gulass.cn/ansible-sed-replace.html编辑:张雄,审核员:逄增宝
本文原创地址://gulass.cn/ansible-sed-replace.html编辑:问题终结者,审核员:暂无