twodanaz
twodanaz

Reputation: 11

Ansible Looping over list of directories and subdirectories with relative paths

How do I loop over a list of directories and subdirectories with relative paths in Ansible?

I tried to loop the processes of creating folders by reusing found paths as variables later on in the program.

This is for creating a list of every directory in one directory:

- find:
      paths: /usr/local/foo/bar/
      recurse: no
      file_type: directory
      register: find_result

I wanted this to create a list of every folder in /bar/

Every subfolder in /bar/ contains a subfolder named alice, bob, or charlie. For example:

/usr/local/foo/bar/www/alice

/usr/local/foo/bar/ww2/bob

/usr/local/foo/bar/ww3/charlie

I created a list for the folder names:

FolderTypes:

source: {

   - alice

   - bob

   - charlie

This is for looping over every alice, bob, or charlie and creating an 'example' folder if it doesn't exist already:

  - file:

      path: {{ item.path }}/{{ item.value }}/example

      state: directory

      owner: user

      group: user

  -  with_items:

      - "{{ find_result.directory }}"

      - "{{ items.FolderTypes }}"

The program is intended to save found paths, loop through them, and create folders in their subdirectories. I get an error when i try to check the syntax:

ERROR! Syntax Error while loading YAML.
  did not find expected key

The offending line appears to be:

  - file:
      path: {{ item.path }}/{{ item.value }}/example

                           ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

Upvotes: 1

Views: 9281

Answers (3)

jackaaxc
jackaaxc

Reputation: 31

In case you want to manipulate files in local (Ansible controller) host you could use with_fileglob

Sample:

- name: Copy each file over that matches the given pattern
  copy:
    src: "{{ item }}"
    dest: "/etc/fooapp/"
    owner: "root"
    mode: 0600
  with_fileglob:
    - "/playbooks/files/fooapp/*"

Upvotes: 1

sebthebert
sebthebert

Reputation: 12497

You get this error message because you forgot " at the beginning of path.

You should write something like this (with with_together: or with_nested: (not really clear in your question))

  - file:
      path: "{{ item.path }}/{{ item.value }}/example"
      state: directory
      owner: user
      group: user

  -  with_nested:
      - "{{ find_result.directory }}"
      - "{{ items.FolderTypes }}"

Upvotes: 0

Loïc Le Roy
Loïc Le Roy

Reputation: 55

I think you should use with_together than the with_items

I think your playbook would be something like this :

 - file:
      path: {{ item[0].path }}/{{ item[1] }}/example
      state: directory
      owner: user
      group: user
  -  with_items:
      - "{{ find_result.directory }}"
      - "{{ Types | list }}"

If not, could explain more that you need exactly.

Upvotes: 0

Related Questions