Running Ansible directly from a yml file.


This post covers how to run ansible directly from a yml file.

Why running Ansible from a yml file?

We all already know Ansible as quite nifty tool for system configuration management. However, it still requires some degree of understanding when comes to connectivity, inventory files and other nuts and bolts. Sometimes you need to run a simple playbook just to apply a basic system configuration or a hardening task from the Vm itself and you don’t fancy to run commands with arguments.

How to achieve it?

Simply specifying the Ansible shebang on the very top of the yml file and making it executable.

#!/usr/bin/env ansible-playbook
- hosts: localhost
  connection: local


In order to complete this walk-through you need to have Vagrant and Git installed, the installation is not in the scope of this tutorial.

Please find the installation guides from the below link:

Setup and Demo.

In this demo we will perform few system hardening tasks such, enabling Ubuntu firewall denying all traffic except HTTP and SSH, installing NTP server and finally setting up NGINX web server.

git clone demo_ansible

cd demo_ansible

vagrant up

Vagrant will start an Ubuntu 16.04 server with Ansible already installed. Upon the boot, Ansible provisioner will be invoked and will execute the playbook as a simple and plain script.


Welcome to modify ansible_provision.yml and re-run the provisioner as:

vagrant provision


There are many ways and means to provision servers using this Ansible implementation, e.g using Terraform remote-exec or invoking the yml file via Packer script.

I hope you enjoyed this tutorial, stay tuned for more.