Difference between revisions of "Ansible"

From Christoph's Personal Wiki
Jump to: navigation, search
(New page: '''Ansible''' is an open-source software platform for configuring and managing computers. It combines multi-node software deployment, ''ad hoc'' task execution, and configuration managemen...)
 
(Examples)
Line 14: Line 14:
 
  $ git init
 
  $ git init
  
* Create your inventory file and add it to your [[git]] repo (we will call our development group of hosts "<code>dev</code>"):
+
* Create your [http://docs.ansible.com/intro_inventory.html inventory file] and add it to your [[git]] repo (we will call our development group of hosts "<code>dev</code>"):
 
  $ cat << EOF > hosts
 
  $ cat << EOF > hosts
 
  [dev]
 
  [dev]
  192.168.22.10
+
# Development host using a Ubuntu 14.04 vagrant box
 +
  192.168.1.10 ansible_ssh_user=vagrant
 
  $ git add hosts
 
  $ git add hosts
 +
 +
Note: The default inventory file lives at <code>/etc/ansible/hosts</code>. However, since I will be storing this file in my git repo, I like to keep it with the rest of my Ansible files.
 +
 +
* Ansible command syntax:
 +
$ ansible <host-pattern> [-f forks] [-m module_name] [-a args]
  
 
* Test that you can communicate with your vagrant VM (I like to sometimes add "<code>-vvvv</code>" for extra verbosity whilst debugging/developing):
 
* Test that you can communicate with your vagrant VM (I like to sometimes add "<code>-vvvv</code>" for extra verbosity whilst debugging/developing):
 +
$ ansible dev -i hosts -u vagrant -vvvv -m command -a "cat /etc/issue"
 +
10.11.1.103 | success | rc=0 >>
 +
Ubuntu 14.04.2 LTS \n \l
 
  $ ansible dev -i hosts -u vagrant -vvvv -m command -a "uptime"
 
  $ ansible dev -i hosts -u vagrant -vvvv -m command -a "uptime"
  
* Update your remote (vagrant VM, in this case) machine ("<code>-s</code>" to use <code>`sudo`</code>):
+
Note: Since we defined our vagrant user in the inventory file ("<code>hosts</code>"), we no longer need to specify this user in the following examples. Also note that my vagrant box already has my [[SSH]] public key copied over (if not, make sure to add "<code>-k</code>" so Ansible prompts you for the password).
  $ ansible dev -i hosts -u vagrant -s -m command -a "apt-get update"
+
 
 +
$ ansible dev -i hosts -m ping
 +
10.11.1.103 | success >> {
 +
    "changed": false,
 +
    "ping": "pong"
 +
}
 +
 
 +
* Update your remote (vagrant VM, in this case) machine ("<code>-s</code> or "<code>--sudo</code>" to run operation with <code>`sudo`</code>):
 +
  $ ansible dev -i hosts -s -m command -a "apt-get update"
  
 
  [remote] $ tailf /var/log/syslog
 
  [remote] $ tailf /var/log/syslog

Revision as of 20:59, 10 June 2015

Ansible is an open-source software platform for configuring and managing computers. It combines multi-node software deployment, ad hoc task execution, and configuration management. It manages nodes over SSH and requires Python (2.4 or later) to be installed on them. Modules work over JSON and standard output and can be written in any programming language. The system uses YAML to express reusable descriptions of systems.

Ansible is a DevOps tool for configuring, deploying, monitoring, and automating servers (among other things). This article will only discuss the Linux aspects. Most of the examples will use Vagrant. However, some of the examples/demos will include Rackspace, DigitalOcean, and Amazon's AWS as well.

Examples

Note: This article assumes you already have Vagrant and Ansible installed.

In the following examples, lines starting with "$" indicate a command to be run on the Ansible controlling machine (my laptop/local machine in most cases) and "[remote] $" indicates a command to be run on one of the nodes.

  • Setup your local Ansible environment:
$ mkdir -p $HOME/dev/ansible
$ cd $HOME/dev/ansible
$ git init
  • Create your inventory file and add it to your git repo (we will call our development group of hosts "dev"):
$ cat << EOF > hosts
[dev]
# Development host using a Ubuntu 14.04 vagrant box
192.168.1.10	ansible_ssh_user=vagrant
$ git add hosts

Note: The default inventory file lives at /etc/ansible/hosts. However, since I will be storing this file in my git repo, I like to keep it with the rest of my Ansible files.

  • Ansible command syntax:
$ ansible <host-pattern> [-f forks] [-m module_name] [-a args]
  • Test that you can communicate with your vagrant VM (I like to sometimes add "-vvvv" for extra verbosity whilst debugging/developing):
$ ansible dev -i hosts -u vagrant -vvvv -m command -a "cat /etc/issue"
10.11.1.103 | success | rc=0 >>
Ubuntu 14.04.2 LTS \n \l
$ ansible dev -i hosts -u vagrant -vvvv -m command -a "uptime"

Note: Since we defined our vagrant user in the inventory file ("hosts"), we no longer need to specify this user in the following examples. Also note that my vagrant box already has my SSH public key copied over (if not, make sure to add "-k" so Ansible prompts you for the password).

$ ansible dev -i hosts -m ping
10.11.1.103 | success >> {
    "changed": false, 
    "ping": "pong"
}
  • Update your remote (vagrant VM, in this case) machine ("-s or "--sudo" to run operation with `sudo`):
$ ansible dev -i hosts -s -m command -a "apt-get update"
[remote] $ tailf /var/log/syslog
Jun 10 18:52:36 vagrant ansible-command: Invoked with executable=None shell=True args=apt-get update  removes=None creates=None chdir=None

See also

External links