Ansible

Installation

Version 2.1.1.0 in Ubuntu 16.10 repository, 2.2.1.0 from Ansible's repo.

sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install -y ansible

Hosts

Add hosts into file /etc/ansible/hosts, for example

[proxy]
proxy01.domain.com
[db]
db01.domain.com
db02.domain.fr

Propagate SSH key

egrep -v '\[|^#|^$' /etc/ansible/hosts | xargs -1 ssh-copy-id

Add-hoc commands

Execute hostname on all nodes

ansible -a hostname all

Get a list of unique crt file on all proxy server

ansible -m command -a 'locate .crt' proxy | grep -v bundle | grep '^/' | sort | uniq

Playbooks basics

packages.yml

---
- hosts: all
  remote_user: root
  tasks:
  - name: Install packages
    yum:
      name: "{{ item }}"
      state: latest
    with_items:
      - bash-completion
      - mlocate
      - nmap
      - vim

Display list of hosts affected without doing anything

ansible-playbook --list-hosts packages.yml

Execute playbook

ansible-playbook packages.yml

Example output

remy@POADV175: ~/work/ansible $ ansible-playbook packages.yml                                                                                                                                     [10:16:48]

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [proxy01.domain.com]
...

TASK [Install packages] ********************************************************
changed: [proxy01.domain.com] => (item=[u'bash-completion', u'mlocate', u'nmap', u'vim'])
...

PLAY RECAP *********************************************************************
proxy01.domain.com : ok=2    changed=1    unreachable=0    failed=0
...