State exists for group of items

1,028 views
Skip to first unread message

Patrick Heeney

unread,
Dec 6, 2013, 2:12:40 AM12/6/13
to ansible...@googlegroups.com
I keep running into this a lot but I am not sure how to handle it. Lets assume I have the following yaml:

project_paths:
  - path: /srv/www/web/app1
    name: app1
  - path: /srv/www/web/app2
    name: app2

Then I want to check if a file exists before running the following action:

- name: application | check for installer
  stat: path={{ item.path }}/install.php
  register: check_install
  with_items: project_paths

- name: application | run install
  command: php install.php chdir={{ item.path }}
  with_items: project_paths
  when: check_install.stat.exists == false

The issue is registering check_install since we are doing it in a loop. It looks to be resetting the variable for every item. I tried something like this and I get 'check_install' is undefined.

- name: application | check for installer
  stat: path={{ item.path }}/install.php
  register: check_install[item.name]
  with_items: project_paths

- name: application | run install
  command: php install.php chdir={{ item.path }}
  with_items: project_paths
  when: check_install[item.name].stat.exists == false

- name: done
  fail: msg="{{ item.name } installer does not exist"
  with_items: project_paths
  when: check_install[item.name].stat.exists == false

Basically I am wondering the best way to run stat or other register commands with_items so that I can later check that on other with_item tasks for the same variables.

Michael DeHaan

unread,
Dec 6, 2013, 7:49:54 AM12/6/13
to ansible...@googlegroups.com

Verify the results of a registered variable like so:

- debug: var=register




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Patrick Heeney

unread,
Dec 6, 2013, 12:18:13 PM12/6/13
to ansible...@googlegroups.com
Thank you! This is the code I ended up with for anyone that stumbles upon this:

- debug: >
    msg="{{ item.0.path }} - {{ item.1.stat.exists }}"
  with_together: 
    - project_paths
    - check_install.results

- name: application | run install
  command: php install.php chdir={{ item.0.path }}
  with_together: 
    - project_paths
    - check_install.results
  when: item.1.stat.exists




--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/MO9Wl-MWNDE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages