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/945837.cloudwaysapps.com/byzecxcvae/public_html/wp-content/themes/twentyfifteen-child-zenva 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/gamedevacademy.org/public_html 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

  tasks:
    - 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