# Ansible conditions and logics

**URL:** <https://forum.ansible.com/t/ansible-conditions-and-logics/14896>\
**Category:** Ansible Project\
**Created:** [March 31, 2014, 11:56am UTC](https://forum.ansible.com/t/ansible-conditions-and-logics/14896 "2014-03-31T11:56:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maxim\_Odinintsev](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@Maxim\_Odinintsev](https://forum.ansible.com/u/Maxim_Odinintsev)\
**Post date:** [March 31, 2014, 11:56am UTC](https://forum.ansible.com/t/ansible-conditions-and-logics/14896/1 "2014-03-31T11:56:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [March 31, 2014, 8:42pm UTC](https://forum.ansible.com/t/ansible-conditions-and-logics/14896/2 "2014-03-31T20:42:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Maxim\_Odinintsev](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@Maxim\_Odinintsev](https://forum.ansible.com/u/Maxim_Odinintsev)\
**Post date:** [April 2, 2014, 10:20am UTC](https://forum.ansible.com/t/ansible-conditions-and-logics/14896/3 "2014-04-02T10:20:47Z")

</div>

Hello,

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

Thank you.

---

<div class="post-metadata">

**Author:** ![Maxim\_Odinintsev](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@Maxim\_Odinintsev](https://forum.ansible.com/u/Maxim_Odinintsev)\
**Post date:** [April 2, 2014, 10:53am UTC](https://forum.ansible.com/t/ansible-conditions-and-logics/14896/4 "2014-04-02T10:53:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [April 2, 2014, 12:22pm UTC](https://forum.ansible.com/t/ansible-conditions-and-logics/14896/5 "2014-04-02T12:22:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_Green](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@Brian\_Green](https://forum.ansible.com/u/Brian_Green)\
**Post date:** [April 4, 2014, 12:52am UTC](https://forum.ansible.com/t/ansible-conditions-and-logics/14896/6 "2014-04-04T00:52:32Z")

</div>

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’ ?

---

<div class="post-metadata">

**Author:** ![Maxim\_Odinintsev](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@Maxim\_Odinintsev](https://forum.ansible.com/u/Maxim_Odinintsev)\
**Post date:** [April 4, 2014, 12:58pm UTC](https://forum.ansible.com/t/ansible-conditions-and-logics/14896/7 "2014-04-04T12:58:26Z")

</div>

Yes, this is valid syntax and this conditions working as AND. And it working for me 😉  
You can create simple playbook for verifying it.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 4, 2014, 3:15pm UTC](https://forum.ansible.com/t/ansible-conditions-and-logics/14896/8 "2014-04-04T15:15:15Z")

</div>

Correct.
