在我目前的剧本中,我有以下内容:
- 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
列表越来越长,为了代码可读性,我想使用with_items来完成它。我把它更新为:
- 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
}
问题是,当我想在后续任务中引用其中一个寄存器时,它找不到变量。引用它的代码是:
- 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
错误是 {“failed”:true,“msg”:“条件检查'fortidev2.changed'失败。错误是:评估条件时出错(fortidev2.changed):'fortidev2'未定义\ n \ n错误似乎是在'/var/lib/jenkins/jobs/Devops/jobs/update_chroot/workspace/roles/chroot/tasks/main.yml':第70行第3列,但可能在文件的其他位置,具体取决于确切的语法问题。\ n \ n违规行似乎是:\ n \ n#unmount / proc和/ dev首先删除文件夹\ n-名称:umount fortidev2 / proc | / dev | / tmp | / cov-analysis \ n ^这里\ n“个}
答案 0 :(得分:2)
试试这个:
- 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