Compilamos ansible:
echo 'app-admin/ansible' >> /etc/portage/package.accept_keywords/ansible emerge -av app-admin/ansible
En los servers a administrar es necesario tener gentoolkit instalado:
emerge -av app-portage/gentoolkit
NOTA: Ansible lee toda nuestra config SSH -> ~/.ssh/config pero solo si se utiliza el modo openssh, si se habilita paramiko NO
Añadimos hosts a gestionar:
mkdir /etc/ansible/ chown -R root:kr0m /etc/ansible/ chmod 775 /etc/ansible/ vi /etc/ansible/hosts
Pingeamos los hosts para asegurarnos de que hay conectividad:
ansible all -m ping node00 | SUCCESS => { "changed": false, "ping": "pong" }
Si tenemos acceso con un usuario restringido y queremos pasar a root mediante sudo.
Accedemos como bruce pasando a root mediante sudo:
ansible all -m ping -u bruce -b
Accedemos como bruce pasando a batman mediante sudo:
ansible all -m ping -u bruce -b --become-user batman
Podemos ejecutar un comando(Ad-Hoc command) en todos los servers con:
ansible all -a "uname -a" node00 | SUCCESS | rc=0 >> Linux node00 4.4.0-16-generic #32-Ubuntu SMP Thu Mar 24 22:38:01 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
Para gestionar software:
http://docs.ansible.com/ansible/portage_module.html
Instalamos htop mediante ansible:
ansible all -m portage -a "name=htop state=present" lxd00 | SUCCESS => { "changed": false, "msg": "Packages already present." }
En el fichero de hosts se puede organizar por grupos:
[webservers] foo.example.com bar.example.com [dbservers] one.example.com two.example.com three.example.com
Además podemos indicar el puerto a utilizar:
badwolf.example.com:5309
También es posible utilizar patrones:
[webservers] www[01:50].example.com [databases] db-[a:f].example.com
Si necesitamos utilizar variables para configurar los hosts se pueden definir aquí también:
[atlanta] host1 http_port=80 maxRequestsPerChild=808 host2 http_port=303 maxRequestsPerChild=909
Otra opción es dejar el listado de hosts "limpio" y definir las variables en otra sección:
[atlanta] host1 host2 [atlanta:vars] ntp_server=ntp.atlanta.example.com proxy=proxy.atlanta.example.com
También es posible tener el fichero de inventario de hosts/grupos en el fichero principal y meter las variables de grupos en:
vi /etc/ansible/group_vars/NOMBRE_GRUPO ntp_server: acme.example.org database_server: storage.example.org
Ansible dispone de una gran variedad de módulos, algunos de ellos son:
ansible all -m user -a "name=foo password=<crypted password here>" ansible all -m user -a "name=foo state=absent"
ansible webservers -m git -a "repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD"
ansible webservers -m service -a "name=httpd state=started" ansible webservers -m service -a "name=httpd state=restarted" ansible webservers -m service -a "name=httpd state=stopped"
Los facts son variables que se pueden utilizar para tomar ciertas decisiones en la ejecución del playbook.
Podemos consultar los facts con:
ansible all -m setup
Obtenemos el fact ansible_distribution y donde machee ejecutar X:
vi playbook.yml # talk to all hosts just so we can learn about them - hosts: all tasks: - group_by: key=os_{{ ansible_distribution }} - name: Gentoos stuff hosts: os_Gentoo tasks: - name: install nano portage: package=app-editors/nano state=present - name: Ubuntuss stuff hosts: os_Ubuntu tasks: - name: install nano apt: name=nano state=present
Ejecutamos el playbook:
ansible-playbook playbook.yml
Podemos debugear ejecutando paso a paso el playbook:
ansible-playbook playbook.yml --step
También es posible ejecutar un playbook en un único host, en el playbook indicaremos - hosts: all y filtraremos con un argumento extra, --limit:
ansible-playbook --limit SERVERNAME playbook.yml