Ansible set_fact --> 该任务包含一个带有未定义变量的选项

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

如果我在剧本中使用 set_fact 和 if-else 语句,我会收到以下错误。

FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'hostname'. 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'hostname'\n\nThe error appears to be in '/runner/project/playbooks/Stack_Status.yml': line 148, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: Write report data\n      ^ here\n"}

有问题的代码在这里:

- name: Set final stack version fact
      set_fact:
        stack_version: >
          {% if composer_stack is defined and composer_stack != 'Stack not deployed' %}
            {{ composer_stack }}
          {% elif fleet_agent_stack is defined and fleet_agent_stack != 'Stack not deployed' %}
            {{ fleet_agent_stack }}
          {% else %}
            V3 Stack not deployed
          {% endif %}
- name: Gather server report
      set_fact:
        server_report: >
          {
            "hostname": "{{ inventory_hostname }}",
            "stack_version": "{{ stack_version | default('N/A') }}"
          }
name: Write report data
  shell: |
    echo "\"{{ server_report.hostname }}\",\"{{ server_report.stack_version }}\"" >> "{{ report_file }}"

我希望显示 Composer 的版本(如果正在运行),或者打印 Fleet-Manager 的版本,但两者都没有运行显示,Stack 未部署。

我希望显示 Composer 的版本(如果正在运行),或者打印 Fleet-Manager 的版本,但两者都没有运行显示,Stack 未部署。

ansible ansible-template
1个回答
0
投票

错误消息已经说明了

AnsibleUnsafeText object' has no attribute 'hostname'

字符串对象不是字典,因为使用

set_fact: server_report
任务创建了包含 YAML 内容的字符串。最小的示例手册

---
- hosts: localhost
  become: false
  gather_facts: false

  tasks:

  - set_fact:
      stack_version: >
        {% if composer_stack is defined and composer_stack != 'Stack not deployed' %}
        {{ composer_stack }}
        {% elif fleet_agent_stack is defined and fleet_agent_stack != 'Stack not deployed' %}
        {{ fleet_agent_stack }}
        {% else %}
        V3 Stack not deployed
        {% endif %}

  - set_fact:
      server_report: >
        {
           "hostname": "{{ inventory_hostname }}",
           "stack_version": "{{ stack_version | default('N/A') }}"
        }

  - name: Content of variable is
    debug:
      var: server_report | type_debug

  - name: Whereby it should be
    debug:
      var: server_report | from_yaml | type_debug

  - name: Now it is working
    debug:
      msg: "{{ ( server_report | from_yaml ).hostname | trim }}"

将产生

的输出
PLAY [localhost] *****************************

TASK [set_fact] ******************************

TASK [set_fact] ******************************
ok: [localhost]

TASK [Content of variable is] ****************
ok: [localhost] =>
  server_report | type_debug: str

TASK [Whereby it should be] ******************
ok: [localhost] =>
  server_report | from_yaml | type_debug: dict

TASK [Now it is working] *********************
ok: [localhost] =>
  msg: localhost

文档链接

© www.soinside.com 2019 - 2024. All rights reserved.