Create a gist now

Instantly share code, notes, and snippets.

@todgru /ansible.md
Last active Feb 13, 2017

What would you like to do?
I just figured out how to use Ansible. This is my Hello World.

Ansible

Ansible is a tool to automate platform changes such as deploying new app code or ensure services are installed. The Ansible application runs on both your local machine and you're remote server. There is not need for a central master - like Puppet.

A local version of the playbook is used to update your remote servers. Playbooks are similar to a Puppet Manifest.

All communication between local and remote are handled though ssh. So make sure you can ssh into your remote server from where you are.

Setup

The gist of getting an Ansible Ping to return a Pong:

  • I'm running OSX locally. I ran $ brew instsall ansible. That gave me version 2.2.1.0.
  • I launched my server with an ssh key-pair that I have. Make a note.
  • On my Ubuntu server, I ensured that Ansible is installed when I launch the instance, sudo apt-get -y install ansible.

Now we have Ansible available both locally and on our remote server. To get Ansible to know about the remote server, you'll need a hosts file.

Since Brew installs the host file in an inconvienent location, I created my own. The hosts file is also known as an inventory file. I keep in in my project directory. Here is what mine looks like:

[myserver]
12.34.56.78 ansible_user=ubuntu ansible_ssh_private_key_file=/path/to/my/key.pem

Super simple! I declare what user and ssh key we'll use when contacting the remote server. I've also named this host configuration myserver. I could have more entries for other servers in this stack. Like mywebserver, myapiserver, etc. Each would have thier own ip addres.

Finally, here is a really simple test to see that your local Ansible can reach out and contact the remote server. From the command line:

$ ansible --inventory=./hosts myserver --module-name=ping

This tells Ansible to use the local hosts file and to use the module ping. You should get a response back to the console similar to this:

12.34.56.79 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

💥

I had a few errors when I tried to get this working the first time around. My entire issue stemmed from the fact that I didn't indicate the correct ssh key.

Here, another example, I want to make sure git is installed on my remote server. Here is how that would look:

$ ansible --inventory=./hosts myserver --module-name=shell --args='apt-get install git' --sudo

Like my sources say, Ansible is idempotent. You can run that last command as often as you want. Once git is installed, it will alays be installed. Ansible won't try to re-install it each time. Ansible stores a fact sheet on the remote sever. It knows what's running and installed.

Next up: Playbooks! This will allow us to specify several items in a single file instead of individually on the command line. It will ensure all of our services are installed and the lastest version of our application is deployed and running.

To be continued...

Sources and Inspiration

https://serversforhackers.com/an-ansible-tutorial

http://docs.ansible.com/ansible/intro_inventory.html#list-of-behavioral-inventory-parameters

❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment