Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible 1.6 include with_items deprecated

So looks like this feature has been deprecated, i really don't understand why, Ansible CTO's says that we should use instead with_nested but honestly i have no idea how to do it,

Here's my playboook:

- hosts: all
  user: root
  vars: 
   - sites:
     - site: site1.com
       repo: ssh://[email protected]/orgname/reponame
       nginx_ssl: true;
       copy_init:
        - path1/file1.txt
        - path2/file2.php
        - path2/file3.php

     - site: site2.net
       repo: ssh://[email protected]/orgname/reposite2

     - site: site4.com
       repo: ssh://[email protected]/orgname/reposite3
       copy_init:
        - path2/file2.php

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

And the error message when trying to execute this in Ansible 1.6.6:

ERROR: [DEPRECATED]: include + with_items is a removed deprecated feature. Please update your playbooks.

How can i convert this playbook to something that works with this ansible version?

like image 958
jmserra Avatar asked Jul 08 '14 06:07

jmserra


People also ask

Is With_items deprecated?

ERROR: [DEPRECATED]: include + with_items is a removed deprecated feature.

Can there be n number of plays inside a playbook?

A playbook can have 'n' number of plays in a list. In the following example, the first play has set of tasks for ubuntu servers and the second for centos servers.

How do you use multiple loops in Ansible?

Ansible's syntax also supports the idea of nested looping. Nested loops in many ways are similar in nature to a set of arrays that would be iterated over using the with_nested operator. Nested loops provide us with a succinct way of iterating over multiple lists within a single task.

What is With_dict in Ansible?

with_dict: - "{{ zones_hash }}" declares a list with a dict as the first index, and Ansible rightfully complains since it expects a dict. The solution kfreezy mentioned works since it actually gives a dictionary to with_dict and not a list: with_dict: "{{ zones_hash }}" Follow this answer to receive notifications.


2 Answers

There's no drop-in replacement, unfortunately. Some things you can do:

  • Pass the list to your included file and iterate there. In your playbook:

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

    And in bootstrap_site.yml:

    - some_Task: ...
      with_items: sites
    
    - another_task: ...
      with_items: sites
    
    ...
    
  • Rewrite bootstrap_site as a module (in python, bash, whatever), put it in a library dir next to your playbook. Then you could do:

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

Update: Ansible V2 is out and brings back the include + with_items combo loop!

like image 91
hkariti Avatar answered Sep 18 '22 23:09

hkariti


I found an answer to circumvent the blahblah-deprecated message... as asked in the original post.

I added a file vars/filenames.yml:

  filenames:
  - file1
  - file2
  - file3

Next I read these names at the beginning of the playbook:

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

Then, I can use them later:

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

and so on....

Regards, Tom

like image 40
Thomas Dieckmann Avatar answered Sep 20 '22 23:09

Thomas Dieckmann