Ansible conditions and logics

232 views
Skip to first unread message

Maxim Odinintsev

unread,
Mar 31, 2014, 7:56:58 AM3/31/14
to ansible...@googlegroups.com
Hello,

I'm trying migrate out my infrastructure under ansible control, but stopped on simple things realization with ansible.

I would be very grateful if some one can explain, best way for moving on this simple shell script under ansible.
May be simplest way is running external shell, and not try to push it all under ansible logic ?

pushd /
for i in \
     
/etc/ipsec* \
     
/home \
     
/root \
     
do
     
[[ -r $i ]] || continue
     
[[ -L $i ]] && continue
      dst_dir
=$(readlink -m /protected$(dirname $i))
      dst_name
="${dst_dir}/$(basename $i)"
      mkdir
-p $dst_dir
      rsync
-a $i $dst_dir && rm -rf $i && ln -s $dst_name $i
      ls
-ld $i
done
popd

Thank you

James Cammarata

unread,
Mar 31, 2014, 4:42:39 PM3/31/14
to ansible...@googlegroups.com
If I understand correctly, you're copying the contents of several directories to a central location, removing the old path, and then creating a symlink to the new location? To do that with ansible you'd just need to call the synchronize module to copy the files (or use command/shell to do the rsync), and then use the file module to create the symlink.


--
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/a29d1d41-ffd3-40e9-8cc4-a4a891e443c6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Maxim Odinintsev

unread,
Apr 2, 2014, 6:20:47 AM4/2/14
to ansible...@googlegroups.com, jcamm...@ansible.com


Hello,

Yes, you understand correctly. How can I synchronize files on remote node (not from local host to remote) ?

Thank you.

Maxim Odinintsev

unread,
Apr 2, 2014, 6:53:52 AM4/2/14
to ansible...@googlegroups.com, jcamm...@ansible.com
Hello,

I'm tried make it over ansible, but in final, i think much easiest way is using script module.

If someone interesting, when i tried it, i'm used this construction for getting files attributes and states:

- name: Getting files stats
  stat
: path={{ item }}
 
register: st
  with_items
:
   
- /etc/asterisk
   
- /etc/bind
   
- /etc/GeoIP.conf
   
- /etc/iptables
   
- /etc/jabber
   
- /etc/monit
   
- /etc/mysql
   
- /etc/nginx
   
- /etc/postfix
   
- /etc/ppp
   
- /etc/snmp
   
- /etc/sphinx
   
- /etc/xl2tpd

- name: Moving on
  debug
: msg="doing something with {{ item.item }}"
 
when:
   
- item.stat.exists == true
   
- item.stat.islnk != true
  with_items
: st.results


Thank you

Marcin Prączko

unread,
Apr 2, 2014, 8:22:10 AM4/2/14
to ansible...@googlegroups.com
Hi,

I think that you have to find best way which works for you.

For example I have to setup quite a lot munin symlinks for checks which depends of server role.
1. Running this via ansible was really slow and caused quite complicated solution in terms of map links to roles.

2. So I decided to write SHell template - which allow much easier setup conditions and control what must be run when server belong to some goup, and then in role have following tasks.
- Copy shell script from template (during copy - ansible genertes everything what is requried in terms of symlinks for munin).
- Run shell script.

This solution is fast for ansible now and all what I have to do is update template if some sym links must be adjusted (added /removed).

Hope that will help you as well.

Best regards,
Marcin Praczko

Brian Green

unread,
Apr 3, 2014, 8:52:32 PM4/3/14
to ansible...@googlegroups.com, jcamm...@ansible.com
I've never seen this syntax used with when:

- name: Moving on
  debug
: msg="doing something with {{ item.item }}"
  
when:
    
- item.stat.exists == true
    
- item.stat.islnk != true
  with_items
: st.results

Is that a valid conditional? Is it equivalent to 'and' ?

Maxim Odinintsev

unread,
Apr 4, 2014, 8:58:26 AM4/4/14
to ansible...@googlegroups.com, jcamm...@ansible.com
Yes, this is valid syntax and this conditions working as AND. And it working for me ;)
You can create simple playbook for verifying it.

Michael DeHaan

unread,
Apr 4, 2014, 11:15:15 AM4/4/14
to ansible...@googlegroups.com, James Cammarata
Reply all
Reply to author
Forward
0 new messages