使用with_subelements时Ansible v2.2错误在1.7中工作正常

时间:2018-05-09 13:23:53

标签: ansible

运行我的ansible playbook时出现以下错误。

  

任务[基地:Rsyslog yapilandiriliyor。]   ****************************************致命:[gkts.ahtapot]:失败了! => {"失败":true," msg":"条件检查   ' ansible_fqdn == {{item.1}}'失败。错误是:错误的同时   评估条件(ansible_fqdn == {{item.1}}):' item'是   undefined \ n \ n错误似乎已经出现   ' /etc/ansible/roles/base/tasks/rsyslog.yml' ;:第2行第3列,但   可能在文件的其他位置,具体取决于确切的语法   问题。\ n \ n违规行似乎是:\ n \ n --- \ n-名称:Rsyslog   yapilandiriliyor。\ n ^ here \ n"}

这在ansible 1.7中运行良好,但在ansible 2.2.1

中不起作用
- name: Rsyslog yapilandiriliyor.
  template:
        src: "{{ rsyslog['conf']['source'] }}"
        dest: "{{ rsyslog['conf']['destination'] }}"
        owner: "{{ rsyslog['conf']['owner'] }}"
        group: "{{ rsyslog['conf']['group'] }}"
        mode: "{{ rsyslog['conf']['mode'] }}"
# when: "ansible_fqdn == item.1"
  when: "ansible_fqdn == {{item.1}}"
  with_subelements:
    - "{{ossimciks}}"
    - "{{clients}}"
  notify:
    - rsyslog servisini yeniden baslat
  sudo: yes
  tags: rsyslog

ossimciks和客户在我的vars文件中定义:

ossimciks:
   server01:
      fqdn: "OSSIMCIK_FQDN"
      port: "20514"
      clients:
        - "LOG_KAYNAGI_FQDN"
        - "LOG_KAYNAGI_FQDN"

我错过了什么?

1 个答案:

答案 0 :(得分:0)

with_subelements:
  - "{{ossimciks}}"
  - "{{clients}}"

语法

我不知道Ansible在何处/如何/何时发生变化,但我认为

  • with_subelements 中的第一个元素是变量
  • with_subelements 中的第二个元素是一个键

这适用于我编写过的剧本,并匹配the relevant docs(自2.5以来,文档中没有with_*个循环[1]因为无论如何总是查找[2]) :

with_subelements:
  - "{{ ossimciks }}"
  - clients

数据

我不清楚您在游戏手册中使用{{ item.1 }}尝试定位哪个项目,但如果允许对您的数据进行微小更改,则可以对其进行构建,以便所显示的任何信息都可以在with_subelements循环中访问(我还对数据进行了扩展和重组,以阐明循环正在做什么):

---
- hosts: localhost
  gather_facts: false
  vars:
    fqdn_var: "OSSIMCIK_FQDN_2"
    ossimciks:
      - server: "server01"
        fqdn: "OSSIMCIK_FQDN_1"
        port: "20514"
        clients:
          - "LOG_KAYNAGI_FQDN_1"
          - "LOG_KAYNAGI_FQDN_2"
      - server: "server02"
        fqdn: "OSSIMCIK_FQDN_2"
        port: "20514"
        clients:
          - "LOG_KAYNAGI_FQDN_1"
          - "LOG_KAYNAGI_FQDN_2"

  tasks:
    - name: Output ossimciks contents.
      debug:
        msg: "{{ item.0.server }} client: {{ item.1 }}"
      with_subelements:
        - "{{ ossimciks }}"
        - clients
      when: item.0.fqdn == fqdn_var

输出:

skipping: [localhost] => (item=None)  => {"skip_reason": "Conditional result was False"}
skipping: [localhost] => (item=None)  => {"skip_reason": "Conditional result was False"}
ok: [localhost] => (item=None) => {
    "msg": "server02 client: LOG_KAYNAGI_FQDN_1"
}
ok: [localhost] => (item=None) => {
    "msg": "server02 client: LOG_KAYNAGI_FQDN_2"
}

编辑添加:我使用上述语法的剧本肯定在2.x版本系列中 - 我认为~2.1。