0
votes

In my current playbook, I have something like the following:

- name: Copy cov-analysis-linux64-8.0.0.tgz
  copy: src=/home/devops/chroot/cov-analysis-linux64-8.0.0.tgz                     dest=/var/tmp/cov-analysis.tgz owner=devops
  register: coverity

- name: Copy fortidev-2.02.chroot.tar.bz2
  copy: src=/home/devops/chroot/fortidev-2.02.chroot.tar.bz2
  dest=/var/tmp/fortidev2.chroot.tar.bz2 owner=devops
  register: fortidev2

The list is getting longer and longer and for code readability, I want to use with_items to do it. I've updated it to something like:

- name: copy chroot tarball to the servers
  copy: src={{ item.src }} dest={{ item.dest }} owner=devops
  register: "{{ item.register }}"
  with_items:
    - { src: /home/devops/chroot/cov-analysis-linux64-8.0.0.tgz, 
        dest: /var/tmp/cov-analysis.tgz,
        register: coverity
      }
    - { src: /home/devops/chroot/fortidev-2.02.chroot.tar.bz2,
        dest: /var/tmp/fortidev2.chroot.tar.bz2,
        register: fortidev2
      }
    - { src: /home/devops/chroot/fmdev-6.0.tar.xz,
        dest: /var/tmp/fmdev6.tar.xz,
        register: fmdev6
      }

The problem is now when I want to refer to one of the registers in subsequent tasks, it couldn't find the variable. The code to refer it is:

- name: umount fortidev2 /proc|/dev|/tmp|/cov-analysis 
  command: umount {{ item }}
  become: yes
  when: fortidev2.changed
  with_items:
          - /home/devops/fortidev2/proc
          - /home/devops/fortidev2/dev
          - /home/devops/fortidev2/tmp
  ignore_errors: yes

The error is {"failed": true, "msg": "The conditional check 'fortidev2.changed' failed. The error was: error while evaluating conditional (fortidev2.changed): 'fortidev2' is undefined\n\nThe error appears to have been in '/var/lib/jenkins/jobs/Devops/jobs/update_chroot/workspace/roles/chroot/tasks/main.yml': line 70, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n# unmount /proc and /dev first before deleting the folder\n- name: umount fortidev2 /proc|/dev|/tmp|/cov-analysis\n ^ here\n"}

1

1 Answers

2
votes

Try this:

- name: copy chroot tarball to the servers
  copy: src={{ item.src }} dest={{ item.dest }} owner=devops
  register: tarballs
  with_items:
    - { src: /home/devops/chroot/cov-analysis-linux64-8.0.0.tgz, 
        dest: /var/tmp/cov-analysis.tgz,
        name: coverity
      }
    - { src: /home/devops/chroot/fortidev-2.02.chroot.tar.bz2,
        dest: /var/tmp/fortidev2.chroot.tar.bz2,
        name: fortidev2
      }
    - { src: /home/devops/chroot/fmdev-6.0.tar.xz,
        dest: /var/tmp/fmdev6.tar.xz,
        name: fmdev6
      }

- name: umount fortidev2 /proc|/dev|/tmp|/cov-analysis 
  command: umount {{ item }}
  become: yes
  when: tarballs.results | selectattr('item.name','equalto','fortidev2') | join('',attribute='changed') | bool
  with_items:
    - /home/devops/fortidev2/proc
    - /home/devops/fortidev2/dev
    - /home/devops/fortidev2/tmp