Setup Ansible on Ubuntu

From Knowledge Center
Jump to: navigation, search

Installation of Ansible:

This article will take you through the steps to setup Ansible on Ubuntu:

$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install ansible -y

Using PIP:

$ sudo easy_install pip
$ sudo pip install ansible

Ref:

Setup SSH Config:

Ansible uses SSH to communite to remote nodes, so passwordless auth is convenient:

$ ssh-keygen -t rsa
$ ssh-copy-id myuser@node1.mydomain.com

If your key has an additional password layer for security, you can add the key and pass they password auth while adding it to your ssh-agent:

$ eval $(ssh-agent)
$ ssh-add ~/.ssh/id_rsa
Enter passphrase for /home/myser/.ssh/id_rsa:
Identity added: /home/myser/.ssh/id_rsa (/home/myser/.ssh/id_rsa)

For your ssh config:

$ cat .ssh/config
Host node1
    HostName node1.mydomain.com
    port 22
    user myuser
    IdentityFile ~/.ssh/id_rsa


Setup Host/Group information:

$ cat /etc/ansible/hosts

# ungrouped hosts
test1.mydomain.com
test2.mydomain.com

# grouped hosts
[dbs]
db1.mydomain.com
db2.mydomain.com

[web]
web1.mydomain.com
web2.mydomain.com

# patterned hosts
[app]
app[01:05].mydomain.com

Testing our Setup to all hosts:

We will test using the ping module, to all our hosts described in our /etc/ansible/hosts configuration:

$ ansible all -m ping

test1.mydomain.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
test2.mydomain.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
db1.mydomain.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
db2.mydomain.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
web1.mydomain.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
...

Testing only our dbs servers:

$ ansible dbs -m ping

db1.mydomain.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
db2.mydomain.com | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

Running Commands on your Nodes:

$ ansible dbs -a "/usr/bin/uptime"
db1.mydomain.com | SUCCESS | rc=0 >>
 14:46:20 up 2 days, 19:17,  2 users,  load average: 0.00, 0.01, 0.00

db2.mydomain | SUCCESS | rc=0 >>
 14:46:20 up 2 days, 19:17,  1 user,  load average: 0.02, 0.02, 0.00

Ref: