Iterate over inventory group

143 views
Skip to first unread message

Laurent Goussard

unread,
Jan 13, 2015, 3:41:07 AM1/13/15
to ansible...@googlegroups.com
Hello,

I try to setup a cron task using ansible (indeed) on my db slave pool.
This cron task is supposed to stop the replication process, dump the database and restart replication.
Because the backup takes a little time, I don't want to init the cron task at the same time on all the db slaves and I don't want to stop all the slaves at the same times either.

My first idea was to set a different time while looping on the group.
But it's definitely not as trivial as it seems :

- name: database backup cron task
  cron
: name="database backup" hour={{ item.key }} user="root" job="backup_db.sh" cron_file=database_backup
  with_dict
: groups.mysql

The point is an inventory group is a list, not a dictionnary and I cannot find any simple way to convert a list to a dict (whereas it's easy to convert a dict as a list)
I also tried to use the group list instead, but I cannot figure out how I can get an iterator key.

Did I miss something ? Do you have any clue regarding my (not so complex imho) problem ?

Dan Vaida

unread,
Jan 13, 2015, 2:47:22 PM1/13/15
to ansible...@googlegroups.com
to iterate over a hosts group you need with_items: groups['mysql'] instead of with_dict

Laurent Goussard

unread,
Jan 14, 2015, 3:37:02 AM1/14/15
to ansible...@googlegroups.com
Thanks, but as I said, I tried to loop over the group list, but I cannot get any iteration key.

I try to loop over a hosts group and getting an index of the current iteration in order to set a cron task at midnight for the first host, 01am for the second, 02am for the third, etc...

Dan Vaida

unread,
Jan 14, 2015, 6:23:07 AM1/14/15
to ansible...@googlegroups.com
Isn't it feasible for you to simply do
- hosts: mysql
  sudo
: yes
  tasks
:
   
- cron: name="database backup" hour={{ hour }} user="root" job="backup_db.sh" cron_file=database_backup

and then in your host_vars/member_of_mysql_group, you would have 
hour: "01"
and so on?

Matt Martz

unread,
Jan 14, 2015, 7:31:56 AM1/14/15
to ansible...@googlegroups.com
Take a look at with_indexed_items: 
http://docs.ansible.com/playbooks_loops.html#looping-over-a-list-with-an-index

--
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.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/606d4a34-9344-42a8-a36a-dd2601dcbd32%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Laurent Goussard

unread,
Jan 14, 2015, 12:22:03 PM1/14/15
to ansible...@googlegroups.com
That would be a fallback solution, but I'd prefer not to use group nor host vars for defining such iterator. That would lead on errors and that not very elegant, in my opinion..

Laurent Goussard

unread,
Jan 14, 2015, 12:23:22 PM1/14/15
to ansible...@googlegroups.com
Thank you Matt !
That's definitely what I needed.

There is a lot of useful gems hidden in the footer or subnotes of the documentation !


Le mercredi 14 janvier 2015 13:31:56 UTC+1, Matt Martz a écrit :

On Wednesday, January 14, 2015, Laurent Goussard <laur...@gmail.com> wrote:
Thanks, but as I said, I tried to loop over the group list, but I cannot get any iteration key.

I try to loop over a hosts group and getting an index of the current iteration in order to set a cron task at midnight for the first host, 01am for the second, 02am for the third, etc...

Le mardi 13 janvier 2015 20:47:22 UTC+1, Dan Vaida a écrit :
to iterate over a hosts group you need with_items: groups['mysql'] instead of with_dict

On Tuesday, 13 January 2015 09:41:07 UTC+1, Laurent Goussard wrote:
Hello,

I try to setup a cron task using ansible (indeed) on my db slave pool.
This cron task is supposed to stop the replication process, dump the database and restart replication.
Because the backup takes a little time, I don't want to init the cron task at the same time on all the db slaves and I don't want to stop all the slaves at the same times either.

My first idea was to set a different time while looping on the group.
But it's definitely not as trivial as it seems :

- name: database backup cron task
  cron
: name="database backup" hour={{ item.key }} user="root" job="backup_db.sh" cron_file=database_backup
  with_dict
: groups.mysql

The point is an inventory group is a list, not a dictionnary and I cannot find any simple way to convert a list to a dict (whereas it's easy to convert a dict as a list)
I also tried to use the group list instead, but I cannot figure out how I can get an iterator key.

Did I miss something ? Do you have any clue regarding my (not so complex imho) problem ?

--
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-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages