首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在带触角的烟囱中添加单词或nimber

如何在带触角的烟囱中添加单词或nimber
EN

Stack Overflow用户
提问于 2021-10-27 11:59:28
回答 1查看 55关注 0票数 1

我有一个代码

代码语言:javascript
复制
  - name: Ansible replace string example
    replace:
      path: /etc/jitsi/videobridge/sip-communicator.properties contains
      regexp: 'shard'
      replace: "shard-1"

但它不起作用

我中风了:

代码语言:javascript
复制
org.jitsi.videobridge.xmpp.user.shard.HOSTNAME=localhost
org.jitsi.videobridge.xmpp.user.shard.DOMAIN=auth.jc.name.com
org.jitsi.videobridge.xmpp.user.shard.USERNAME=name
org.jitsi.videobridge.xmpp.user.shard.PASSWORD=Hfr*7462
org.jitsi.videobridge.xmpp.user.shard.MUC_JIDS=JvbBredjoy@internal.auth.jc.name.com
org.jitsi.videobridge.xmpp.user.shard.MUC_NICKNAME=7896aee5-fgre-4b02-4569-0bcc75ed1d0d

文件中的/etc/jitsi/videobridge/sip-communicator.properties

我应该在单词shard符号"-“之后加上数字(1,2,3)等,例如org.jitsi.videobridge.xmpp.user.shard-1.HOSTNAME=localhost

在此之前,我应该检查-如果org.jitsi.videobridge.xmpp.user.shard-1.HOSTNAME=localhost行包含单词shard-1,那么我们将重命名为shard-2等等。

EN

回答 1

Stack Overflow用户

发布于 2021-10-27 15:04:11

要回答您的问题,我需要使用一个自定义过滤器插件来创建新值,以替换旧值:

在剧本文件夹中创建一个文件夹filter_plugins (我已将文件命名为myfilters.py和filter buildvalue)

过滤器:为了避免内存泄漏,使用“with”打开文件(感谢@mdaniel)

代码语言:javascript
复制
#!/usr/bin/python
import re
class FilterModule(object):
    def filters(self):
        return {
            'buildvalue': self.buildvalue
        }

    def buildvalue(self, nfile, srch):
        found = False
        with open(nfile, 'r') as f: 
            lines = f.readlines()
            for line in lines: 
                x = re.search(srch + '-*[0-9]*', line)
                if x != None:
                    found = True
                    break

        if found:        
            rep = srch + ('-1' if '-' not in x.group() else '-' + str((int(x.group().split('-')[1]) + 1))) 
        else:
            rep = srch

        return rep

剧本:

代码语言:javascript
复制
  tasks:
    - name: replace
      ansible.builtin.replace:
        path: "{{ filename }}"
        regexp: "{{ search }}-*[0-9]*"
        replace: "{{ newvalue }}"
      vars:
        filename: 'files/fileregex.txt'
        search: 'shard'
        newvalue: "{{ filename | buildvalue(search)  }}"  
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69738282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档