亳州网站建设推广,京东的网站建设历史,东莞网站新站排名,团购网站建设怎么样#发文福利# 
一、前言 
1、目标#xff1a;批量修改服务器上的xml配置文件 
2、原配置文件的最后一部分内容如下 
[roots40 tmp]# tail kahn.confvaluenginx/valuedescriptionmy nginx config!/description
/propertyproperty批量修改服务器上的xml配置文件 
2、原配置文件的最后一部分内容如下 
[roots40 tmp]# tail kahn.confvaluenginx/valuedescriptionmy nginx config!/description
/propertypropertynameaddress/namevalueshanghai.china/value
/property/configuration 
3、要求在倒数第二行的位置插入新的配置信息 property1name1kahn.config/namevalue1hahahaha.hello world!/value/property1 
二、ansible-playbook的写法 
---- hosts: allgather_facts: noignore_unreachable: yestasks:- name: debug 1debug:msg: hello , newgroup- name: 往xml里插入一段配置信息blockinfile:path: /opt/tmp/hdfs-site.xmlinsertbefore: /configurationblock: |property1name1kahn.config/namevalue1hahahaha.hello world!/value/property1marker: #{mark} my marker kahn...state: presentnotify:- handler1handlers:- name: handler1command: /bin/bash -c /usr/bin/sed -ie s/\/configuration/\n\/configuration/g /opt/tmp/kahn.conf 
说明 
1、insertbefore: /configuration 指定在 /configuration一行的前面插入新配置信息 
2、marker: #{mark} my marker kahn...  指定自定义标记注意#是linux下的注释{mark}是固定写法必须写。到时候再你的配置信息上下放会有标记开始和结束的信息如#BEGIN my marker kahn...和#END my marker kahn...。为啥要加{mark}有这个标记才能以后使用state: absent去删除块。 
3、state: present 增加块。反之是state: absent如果要删除块state: absent的前提是在增加的时候必须有marker: #{mark}是必不可少的。 
4、notify:  - handler1 是本次的拓展自定义动作标签当我们修改了配置文件后就会触发将最后一行的/configuration替换掉为/configuration上面增加一个空行。注意这是个迂回的方法没能在ansible中找到给指定行增加空白行的方法。如果我们反反复复的这样弄的话会在最后一行/configuration前产生多条空行 
执行结果如下 
[roots40 tmp]# tail kahn.confvaluenginx/valuedescriptionmy nginx config!/description
/propertypropertynameaddress/namevalueshanghai.china/value
/propertyproperty1name1kahn.config/namevalue1hahahaha.hello world!/value
/property1/configuration