Copy a local variable file if it exists

786 views
Skip to first unread message

Pierre Mavro

unread,
May 29, 2014, 1:04:42 PM5/29/14
to ansible...@googlegroups.com
Hi,

I'm trying to do something with Ansible and I would like to know how to do something.

Is it possible to launch an action if a local (not remote) file exists? Example:

- name: Install firewall rules
  copy: src=init.{{ ansible_hostname }} dest=/etc/init.d/iptables owner=root group=root mode=0755 backup=yes

The problem here is my source fails (if init.{{ ansible_hostname }} doesn't exists), I got an error message. Is it possible to do a such thing?

Thanks

Nathan Howell

unread,
May 29, 2014, 7:47:25 PM5/29/14
to ansible...@googlegroups.com
I haven't tested this, but how about the stat module run as a local action?

- local_action: stat path=init.{{ ansible_hostname }}
  register: init_file


- name: Install firewall rules
  copy: ...
  when: init_file.stat.exists

Nathan

Pierre Mavro

unread,
May 30, 2014, 9:09:22 AM5/30/14
to ansible...@googlegroups.com
Hi,

Thanks a lot it works, that was absolutely what I was waiting for!

I totally missed local_action Ansible function.

Regards
Reply all
Reply to author
Forward
0 new messages