17

所以看起来这个功能已经被弃用了,我真的不明白为什么,Ansible CTO 说我们应该使用 with_nested 但老实说我不知道​​该怎么做,

这是我的剧本:

- hosts: all
  user: root
  vars: 
   - sites:
     - site: site1.com
       repo: ssh://hg@bitbucket.org/orgname/reponame
       nginx_ssl: true;
       copy_init:
        - path1/file1.txt
        - path2/file2.php
        - path2/file3.php

     - site: site2.net
       repo: ssh://hg@bitbucket.org/orgname/reposite2

     - site: site4.com
       repo: ssh://hg@bitbucket.org/orgname/reposite3
       copy_init:
        - path2/file2.php

  tasks:
     - name: Bootstrap Sites
      include: bootstrap_site.yml site={{item}}

尝试在 Ansible 1.6.6 中执行此操作时出现错误消息:

错误:[已弃用]:include + with_items 是已删除的弃用功能。请更新您的剧本。

我怎样才能将此剧本转换为适用于此 ansible 版本的内容?

4

2 回答 2

21

不幸的是,没有临时替代品。你可以做的一些事情:

  • 将列表传递给您包含的文件并在那里迭代。在你的剧本中:

    vars:
        sites:
           - site1
           - site2
    tasks:
        - include: bootstrap_site.yml sites={{sites}}
    

    在 bootstrap_site.yml 中:

    - some_Task: ...
      with_items: sites
    
    - another_task: ...
      with_items: sites
    
    ...
    
  • 将 bootstrap_site 重写为模块(在 python、bash 等中),将其放在library剧本旁边的目录中。然后你可以这样做:

    - bootstrap_site: site={{item}}
      with_items: sites
    

更新: Ansible V2 已经发布并带回了 include + with_items组合循环

于 2014-07-14T15:50:59.810 回答
1

我找到了一个答案来规避 blahblah-deprecated 消息……正如原始帖子中所要求的那样。

我添加了一个文件 vars/filenames.yml:

  filenames:
  - file1
  - file2
  - file3

接下来我在剧本的开头阅读了这些名字:

  - name: read filenames
    include_vars: vars/filenames.yml

然后,我可以稍后使用它们:

  - name: Copy files 1
    copy: src=/filesrc1/{{ item }} dest=/filedest1/{{ item }} owner=me group=we
    with_items: filenames

等等....

问候,汤姆

于 2015-08-18T12:46:18.693 回答