Looping Issue making me Loopy!

56 views
Skip to first unread message

Evan Tahler

unread,
Jan 27, 2015, 9:11:01 PM1/27/15
to ansible...@googlegroups.com
Hi All!  I cannot sort out how to itterate though this loop properly:

I have 2 data dictionaries: 

applications:
  - application_1
  - application_2

and 

big_var_collection:
  application_1:
    deployment_tasks:
      - { cmd: 'bundle exec rake thing:1', metadata: 'yay' }
      - { cmd: 'bundle exec rake thing:2', metadata: 'boo' }
  application_2:
    deployment_tasks:
      - { cmd: 'bundle exec rake thing:3', metadata: 'yay' }
      - { cmd: 'bundle exec rake thing:4', metadata: 'boo' }

In this example, I want to run 4 commands: cd into the directory and run the `cmd`, IE:

cd ~/www/projects/application_1 && bundle exec rake thing:1
cd ~/www/projects/application_1 && bundle exec rake thing:2
cd ~/www/projects/application_2 && bundle exec rake thing:3
cd ~/www/projects/application_2 && bundle exec rake thing:4

I would have assumed that the following loop would work, but I always end up with an access error of some sort

- name: run deployment tasks
  shell: > 
    cd ~/www/projects/{{ item.0 }} && {{ item.1.cmd }}
  with_nested: 
  - applications
  - big_var_collection[item.0].deployment_tasks

Help?

mic...@mcrilly.me

unread,
Jan 28, 2015, 1:28:05 PM1/28/15
to ansible...@googlegroups.com
Hello.

I'm wondering if this data stucture is something you're getting back from another application or external source? If not, are you able to define the structure in anyway you desire? If not, why not?

Here is the code I managed to come up with, which has a much, much simpler data structure and does the same job.

---
- hosts: all
  sudo
: no
  vars
:
    applications
:
     
# - { cmd: 'bundle exec rake thing:1', path: 'application_1' }
     
- { cmd: 'uptime', path: 'application_1' }
     
- { cmd: 'uptime', path: 'application_2' }
     
- { cmd: 'uptime', path: 'application_3' }
     
- { cmd: 'uptime', path: 'application_4' }
  tasks
:
   
- name: Run dev tasks
      shell
: >
        cd
{{item.path}} && {{item.cmd}}
      with_items
: applications



I hope this helps.

Evan Tahler

unread,
Jan 28, 2015, 1:43:27 PM1/28/15
to ansible...@googlegroups.com
The data structure is defined elsewhere, and it would easiest not to change it.

I'm currently migrating from Chef to Ansible, and the only pain point I've had has been the lack of data manipulation tools.  In chef you have an (almost) full ruby VM to work with, so hash manipulation, loops, etc are all available to you.  

What is the best practice in Ansible-land?  Should I write a python plugin to manipulate the data structure?  I assume that I'm just doing something wrong with my loop syntax... 

Evan Tahler

unread,
Jan 28, 2015, 4:18:10 PM1/28/15
to ansible...@googlegroups.com
It looks like with_subelements is what I wanted:

- name: run deployment tasks
  shell: > 
    cd ~/www/projects/{{ item.0.name }} && {{ item.1.cmd }}
  with_subelements: 
  - big_var_collection
  - deployment_tasks

I added a 'name' property to the hash (which was the same as the key) to make this work:

big_var_collection:
  application_1:
    name: application_1
    deployment_tasks:
      - { cmd: 'bundle exec rake thing:1', metadata: 'yay' }
      - { cmd: 'bundle exec rake thing:2', metadata: 'boo' }
  application_2:
    name: application_2
    deployment_tasks:
      - { cmd: 'bundle exec rake thing:3', metadata: 'yay' }
      - { cmd: 'bundle exec rake thing:4', metadata: 'boo' }

mic...@mcrilly.me

unread,
Jan 29, 2015, 4:13:29 AM1/29/15
to ansible...@googlegroups.com
So you CAN manipulate the data structure? Glad it's working for you.
Reply all
Reply to author
Forward
0 new messages