【问题标题】:ansible debug msg failed_when and changed_whenansible debug msg failed_when 和 changed_when
【发布时间】:2021-05-05 11:01:45
【问题描述】:

y 代码 -

# Update apt cache reposoitary
        - name: Update apt cache
          command: apt-get update
          register: return

# Debug msg
        - debug:
            msg: '{{ return }}'

result.stdout_lines

    "stdout_lines": [
        "Hit:1 http://server/pub/mirrors/ubuntu bionic InRelease",
        "Get:2 http://server/pub/mirrors/ubuntu bionic-updates InRelease [88.7 kB]",
        "Get:3 http://server/pub/mirrors/ubuntu bionic-backports InRelease [74.6 kB]",
        "Hit:4 https://download.package.com/infrastructure_agent/linux/apt bionic InRelease",
        "Hit:5 https://archive.repo.package.com/apt/ubuntu/18.04/amd64/2018.3 bionic InRelease",
        "Get:6 http://server/pub/mirrors/ubuntu bionic-security InRelease [88.7 kB]",
        "Hit:7 http://apt.package123.org/pub/repos/apt bionic-pgdg InRelease",
        "Fetched 252 kB in 1s (222 kB/s)",
        "Reading package lists..."

我想考虑是否有任何一行有 2 个字符串“Err”和“package”,这意味着它无法从网站更新 apt 缓存 - https://download.package.com/

我在想下面这样的事情:

      changed_when: >
        (("Get" in return.stdout_lines) and
        ("package" in ret.stdout_lines))    or
        (("Hit" in return.stdout_lines) and
        ("package" in ret.stdout_lines))

      failed_when: >
        ("Err" in return.stdout_lines) and
        ("package" in ret.stdout_lines)

这里的问题是 2 个字符串是查找所有行还是逐行查找? 如果是这样,如何逐行检查。

【问题讨论】:

    标签: automation ansible yaml devops


    【解决方案1】:

    当 APT 下载包失败时,会返回错误。在这种情况下,不再使用标准输出,而是使用 stderr。因此,您很可能需要将其更改为:

    # Update apt cache reposoitary
            - name: Update apt cache
              command: apt-get update
              ignore_errors: true
              register: return
    
    # Debug msg
            - debug:
                msg: '{{ return }}'
              changed_when: >
                (("Get" in return.stderr_lines) and
                ("newrelic" in ret.stderr_lines))    or
                (("Hit" in return.stderr_lines) and
                ("newrelic" in ret.stdout_lines))
              failed_when: >
                ("Err" in return.stderr_lines) and
                ("newrelic" in ret.stderr_lines)
    

    这里的问题是 2 个字符串是查找所有行还是逐行查找?

    答:它会在该任务的 stderr_lines 输出中查找所有“文本”。

    如果是这样,如何逐行检查。

    答:你的代码看起来很正常。

    也许您想自己测试这个特定的用例。您可以做的是将以下内容附加到目标系统上的 /etc/hosts:127.0.0.1 download.newrelic.com

    这样,APT 在目标机器上查找存储库。由于它不存在,因此下载将失败。

    还要注意 Ansible 有 update cache built-in the APT module

    【讨论】:

    • stderr 与 stdout_lines 相同,但在一行中,我在 "Hit:4 download.newrelic.com" 特定行上寻找它有 download.newrelic.com 的内容......如果这个失败我会认为我的 apt 更新失败了,因为我不担心是否有任何其他线路失败了。我本可以输入“Hit:4 download.newrelic.com”,但在这种情况下,这里的数字 4 可以是任何其他数字用于 1000 台服务器中的其他服务器。我只想过滤掉“Err:* download.newrelic.com”失败。
    【解决方案2】:

    这里是工作代码 -

    -----------------
    # Update apt cache reposoitary
            - name: Update apt cache
              command: apt-get update
              register: return
              failed_when: return.stdout_lines is search("Hit:* https://download.package.com")
              changed_when: return.stdout_lines is search("Err:* https://download.package.com")     
    
    # Debug msg
            - debug:
                msg: '{{ return }}'
    
    -----------------
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多