0

Team, I have below task that runs on local host. But I need to run it on all hosts under a group defined in inventory for which I need to do delegate_to and with_items. Any hint how can run this task on all hosts?

      - name: verify if kernel modules exists
        stat:
          path: /lib/modules/{{ kernel_version }}/kernel/{{ item.dir }}/{{ item.module_name }}
          checksum_algorithm: sha1
        register: res
        failed_when: res.stat.checksum != item.sha1
        with_items:
          - { dir: fs/fscache, module_name: fscache.ko, sha1: "{{ checksum.fscache }}"  }
          - { dir: fs/cachefiles, module_name: cachefiles.ko, sha1: "{{ checksum.cachefiles }}" }
          - { dir: fs/isofs, module_name: isofs.ko, sha1: "{{ checksum.isofs }}" }
          - { dir: drivers/target , module_name: target_core_user.ko, sha1: "{{ checksum.target_core_user }}" }
          - { dir: drivers/target/loopback , module_name: tcm_loop.ko, sha1: "{{ checksum.tcm_loop }}" }

     - fail:
          msg: "FAILED TO FIND {{ item.item }}"
        with_items: "{{ res.results }}"
        when: item.stat.exists == False

Do I need to use below?

        delegate_to: "{{ item }}"
        with_items: "{{ groups['kube-gpu-node'] }}”

tried to use hosts: directly in task as below but i get syntax error

# tasks file for maglev-deployment
      - name: "verify if kernel modules exists"
        hosts: kube-gpu-node
        stat:
          path: /lib/modules/{{ gpu_node_kernel }}/kernel/{{ item.dir }}/{{ item.module_name }}
          checksum_algorithm: sha1
        register: res
        #failed_when: res.results.item.sha1 != item.sha1
        #failed_when: res.results[0] == ''
        with_items:
          - { dir: fs/fscache, module_name: fscache.ko, sha1: "{{ checksum.fscache }}"  }
          - { dir: fs/cachefiles, module_name: cachefiles.ko, sha1: "{{ checksum.cachefiles }}" }
          - { dir: fs/isofs, module_name: isofs.ko, sha1: "{{ checksum.isofs }}" }
          - { dir: drivers/target , module_name: target_core_user.ko, sha1: "{{ checksum.target_core_user }}" }
          - { dir: drivers/target/loopback , module_name: tcm_loop.ko, sha1: "{{ checksum.tcm_loop }}" }
        ignore_errors: yes
      - debug:
          var: res

      - name: check if cachefilesd.conf  exists
        stat:
          path: /etc/cachefilesd.conf
        register: result

ERROR! 'hosts' is not a valid attribute for a Task

The error appears to be in '/home/ssh/tasks/main.yaml': line 2, column 9, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:
deployment
      - name: "verify if kernel modules exists"
        ^ here

This error can be suppressed as a warning using the "invalid_task_attribute_failed" configuration
AhmFM
  • 1,552
  • 3
  • 23
  • 53
  • Not sure if I got it correctly.but you can create your group task under - hosts: kube-gpu-node, and for the localhost, hosts:kube-gpu-node – Smily Nov 06 '19 at 09:22
  • Run your tasks on your group, i.e. in your play `hosts: localhost => hosts: kube-gpu-node`. The task will run on each host and you won't have to delegate. – Zeitounator Nov 06 '19 at 10:26
  • so there is no way i can do this from role and not the play? – AhmFM Nov 07 '19 at 06:43

0 Answers0