规则手册中的 Extra_vars 在 run_job_template 触发的 playbook 中显示为未定义

问题描述 投票:0回答:1

规则手册:

- name: Listen for events on a webhook
  hosts: all
  # Define our source for events
  sources:
    - ansible.eda.webhook:
        host: 0.0.0.0
        port: 5000
  # Define the conditions we are looking for
  rules:
    - name: Demo rule 
      condition: event.meta["headers"]["Os"] == "linux"
      action:
        run_job_template:
          name: Ansible-Windows-Template
          organization: default
          job_args:
            extra_vars:
              payload: "{{ event.payload.data }}"

模板剧本:

---
- name: Check Connectivity and Report on Windows Hosts
  hosts: G_WIN
  gather_facts: false
  tasks:
    - name: 00 - Display payload from the rulebook
      ansible.builtin.debug:
        msg: "Payload from the rulebook: {{ payload }}"
      delegate_to: localhost

错误信息:

{
  "msg": "The task includes an option with an undefined variable. The error was: 'payload' is undefined. 'payload' is undefined\n\nThe error appears to be in '/runner/project/win.yml': line 6, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n    - name: 00 - Display payload from the rulebook\n      ^ here\n",
  "_ansible_no_log": false
}

参考文档:变量

AAP版本:2.5

如有任何帮助,我们将不胜感激:-)

ansible ansible-automation-platform
1个回答
0
投票

我将 rulebook actionrun_job_template 更改为 run_playbook 并添加了前面的 print_event action 以提供 payload 的一些调试,如下所示:

- name: Listen for events on a webhook
  hosts: all
  # Define our source for events
  sources:
    - ansible.eda.webhook:
        host: 127.0.0.1
        port: 5000
  # Define the conditions we are looking for
  rules:
    - name: Demo rule 
      condition: event.meta["headers"]["Os"] == "linux"
      actions:
        - print_event:
            pretty: true
        - run_playbook:
            name: Ansible-Windows-Template
            extra_vars:
              payload: "{{ event.payload.data }}"

我使用 curl 将以下数据发布到 webhook:

curl --location --request POST 'http://127.0.0.1:5000' \
--header 'Content-type: application/json' \
--data-raw '{"data": "something"}'

并看到以下输出:

PLAY [Check Connectivity and Report on Windows Hosts] **************************

TASK [00 - Display payload from the rulebook] **********************************
ok: [127.0.0.1 -> localhost] => {
    "msg": "Payload from the rulebook: something"
}

这就是我们正在寻找的。

我想知道使用 run_job_templateextra_vars payload 变量是否有问题。将其重命名为其他名称(例如 info)可能是值得的,如下所示:

actions:
  - print_event:
      pretty: true
  - run_job_template:
      name: Ansible-Windows-Template
      organization: default
      job_args:
        extra_vars:
          info: "{{ event.payload.data }}"

并相应地更新模板

---
- name: Check Connectivity and Report on Windows Hosts
  hosts: G_WIN
  gather_facts: false
  tasks:
    - name: 00 - Display payload from the rulebook
      ansible.builtin.debug:
        msg: "Payload from the rulebook: {{ info }}"
      delegate_to: localhost
© www.soinside.com 2019 - 2024. All rights reserved.