Executing a playbook

Execution of playbooks is done using the dedicated ansible-playbook command. The command relies on the same Ansible configuration file as we used previously and therefore we want to run the command from the root of our Ansible repository.

The syntax of the command is:

ansible-playbook <playbook.yml> [options]   

We will first run the following command (adapting the value of the private-key option):

$ ansible-playbook helloworld.yml 
      --private-key ~/.ssh/EffectiveDevOpsAWS.pem 
      -e target=ec2 
      --list-hosts  

The option -e (or --extra-vars) allows us to pass extra options for execution. In our case, we are defining the variable target (which we declared in the hosts file of our playbook) to be equal to ec2. This first ansible-playbook command will tell Ansible to target all EC2 instances. The option --list-hosts will make Ansible return a list of hosts that match the hosts criteria. It won't actually run anything against those hosts.

The output of the command will be something like:

playbook: helloworld.yml

play #1 (ec2): host count=1

54.175.86.38

The list-hosts option is a good way to verify your inventory and, on more complex playbooks with more specific hosts values, to verify which hosts would run actual playbooks, allowing you to verify that they are targeting the hosts you expect.

We now know which hosts will be impacted if we were to use this value for the target. The next thing we want to check is what will happen if we run our playbook. The ansible-playbook command has an option -C (or --check) that will try to predict the change a given playbook will make:

$ ansible-playbook helloworld.yml 
      --private-key ~/.ssh/EffectiveDevOpsAWS.pem 
      -e target=54.175.86.38 
      --check  

PLAY [54.175.86.38] **********************************************************

GATHERING FACTS ***************************************************************

ok: [54.175.86.38]

TASK: [HelloWorld | Installing node] ******************************************

changed: [54.175.86.38]

TASK: [HelloWorld | Copying the application file] *****************************

changed: [54.175.86.38]

TASK: [HelloWorld | Copying the upstart file] *********************************

changed: [54.175.86.38]

TASK: [HelloWorld | Starting the HelloWorld node service] *********************

failed: [54.175.86.38] => {"failed": true}

msg: no service or tool found for: helloworld

 

FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************

to retry, use: --limit @/Users/nathanielfelsen/helloworld.retry

54.175.86.38 : ok=4 changed=3 unreachable=0 failed=1

Running that command will execute our playbook in dry-run mode. Through that mode, we can ensure that the proper tasks will be executed. Because we are in dry-run mode, some of the modules don't really find everything they need to simulate how they would run and that's why we see that error at the end of the service module.

Having verified the hosts and code, we can finally run ansible-playbook and execute our changes:

$ ansible-playbook helloworld.yml 
      --private-key ~/.ssh/EffectiveDevOpsAWS.pem 
      -e target=54.175.86.38  

The output is very similar to the check command except that this time the execution finished properly. Our application is now installed and configured. We can verify that it is running correctly:

$ curl 54.175.86.38:3000
Hello World  

We were able to reproduce what we previously did with CloudFormation using Ansible.

Now that we have tested our first playbook, we can commit our changes. We will do that in two commits to break down the initialization of the repository and the creation of the role:

From the root of your Ansible repository, run the following commands:

$ git add ansible.cfg ec2.ini ec2.py
$ git commit -m "Configuring ansible to work with EC2"
$ git add roles helloworld.yml
$ git commit -m "Adding role for nodejs and helloworld"
$ git push  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset