Ansible Cookbook

Really, an Ansible Cookbook? Yeah I know :-). The documentation of Ansible is phenomenal. This is more like a personal Cookbook, small recepts with some remarks.

It’s also really handy you can copy the code blocks.


Files and Directories

In this section you will find sniplets about files and directories.

Tempory files and directories

In general it’s oke to use temporary files and or directories that your Ansible scripts will create. However it is good practice to cleanup when you leave. Make sure you remove your stuff before ending your Ansible script.

- name: Cleanup working files
    path: /tmp/stoporacle.sql
    state: absent


Make sure a directory exist and it has the correct rights. Also you can run this task when adding the --tags "install" to your playbook.

- name: Make sure /opt/tmp exist
    path: "/opt/tmp"
    state: directory
    mode: '0754'
  - install


Create executable script from within playbook

This code creates a small script that will get the value of a environment variable. Somehow I couldn’t get the correct value from within the ansible facts and also scripts like env |grep variable didn’t work. I needed the value to inspect specific settings on a SAP application server.

- name: Create small script for env checking
    content: |
      # Script created by ansible
      env | grep rsdb_ssfs_connect      
    dest: "/home/{{sap_sid}}adm/check_os_var.csh"
    mode: 0700



A role is a set of ansible code that can be used over and over again for multiple hosts and inventories. The basic explanation is always, the webserver role. An ansible role consist of several directories:

  • defaults (optional)
  • tasks
  • templates(optional)
  • files (optional)
  • vars (optional)

Ansible will always look for a main.yml or main.yaml file in de defaults, vars and tasks directories. The task directory is mandatory because it contains the actions/scripts that need to be executed.


The defaults directory contains a default.yml or yaml that will contain the default values of each variable. It is nog needed that you define every variable in the defaults. I consider it best practice to do so.

If you get many variable and you want to split them in multiple files you can create a main directory in the defaults directory. In the main directory all files will be read and all variables will be available for your scripts.

Final words

Let me know in the comments if you are looking for specifics. I’m happy to dig into it :-).


Leave a Reply

Your email address will not be published.