首页 文章

Ansible 1.6包含with_items已弃用

提问于
浏览
17

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

这是我的playboook:

- 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中执行此错误消息:

错误:[DEPRECATED]:包含with_items是已删除的已弃用功能 . 请更新你的剧本 .

如何将此剧本转换为适用于此ansible版本的内容?

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重写为module(在python中,bash,无论如何),将它放在你的剧本旁边的 library 目录中 . 然后你可以这样做:
    - bootstrap_site: site={{item}}
      with_items: sites
    

    Update: Ansible V2已经退出并带回包含with_items combo loop

  • 1

    我找到了一个答案来规避blahblah弃用的消息......正如原帖中所说的那样 .

    我添加了一个文件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
    

    等等....

    问候,汤姆

相关问题