ansible指定jinja2变量值

ansible指定jinja2变量值,ansible,jinja2,Ansible,Jinja2,以下任务未按预期发送工作。它应该在远程主机上创建icinga2配置文件。它创建文件,但不转换为定义的变量 负责任务: - name: create check for cq6-server template: src=icinga-cq6-template.j2 dest=/etc/icinga2/zones.d/icinga.dus3/{{ ansible_hostname }}-cq6-{{ project_name }}-{{ cq_role_1 }}.conf

以下任务未按预期发送工作。它应该在远程主机上创建icinga2配置文件。它创建文件,但不转换为定义的变量

负责任务:

- name: create check for cq6-server
  template:
    src=icinga-cq6-template.j2
    dest=/etc/icinga2/zones.d/icinga.dus3/{{ ansible_hostname }}-cq6-{{ project_name }}-{{ cq_role_1 }}.conf
    mode=0644
  when: "'cq6-servers' in group_names or 'cq6-dispatcher' in group_names"
  delegate_to: "{{ monitoring_server }}"
  notify: restart icinga2
它在Icinga2服务器上创建以下文件:

{{ ansible_hostname }}-cq6-{{ project_name }}-{{ cq_role_1 }}.conf
此文件仍然包含所有括号,甚至
{{{ansible\u managed}}


有人知道为什么ansible会这样做,但却可以处理其他任务/模板吗?

建议使用yaml dict表示法,即

- name: create check for cq6-server
  template:
    src: icinga-cq6-template.j2
    dest: "/etc/icinga2/zones.d/icinga.dus3/{{ ansible_hostname }}-cq6-{{ project_name }}-{{ cq_role_1 }}.conf"
    mode: 0644
  when: "'cq6-servers' in group_names or 'cq6-dispatcher' in group_names"
  delegate_to: "{{ monitoring_server }}"
  notify: restart icinga2
然而,您的变量可能是空的,所以我建议使用调试来验证,例如

- debug:
    var: "{{ ansible_hostname }}"

建议使用yaml dict表示法,即

- name: create check for cq6-server
  template:
    src: icinga-cq6-template.j2
    dest: "/etc/icinga2/zones.d/icinga.dus3/{{ ansible_hostname }}-cq6-{{ project_name }}-{{ cq_role_1 }}.conf"
    mode: 0644
  when: "'cq6-servers' in group_names or 'cq6-dispatcher' in group_names"
  delegate_to: "{{ monitoring_server }}"
  notify: restart icinga2
然而,您的变量可能是空的,所以我建议使用调试来验证,例如

- debug:
    var: "{{ ansible_hostname }}"

你是对的,它确实是一个未定义的变量。你是对的,它确实是一个未定义的变量。