代码之家  ›  专栏  ›  技术社区  ›  puravidaso

在ansible中,如何在文件末尾添加一个文本块,在标记前面加一个空行?

  •  0
  • puravidaso  · 技术社区  · 4 年前

    - hosts: localhost
      tasks:
        - name: update a file
          blockinfile:
            dest: /tmp/test
            block: |
              line 1
              line 2
    

    运行playbook时,文件 /tmp/test 变成:

    a # this is the end line of the original file
    # BEGIN ANSIBLE MANAGED BLOCK
    line 1
    line 2
    # END ANSIBLE MANAGED BLOCK
    

    我想在标记前加一个空行(换行符) # BEGIN ANSIBLE MANAGED BLOCK “对于视觉效果,最简单的方法是什么?最好在任务范围内,但任何想法都是受欢迎的。如果我重新定义标记,它将同时影响“BEGIN”和“END”标记。

    0 回复  |  直到 4 年前
        1
  •  3
  •   Vladimir Botka    4 年前

    使用 . 例如

    - hosts: localhost
      tasks:
        - name: update a file
          blockinfile:
            dest: /tmp/test
            block: |
              line 1
              line 2
        - name: insert empty line before the marker
          lineinfile:
            dest: /tmp/test
            insertbefore: '^# BEGIN ANSIBLE MANAGED BLOCK$'
            line: ''
    

    不幸的是 在前面插入 不能用更多的积木。 模板

    试试下面的剧本。不幸的是,EOF没有像预期的那样工作

    shell> cat manage-block.yml
    - name: "insert {{ my_marker }} in {{ my_dest }}"
      blockinfile:
        dest: "{{ my_dest }}"
        marker: "# {mark} ANSIBLE MANAGED BLOCK {{ my_marker }}"
        block: "{{ my_block }}"
    - name: "insert empty line before {{ my_marker }}"
      lineinfile:
        dest: "{{ my_dest }}"
        insertbefore: '^# BEGIN ANSIBLE MANAGED BLOCK {{ my_marker }}'
        line: 'empty line'
      when: ansible_loop.first
    - name: "insert empty line after EOF"
      lineinfile:
        dest: "{{ my_dest }}"
        insertafter: EOF
        line: 'empty line'
    
    - hosts: localhost
      vars:
        my_blocks:
          /tmp/test:
            - my_marker: block001
              my_block: |
                line 1
                line 2
            - my_marker: block002
              my_block: |
                line 3
                line 4
      tasks:
        - include_tasks: manage-block.yml
          with_subelements:
            - "{{ my_blocks|dict2items }}"
            - value
          loop_control:
              extended: true
          vars:
            my_dest: "{{ item.0.key }}"
            my_marker: "{{ item.1.my_marker }}"
            my_block: "{{ item.1.my_block }}"
    
        2
  •  1
  •   β.εηοιτ.βε grigorevp    4 年前

    如果您使用的是Ansible 2.5或更高版本,则可以更改 marker , marker_begin marker_end .

    下面是一个示例剧本:

    - hosts: all
      gather_facts: no
    
      tasks:
        - blockinfile:
            dest: /tmp/test
            marker: '{mark} ANSIBLE MANAGED BLOCK'
            marker_begin: '\n# BEGIN' 
            marker_end: '# END'
            block: |
              line 1
              line 2
    

    这将产生概述:

    PLAY [all] ********************************************************************************************************
    
    TASK [blockinfile] ************************************************************************************************
    changed: [localhost]
    
    PLAY RECAP ********************************************************************************************************
    localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    

    文件呢 /tmp/测试 最终是:

    a
     
    # BEGIN ANSIBLE MANAGED BLOCK
    line 1
    line 2
    # END ANSIBLE MANAGED BLOCK
    
        3
  •  0
  •   puravidaso    4 年前

    下面在模式前添加一个空行,当模式不存在时,它是幂等的,不会添加到文件的末尾。

    - hosts: localhost
      gather_facts: no
      tasks:
        - name: update a file
          replace:
            dest: /tmp/foobar
            regexp: '(?smx) (?<!\n\n) ^ (foobar)$'
            replace: "\n\\1"