将with_fileglob与Ansible Playbooks中的另一个列表结合使用

时间:2014-07-30 11:43:54

标签: ansible ansible-playbook

所以我有一个Ansible playbook,我试图为列表中的每个项目调用一个命令,但也在fileglob上运行该命令。在Ansible中有一个“with_nested”,它可以采用变量名,但是如果我添加一个“with_fileglob”,它只是插入“with_fileglob”作为文件名,而不是实际执行一个glob。

  vars:
    repo_versions:
      - version: trusty
        distribution: Ubuntu
      - version: wheezy
        distribution: Debian

  ...

  - command: reprepro -b /var/www/html includedeb {{ item[0].version }} {{ item[1] }}
    with_nested:
      - repo_versions
      with_fileglob: /home/repoman/debs/*.deb
    when: debs_available.stat.exists == True

我尝试了几种不同的组合,我似乎无法让它在双for循环中处理命令(对于每个.deb文件的每个.version)

1 个答案:

答案 0 :(得分:3)

这应该是你想要完成的事情。

我使用shell模块注册fileglob的输出,然后使用循环中寄存变量的stdout_lines属性。我已将测试中的任务转换为您的实际命令和路径,因此您可能需要仔细检查:

  vars:
    repo_versions:
      - version: trusty
        distribution: Ubuntu
      - version: wheezy
        distribution: Debian

  tasks:
    - shell: ls -1 /home/repoman/debs/*.deb
      register: repo_list

    - command: reprepro -b /var/www/html includedeb {{ item[0].version }} {{ item[1] }}
      with_nested:
        - repo_versions
        - repo_list.stdout_lines