From zero to something with ansible

While talking with a friend and chatting about IT stuff we spent a few moments talking about ansible, after this I decided to move myself to check about it, I found a few samples on the internet and make it work for me, I will not cover any details in this post, the ideia is just share the experience running it locally.

Install ansible 2.0

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

Setup a directory tree

mkdir -p ansible-repo/roles/redis/{files,tasks}

Create Inventory:

File: ansible-repo/hosts

[local]
localhost ansible_connection=local ansible_python_interpreter=/usr/bin/python2

Associate host with role

File: ansible-repo/redis.yml

- hosts: local
roles:
— redis

Create a task:

File: ansible-repo/roles/redis/tasks/main.yml

# install vim and redis package
# package command only available in >= 2.0 version
- name: install packages
package: name={{item}} state=present update_cache=yes
with_items:
— vim
— redis-server
# create configure files and register the change
- name: config redis
copy: src=redis/redis.conf dest=/etc/redis/
register: redisconfig
# restart service only if config have changed
- service: name=redis-server state=restarted enabled=yes
when: redisconfig.changed

Create a config file used to overwrite the default redis.conf

File: ansible-repo/roles/redis/files/redis/redis.conf

daemonize yes
pidfile /var/run/redis/redis-server.pid
port 6379
tcp-backlog 511
bind 127.0.0.1
timeout 0
tcp-keepalive 0
loglevel notice
logfile /var/log/redis/redis-server.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repldaemonize yes
pidfile /var/run/redis/redis-server.pid
port 6379
tcp-backlog 511
bind 127.0.0.1
timeout 0
tcp-keepalive 0
loglevel notice
logfile /var/log/redis/redis-server.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename “appendonly.aof”
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events “”
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

Run your playbook

cd ansible-repo && ansible-playbook -i hosts base.yml

your output will be something like this:

PLAY [local] *******************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [base : install redis package] ********************************************
+changed: [localhost] => (item=[u’redis-server’])
TASK [base : config redis] *****************************************************
changed: [localhost]
TASK [base : service] **********************************************************
changed: [localhost]
PLAY RECAP *********************************************************************
localhost : ok=4 changed=3 unreachable=0 failed=0

Tips

The previous code are available in github:

It has a rich documentation:

If you want list all the available variables:

ansible localhost -m setup

You also might want do filter a single variable:

ansible localhost -m setup -a ‘filter=*eth0*’

More examples at:

https://github.com/ansible/ansible-examples