Ansible:
References:
https://sysadmincasts.com/episodes/43-19-minutes-with-ansible-part-1-4
https://sysadmincasts.com/episodes/45-learning-ansible-with-vagrant-part-2-4
Installation:
sudo apt-get update
sudo apt-get install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get install ansible
Ref:
http://docs.ansible.com/ansible/latest/intro_installation.html#latest-releases-via-apt-ubuntu
Version check:
cloud@vm1:~$ ansible --version
ansible 2.4.2.0
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/home/cloud/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4]
cloud@vm1:~$
Configuration:
/etc/ansible/ansible.cfg
[defaults]
inventory = /etc/ansible/hosts
/etc/ansible/hosts
[web]
10.0.1.5
10.0.1.4
Testing :
Run ansible ping command to check the SSH Reachability of the hosts:
ansible [all | host| group] -m ping --user <remote user> --private-key <keyfile> --ask-pass<password>
cloud@vm1:~$ ansible web -m ping --private-key mykey.private
10.0.1.4 | SUCCESS => {
"changed": false,
"ping": "pong"
}
10.0.1.5 | SUCCESS => {
"changed": false,
"ping": "pong"
}
cloud@vm1:~$
Simple playbook execution:
Ref: https://github.com/jweissig/episode-45
This playbook install a public key in the remote machine. The public key is located in /home/cloud/.ssh/id_rsa.pub file. (if you dont have, create one using "ssh-keygen -t rsa -b 2048" command.
Playbook file:
---
- hosts: all
sudo: yes
gather_facts: no
remote_user: cloud
tasks:
- name: install ssh key
authorized_key: user=cloud
key="{{ lookup('file', '/home/cloud/.ssh/id_rsa.pub') }}"
state=present
Running the command:
ansible-playbook examples/e45-ssh-addkey.yml --private-key mykey.private
Log:
cloud@vm1:~$ ansible-playbook examples/e45-ssh-addkey.yml --private-key mykey.private
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo' (default). This
feature will be removed in version 2.6. Deprecation warnings can be disabled by setting deprecation_warnings=False in
ansible.cfg.
PLAY [all] ********************************************************************************************************************
TASK [install ssh key] ********************************************************************************************************
changed: [10.0.1.5]
changed: [10.0.1.4]
PLAY RECAP ********************************************************************************************************************
10.0.1.4 : ok=1 changed=1 unreachable=0 failed=0
10.0.1.5 : ok=1 changed=1 unreachable=0 failed=0