2016-07-19 59 views
0

在我目前的劇本註冊的名字,我有類似如下:Ansible - 如何使用從with_items

- 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 

的誤差 {「失敗」:真正的「味精」:「由於條件檢查‘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#在刪除文件夾\ n名稱之前先卸載/ proc和/ dev: umount fortidev2/proc |/dev |/tmp |/cov-analysis \ n^here \ n「}

回答

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 
相關問題