Ansible The conditional check failed. The error was error while evaluating conditional

Viewed 1070

Why is the conditional check failed ?

My CSV file:

No.,OS,Information       
1,**Linux**,CentOS 8.3   
2,**Windows**,Windows 2016
---
- name: Test_CSV
  hosts: localhost
  gather_facts: True

  tasks:

    - read_csv:
        path: test.csv
        delimiter: ','
      register: servers

    - include_tasks: Linux.yml
      when: vm.OS == "Linux"

    - include_tasks: Windows.yml
      when: vm.OS == "Windows"

      loop: "{{ servers.list }}"
      loop_control:
        loop_var: vm

Linux.yml

---
- debug:
    msg: "OS = Linux"

Windows.yml

---
- debug:
    msg: "OS = Windows"

The error I get is this:

fatal: [localhost]: FAILED! => { "msg": "The conditional check 'vm.OS == "Linux"' failed. The error was: error while evaluating conditional (vm.OS == to be in '/itoa/condition/20210920/Test_CSV.yml': line 13, column 7, but may\nbe elsewhere in the file depending on the eto be:\n\n\n - include_tasks: Linux.yml\n ^ here\n" }

or is there another way Please help guide me.

Thanks,

1 Answers

The problem is the loop. You can not loop over two tasks. You have to define a block (maybe this will not work with include_tasks).

BUT anyway what you try to implement is the core functionality of Ansible. Do do not need to loop over hosts. Ansible exists to do this for you.

Define the hosts in your inventory. Add the Linux systems to a group "linux". Add the Windows systems to a group "windows". Use the groups as a "hosts" argument.

Now Ansible will execute the tasks for all hosts.

If you want to use your CSV file as an inventory, you have to write a dynamic inventory script.

Related