【问题标题】:Add a string inside a configuration file through ansible通过ansible在配置文件中添加字符串
【发布时间】:2020-05-08 16:39:53
【问题描述】:

我需要在其中一个配置文件中添加一个新字符串。我必须在“images =”中添加“mycustomimage”和“,”。所以简而言之,我需要的输出是 images= previousimage,mycustomimage

mycnf.conf 视图

id=1
images=previousimage

为此,我尝试了这段代码

---
- hosts: test_server
  - name: Add new string after "," in images
    lineinfile:
        path: /home/mycnf.conf
        regexp: 'images='
        insertafter: '^,'
        line: mycustomimage

预期输出

id= 1
images=previousimage,mycustomimage

但它对我不起作用。有什么想法吗? 提前致谢! 席德

【问题讨论】:

  • 你能提供预期结果的样本吗?
  • 问题中已经提到过。所需的输出是 images= previousimage,mycustomimage
  • 你的线路总是images=previousimage 吗?
  • 您的第一行是ìmages=previousimage,(以逗号结尾)? ii 会有所不同吗?如果它不同,它是否可以包含多个图像?而在这种情况下,会不会总是以昏迷告终?它是否已经在随机位置包含自定义图像?请编辑您的问题并给出初始配置行的精确示例,详尽描述可能的变化(如果有)和预期结果。
  • 对什么?我的评论中有 5 个问题。

标签: ansible yaml ansible-template


【解决方案1】:

这会有所帮助

- name: replace line
  lineinfile:
    path: myfile.txt
    regexp: "^image="
    line: 'image=previousimage,customimage'

【讨论】:

    【解决方案2】:

    从你的例子

    1. 您知道文件中的最后一行是images=previousimage,mycustomimage
    2. 如果现有的image=.* 行存在且与最后一行不匹配,您希望添加此行来代替它。

    以下将完成这项工作

        - name: Replace line if needed
          lineinfile:
            path: /home/mycnf.conf
            regex: images=.*
            line: images=previousimage,mycustomimage
    

    注意:如果由于任何原因在您的文件中没有与正则表达式匹配的行,则该行将添加到文件末尾。

    【讨论】:

      【解决方案3】:

      假设previousimage 未知,您可以做两件事:

      1.使用grep获取此行,注册到变量并添加行

      - name: get line
        shell: grep "^image=" /config/file.something
        register: current_image
      
      - name: update image
        lineinfile:
          path: /config/file.something
          regexp: '^image='
          line: "{{ current_image.stdout }},{{ new_image | default('customimage') }}"
      

      2.为此配置文件创建一个模板,并在每次 playbook 运行并检测到更改时渲染它:

      - set_fact:
          images: <list of images retrieved by lookup or static>
      
      - name: update config.something
        template:
           src: my_template.j2
           dest: /config/file.something
      

      模板将如下所示:

      id={{ id }}
      images={{ images | join(",") }}
      

      【讨论】:

        【解决方案4】:

        我们假设你不知道previousimage 是什么,你可能有几个以前的图像,并且你想追加mycustomimage,并且你希望剧本是幂等的:

        ---
        - hosts: all
          connection: ssh
          become: no
          gather_facts: no
        
        
          vars:
            image_name: mycustomimage
        
          tasks:
          - lineinfile:
              path: testfile.txt
              regexp: '^images=(.*(?<!{{ image_name }}))'
              line: '\1,{{ image_name }}'
              backrefs: yes
        

        那么让我们来解释一下正则表达式:^images=你可以自己弄清楚!

        第一个括号开始 backref 块,它将把所有内容都吸到行尾:.*

        然后,它回顾一下它刚刚吸入的内容,并确保{{ image_name }} 不在最后:(?&lt;!{{ image_name }})

        最后,我们用 ) 关闭 backref 块。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-02-17
          • 2020-06-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多