Ansible task executed if file not exists condition

In this post, I will share an ansible task that is executed only if a condition is matched, the condition here is to check if file NOT exists on the system.

Ansible playbook create a new nginx vhost by template for wp at nginx proxy, then install wordpress latest if wp-config.php is not exists

if wp-config.php exists, the task Download WP is skipping.

Happy reading!

Ansible structure layout – the best approach

This is called Ansible structure layout – the best approach that we used to use it as a template for our projects. This may be not the best but you can customized it as to fit your own.

Directory Layout

The top level of the directory would contain files and directories like so:

How to Arrange Inventory, Stage vs Production

  • production.ini: contains the inventory of all production hosts
  • stage.ini: contains the inventory of all staging hosts

It is suggested that you define groups based on the purpose of the host (roles) and also geography or data center location (if applicable):

Group And Host Variables

We can also assign variables to all hosts in a group or specific group or specific hosts. Let’s set those now:

If we had any default values or values that were universally true, we would put them in a file called group_vars/all:

We can define specific hardware variance in systems in a host_vars file, but avoid doing this unless you need to:

Playbooks Are Separated By Role

Playbook webservers.yml is used for all hosts in group webservers with roles common, nginx

Task And Handler Organization For A Role

Below is an example tasks file that explains how a role works. Our common role here just sets up NTP, but it could do more if we wanted:

Here is an example handlers file. As a review, handlers are only fired when certain tasks report changes and are run at the end of each play:

Let’s run your playbook

What about just reconfiguring NTP on everything? Easy.:


What about just reconfiguring my webservers?:


What about just my webservers in Boston?:


What about just the first 10, and then the next 10?:

And of course, just basic ad-hoc stuff is also possible.:

Stage vs Production

As also mentioned above, a good way to keep your stage (or testing) and production environments separate is to use a separate inventory file for stage and production. This way you pick with -i what you are targeting. Keeping them all in one file can lead to surprises!

Ansible template module for wordpress nginx vhost

In this post, I will share a simple playbook that run on localhost to demonstrate ansible template module with  nginx vhost for WordPress.

playbook-wp-vhost.yml

templates/wp-vhost.conf.j2

Directory structure layout:

How to run playbook:

Ansible Ad-hoc Commands

Ansible ad-hoc commands will do a quick task but do not want to save that command for later. In below commands “-a” indicates the ad-hoc command.
To see the list of nodes from the master ad-hoc commands.

To create a demo file in node2 from the server using ad-hoc commands.

To install Java in node2 from the server using ad-hoc commands.

To start/manage services by using ad-hoc commands

To copy a file from ansible server to nodes by using ad-hoc commands.

To create a file by using ad-hoc commands

To remove a file by using ad-hoc commands

To create a directory by using ad-hoc commands

Ref:
https://devopssource.blogspot.com/2018/11/ansible-ad-hoc-commands.html
Push VS Pull

Beginning Ansible in 5 Minutes

So let me show you how easy it is to get started with Ansible.

Install

Let’s assume you’ll use pip to get this done :

$ sudo easy_install pip
$ sudo pip install ansible

Make sure it installed by running ansible --version.

Concepts

You’ll often hear that Ansible is agent-less and uses a push approach (as opposed to pull).

In a nutshell, Chef or Puppet work by installing an agent on the hosts they manage. This agent is pulling changes from a

master host, using their own channel (usually not SSH).

Push VS Pull

Ansible on the other hand is simply using SSH to push changes from wherever it runs (a server or your own laptop).

Conceptually, it’s as if instead of connecting to your machines with SSH and running commands manually, you could

script the whole thing and run it automatically.

Ansible VS SSH

We’ll get familiar below with more of Ansible’s concepts: Inventories, Playbooks, Roles and Tasks.

Adding a host with an inventory

The first thing for us to do once Ansible is installed is to specify which hosts we want to manage.

  1. Add a new machine; Fedora, Ubuntu or CentOS will do
  2. Create a folder where you’ll keep the Ansible related code for this example. In this folder, add a file named hosts  following content:
  3. That’s it. Let’s just make sure this works. Run the following command:

    You should get something like that:

This file is called an inventory, it lists the hosts that you will be managing with Ansible.

Installing NGINX with roles

Go to the folder where you created your inventory in, create a roles/ subfolder and then run command:

It will install the NGINX role in the roles subfolder, making it available to Ansible when ran from this folder.

Now, we’re gonna create our first playbook: playbook is a key concept in Ansible. It defines what needs to be configured and executed on your hosts.

Add a file named deploy.yml in the same folder as your inventory with the following content:

We’re now ready to apply this to our host. Just run the following:

Ansible will spurt out logs while running your playbook and should finally display that all tasks were run successfully (“ok”):

Now, if you point your browser at your URL, you’ll get a 404 error from NGINX since we haven’t deployed our site yet. This however means that NGINX is indeed up and running (great success!).

Ansible loop

How to work with Ansible loop

Ansible loop provides a lot of methods to repeat certain tasks until a condition is met.

A basic example which can be used to install a lot of Linux packages can be written like the below example.

 

In the above task, instead of writing 3 separate task we have consolidated them into a single task.

In each iteration, the value of with_items block will be inserted in place of {{ item }}. 

Ansible loop with Index

In some scenarios knowing the index value might come in handy. You can use the “with indexed_items” for this. The loop index will be available at item.0 and the value will be available at item.1. index value starts at zero as usual.

You can also make changes to the index value like addition, subtraction etc.

Ansible loop with conditional

You can also use the “when” conditional statement along with the loop structure. Thus you can control the looping based on a variable or system facts.

The following example will run the task when the loop value is the same as the “loop_1” variable. Note that “item” is not enclosed in double brackets.

Looping through Dictionaries

You can loop through Ansible dictionary variable using the with_dict parameter. In the following task, I have declared a variable ‘Fruits’ with 3 key-value pairs. I am using the with_dict to loop through all the values.