【问题标题】:Replace block in file finds all occurrences and replaces the latest one, but the very first one is needed替换文件中的块查找所有匹配项并替换最新的一个,但需要第一个
【发布时间】:2022-11-26 23:12:02
【问题描述】:

我有一个配置文件,我想替换其中的某个块,但是 marker_end: "" 参数的逻辑不允许我做我想做的事。

配置文件:

# Full configuration options can be found at https://www.vaultproject.io/docs/configuration

ui = true

#mlock = true
#disable_mlock = true

storage "file" {
  path = "/opt/vault/data"
}

#storage "consul" {
#  address = "127.0.0.1:8500"
#  path    = "vault"
#}

# HTTP listener
#listener "tcp" {
#  address = "127.0.0.1:8200"
#  tls_disable = 1
#}

# HTTPS listener
listener "tcp" {
  address       = "0.0.0.0:8200"
  tls_cert_file = "/opt/vault/tls/tls.crt"
  tls_key_file  = "/opt/vault/tls/tls.key"
}

# Enterprise license_path
# This will be required for enterprise as of v1.8
#license_path = "/etc/vault.d/vault.hclic"

# Example AWS KMS auto unseal
#seal "awskms" {
#  region = "us-east-1"
#  kms_key_id = "REPLACE-ME"
#}

# Example HSM auto unseal
#seal "pkcs11" {
#  lib            = "/usr/vault/lib/libCryptoki2_64.so"
#  slot           = "0"
#  pin            = "AAAA-BBBB-CCCC-DDDD"
#  key_label      = "vault-hsm-key"
#  hmac_key_label = "vault-hsm-hmac-key"
#}

这是需要更换的块:

# HTTP listener
#listener "tcp" {
# address = "127.0.0.1:8200"
# tls_disable = 1
#}

内容如下:

# HTTP listener
listener "tcp" { 
  address = "0.0.0.0:8201"
  tls_disable = 1
}

但是下面的代码:

    - name: Disable tls for web interface
      ansible.builtin.blockinfile:
        path: /etc/vault.d/vault.hcl
        backup: true
        marker: "{mark}"
        marker_begin: "# HTTP listener"
        marker_end: ""
        block: |
          listener "tcp" {
            address = "0.0.0.0:8201"
            tls_disable = 1
          }

没有像我预期的那样工作:marker_end: "" 找到所有出现的地方并使用最近的一个,所以输出文件是:

# Full configuration options can be found at https://www.vaultproject.io/docs/configuration

ui = true

#mlock = true
#disable_mlock = true

storage "file" {
  path = "/opt/vault/data"
}

#storage "consul" {
#  address = "127.0.0.1:8500"
#  path    = "vault"
#}

# HTTP listener
listener "tcp" {
  address = "0.0.0.0:8201"
  tls_disable = 1
}

# Example HSM auto unseal
#seal "pkcs11" {
#  lib            = "/usr/vault/lib/libCryptoki2_64.so"
#  slot           = "0"
#  pin            = "AAAA-BBBB-CCCC-DDDD"
#  key_label      = "vault-hsm-key"
#  hmac_key_label = "vault-hsm-hmac-key"
#}

【问题讨论】:

    标签: replace ansible


    【解决方案1】:

    您误解了blockinfile 的工作原理。使用空标记无法获得任何可靠的结果。如果你想用那个模块管理你的块,你需要在它周围有特定的开始和结束标记。这通常是通过第一次从头开始插入块来实现的。然后,您可以随着时间的推移使用不同的内容重新运行该模块。

    这是一个示例剧本,如果使用 replace 模块不存在,它将在您的块上插入开始/结束标记,以便在下一个 blockinfile 任务中对其进行管理。

    请注意,如果您没有明确需要保持该块可管理,您可以直接使用 replace 获得相同的结果。

    ---
    - hosts: localhost
      gather_facts: false
    
      vars:
        # This was my test file, just replace with yours
        file: /tmp/so_find/file/vault.hcl
    
      tasks:
        - name: prepare the file with block markers for later management with blockinfile if not already done
          replace:
            path: "{{ file }}"
            regexp: '^(# HTTP listener)
    (.*{[^}]*})$'
            replace: |-
              g<1> - ANSIBLE MANAGED BLOCK - BEGIN
              g<2>
              g<1> - ANSIBLE MANAGED BLOCK - END
    
        - name: manage block with our inserted markers
          blockinfile:
            path: "{{ file }}"
            marker: "# HTTP listener - ANSIBLE MANAGED BLOCK - {mark}"
            backup: true
            block: |-
              listener "tcp" {
                address = "0.0.0.0:8201"
                tls_disable = 1
              }
    

    【讨论】:

      猜你喜欢
      • 2014-06-18
      • 2022-01-08
      • 2011-03-28
      • 2017-12-19
      • 2021-04-16
      • 2012-09-19
      • 2011-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多