struggling with include files

40 views
Skip to first unread message

Paul Davies

unread,
Apr 28, 2016, 8:10:34 AM4/28/16
to Ansible Project
I am seriously struggling with trying to include yml files in my top level play

I have prereqcheck.yml in which I want to set the host for the rest of the included files to use

---
- name: Perform all prerequisite checks
  hosts: localhost
  tasks:
    - include: prereqcheck_directories.yml
    #- include: prereqcheck_users.yml
    #- include: prereqcheck_owner.yml


for my included file I have

---
- vars:
    directories:
      - directory: "/opt/test"
      - directory: "/etc"
      - directory: "/xyz"

- tasks:
    - name: Check directories exist

      stat:
        path: "{{ item.directory }}"
      with_items: directories
      register: directory_stat

    - debug:
        var: item.1.directory
      with_indexed_items: directories
      when: directory_stat.results[item.0].stat.exists


if I add the hosts in prereqcheck_directories.yml then I can run it independently

However if I try to run my top level play, I get...
fatal: [localhost]: FAILED! => {"failed": true, "reason": "ERROR! no action detected in task\n\nThe error appears to have been in '/home/daviesp/D4/solarisbase/tasks/prereqcheck_directories.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- vars:\n  ^ here\n"}


Various other attempts invariable end in a syntax error
What am I doing wrong here? I have searched ansible docs for some guidance on how to indent using include files but cannot find what I am looking for.

How do I include different plays all using the same host set at the top level?

Brian Coca

unread,
Apr 28, 2016, 8:48:20 AM4/28/16
to ansible...@googlegroups.com
So you have 2 types of includes, plays or task lists, you are mixing play level directives in an include inside a play, this is not permitted. 

An include inside a play can ONLY have a list of tasks, you are putting vars: and tasks: directives which implies play. In >=2.0 vars can now be part of any task, so you can put it with the include itself.


- name: Perform all prerequisite checks
  hosts: localhost
  tasks:
    - include: prereqcheck_directories.yml
      vars:
        directories:
         - directory: "/opt/test"
         - directory: "/etc"
         - directory: "/xyz"

​the included file:​

---
    - name: Check directories exist
      stat:
        path: "{{ item.directory }}"
      with_items: directories
      register: directory_stat

    - debug:
        var: item.1.directory
      with_indexed_items: directories
      when: directory_stat.results[item.0].stat.exists


----------
Brian Coca

Paul Davies

unread,
Apr 28, 2016, 11:22:44 AM4/28/16
to Ansible Project
oh man - thank you :)

Johannes Kastl

unread,
Apr 28, 2016, 2:24:42 PM4/28/16
to ansible...@googlegroups.com
On 28.04.16 14:48 Brian Coca wrote:

> directories:
> - directory: "/opt/test"
> - directory: "/etc"
> - directory: "/xyz"

I wonder that this works, with having the same 'key' called directory
three times. I am not an expert but I would have guessed this should
cause all kinds of trouble, because how can you distinguish between
these three entries?

Johannes

signature.asc

Brian Coca

unread,
Apr 28, 2016, 5:03:58 PM4/28/16
to ansible...@googlegroups.com
its actually a list of dictionaries with single key
top:
 - dictionary: 'stuff'

top[0]['dictionary'] == 'stuff'


​If you remove the leading - they would 'flatten' and only the last entry would be accessible, as in that case they WOULD be multiple updates to the same key.​

----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages