如何使用ansible regexp将新字符串插入telegraf.conf的inputs.ping [英] How to Insert a new string into telegraf.conf's inputs.ping using ansible regexp

查看:383
本文介绍了如何使用ansible regexp将新字符串插入telegraf.conf的inputs.ping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ansible更新telegraf.conf的[[inputs.ping]].

I'm trying to use ansible to update telegraf.conf's [[inputs.ping]].

telegraf.conf如下所示:

telegraf.conf looks like the following:

[[inputs.ping]]
  urls = ["tac-temp1","tac-temp2", "tac-temp3","tac-temp4"] #tac
  count = 30
  timeout = 15.0
  [inputs.ping.tags]
  name = "tac"

[[inputs.ping]]
  urls = ["prod-temp1","prod-temp2", "prod-temp3","prod-temp4"] #prod
  count = 30
  timeout = 15.0
  [inputs.ping.tags]
  name = "prod"

[[inputs.ping]]
  urls = ["test-temp1","test-temp2", "test-temp3","test-temp4"] #test
  count = 30
  timeout = 15.0
  [inputs.ping.tags]
  name = "test"

我正在尝试在上面显示的第2行中的,"tac-temp4"之后添加,"tac-temp10".

I'm trying to add ,"tac-temp10" after ,"tac-temp4" in line 2 shown above.

- hosts: Servers
  become: yes
  become_method: sudo
  tasks:
    - name: Loading telegraf.conf content for search
      shell: cat /tmp/telegraf.conf
      register: tele_lookup

    - name: Adding Server to  /tmp/telegraf.conf if does not exists
      lineinfile:
             path: /tmp/telegraf.conf
             state: present
             regexp: '^((.*)"] #tac$)'       
             line: ',"tac-temp10"'      
             backup: yes
      when: tele_lookup.stdout.find('tac-temp10') != '0'

regexp: '^((.*)"] #tac$)'将整个行替换为,"tac-temp10".预期输出:

regexp: '^((.*)"] #tac$)' is replacing the whole line with ,"tac-temp10". Expected output:

[[inputs.ping]]
  urls = ["tac-temp1","tac-temp2", "tac-temp3","tac-temp4","tac-temp10"] #tac
  count = 30
  timeout = 15.0
  [inputs.ping.tags]
  name = "tac"

推荐答案

警告:丑陋的正则表达式在前面.谨防下一个家伙(包括经过一段时间的您之后)会对您进行不可预测的了解,以进行维护.

Warning: Ugly regexp ahead. Beware of unpredictable understanding for next guys (including you after time passed by...) doing maintenance.

如果没有单个幂等任务,则以下内容会将您的服务器添加到列表的末尾.

The following will add your server at the end of the list if it is not already present (anywhere in the list) with a single idempotent task.

    - name: add our server if needed
      lineinfile:
        path: /tmp/test.conf
        backup: yes
        state: present
        regexp: '^( *urls *= *\[)(("(?!tac-temp10)([a-zA-Z0-9_-]*)",? *)*)(\] #tac)$'
        backrefs: yes
        line: '\1\2, "tac-temp10"\5'

您需要使用向后引用将表达式中已经匹配的部分放回一行.我使用了backup: yes,所以我可以轻松地返回到原始版本进行测试.随意放下它.

You need to use backreferences to put back on the line the already matched parts of the expression. I used backup: yes so I could easily come back to the original for my tests. Feel free to drop it.

正如您所看到的(并在我的警告中建议),对于必须快速阅读代码的任何人来说,这几乎是不可能理解的.如果您需要做更多更复杂的事情,请考虑使用模板并将服务器列表存储在某个位置的变量中.

As you can see (and as advised in my warning) this is pretty much impossible to understand for anyone having to quickly read the code. If you have to do anything more fancy/complicated, consider using a template and storing your server list in a variable somewhere.

这篇关于如何使用ansible regexp将新字符串插入telegraf.conf的inputs.ping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆