Ansible Quickies – Useful Code Snippets

Delete a remote folders and files in Ansible

file: path=/some-folder/some-subfolder state=absent


Sync files from a directory inside of another directory in Ansible

synchronize: src=/home/fariazz/www/cursowordpress/wp-content/themes/twentyfifteen-child-zenva/ dest=/home/ delete=yes


Execute bash code with Ansible. It gets executed with /bin/sh for you

shell: /usr/local/bin/some-tool --some_par=some_value


Add a cron job if it doesn’t exist with Ansible

cron: name="wordpress core update" minute="50" job="/usr/local/bin/wp --path=/home/deideaaa/domains/ core update"


Create a folder if it doesn’t exist with Ansible

file: path=path-here state=directory


Copy a local file to the server with Ansible

template: src=templates/nginx.conf dest=/etc/nginx/nginx.conf


Download a file from a remote server with Ansible

get_url: url=file-url dest=destination-path


Install Ubuntu packages with Ansible

apt: name=make update_cache=yes
sudo: yes
apt: name=make state=present
sudo: yes


Restart server service with Ansible

- name: restart nginx
Β  service: name=nginx state=restarted enabled=yes
Β  sudo: yes


Execute different actions if there was an error

    - name: restart sails server
      register: restartsails
      shell: forever restart /var/www/
      ignore_errors: true
    - name: start sails server
      shell: forever start /var/www/
      when: restartsails|failed