【问题标题】:Filter webhook payload for modified repositories过滤已修改存储库的 webhook 有效负载
【发布时间】:2020-09-30 16:59:17
【问题描述】:

我正在使用 Gitlab 和 AWX 构建管道,并且需要过滤 webhook 的有效负载。我需要的基本上只是将 project1 和 project2(在两个 / 之间)解析为一个我可以在其他角色中使用的变量。

tower_webhook_payload:
  after: 
  before: 
  checkout_sha: 
  commits:
    - id: 
      message: 
      title:  
      timestamp: 
      url: >-
        author:
        name: 
        email: 
      added: []
      modified:
        - repository/project1/file1
      removed: []
    - id: 
      message:
      title: 
      timestamp: 
      url: >-
      author:
        name: 
        email: 
      added: 
      modified:
        - repository/project2/file2
      removed: []

这是我的粗略想法,但我没有太多使用列表或正则表达式的经验。

- debug: msg="{{ tower_webhook_payload.commits | select('match', 'modified') | list }}"

"msg": "Unexpected templating type error occurred on ({{ tower_webhook_payload.commits | select('match', 'modified') | list }}): expected string or bytes-like object"}

【问题讨论】:

    标签: ansible yaml


    【解决方案1】:

    实现此目的的一种方法:

    1. 提取每个commitsmodified 列表属性。
    2. 展平结果,以便获得所有收集元素的单一列表。
    3. regex_replace 过滤器映射到每个元素以仅提取项目名称。下面的正则表达式捕获初始 repository/ 之后不包含 / 的所有内容,并仅用该捕获替换整个匹配项
    4. 应用unique 过滤器,以便消除重复项(如果有)
    ---
    - debug:
        msg: "{{ tower_webhook_payload.commits | map(attribute='modified') | flatten
          | map('regex_replace', 'repository/([^/]*)/.*', '\\1') | unique }}"
    

    编辑:使用旧版本的 ansible,您可能必须在映射后显式转换为列表

    ---
    - debug:
        msg: "{{ tower_webhook_payload.commits | map(attribute='modified')
          | list | flatten
          | map('regex_replace', 'repository/([^/]*)/.*', '\\1')
          | list | unique }}"
    

    【讨论】:

    • [警告]:在回调插件中使用方法 (v2_runner_on_ok) 失败():“set”类型的对象不是 JSON 可序列化的跨度>
    • 你能添加你的剧本吗?因为上面的内容适用于您在使用 ansible_playbook 的命令行上直接启动的简单 playbook 中的示例数据。
    • 除了调试信息外,剧本是空的。但是,正如我所说,我正在使用 ansible 塔。 Ansible 版本 2.8.5 和 AWX 9.0.1.0
    【解决方案2】:

    使用json_query 怎么样:

        - debug: msg="{{ tower_webhook_payload | json_query(\"commits[].modified[0]\") }}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      • 2022-11-03
      • 2021-09-08
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 2012-09-18
      相关资源
      最近更新 更多