0
votes

I am getting the following error while running my ansible playbook.

TASK [base : Rsyslog yapilandiriliyor.] **************************************** fatal: [gkts.ahtapot]: FAILED! => {"failed": true, "msg": "The conditional check 'ansible_fqdn == {{item.1}}' failed. The error was: error while evaluating conditional (ansible_fqdn == {{item.1}}): 'item' is undefined\n\nThe error appears to have been in '/etc/ansible/roles/base/tasks/rsyslog.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: Rsyslog yapilandiriliyor.\n ^ here\n"}

This worked fine in ansible 1.7 but doesn't work in 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 and clients are defined in my vars file:

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

What am I missing?

1

1 Answers

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

Syntax

I don't know how where/how/when this might have changed in Ansible, but I think that

  • the first element in with_subelements is a variable, and
  • the second element in with_subelements is a key.

This works in playbooks I've written, and matches the relevant docs (since 2.5, there are no with_* loops [1] in the docs since they were always lookups anyhow [2]):

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

Data

It's not clear to me which item you're attempting to target with {{ item.1 }} in your playbook, but if making a minor change to your data is permissible, you can structure it so that any of the information shown is accessible within a with_subelements loop (I've also expanded and restructured the data a bit to clarify what the loop is doing):

---
- 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

This outputs:

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"
}


Edited to add: the playbooks where I've used the syntax above were definitely in the 2.x release series--I think ~2.1.