18/03/2018 - ANSIBLE
In this example we are going to run Ansible provisioning on our local server to install "tree" package on remote server over SSH.
I assume that you already have SSH keys set up on your local OS. In order to connect to remote host over SSH, we need to add our local server's public key ~/.ssh/id_rsa.pub
to remote server's ~/.ssh/authorized_keys
file.
Check to see if we have SSH public key on our local server. We are interested in id_rsa.pub
file.
local-user:local-server$ ls -l ~/.ssh/
-rw------- 1 inanzzz staff 3243 20 Mar 2017 id_rsa
-rw-r--r-- 1 inanzzz staff 748 20 Mar 2017 id_rsa.pub
-rw------- 1 inanzzz staff 5176 17 Mar 13:40 known_hosts
Check to see if remote server has authorized_keys
file.
remote-user:remote-server$ ls -l ~/.ssh/
-rw------- 1 inanzzz staff 5176 17 Mar 13:40 authorized_keys
Obtain the content of public key id_rsa.pub
from local server and add it into authorized_keys
file on the remote server.
local-user:local-server$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1y++SDsT1xc9+L2Q # This is actually longer
remote-user:remote-server$ echo "ssh-rsa AAAAB3NzaC1y++SDsT1xc9+L2Q" >> ~/.ssh/authorized_keys
We are going to connect to remote server from local server by using remote user's name and remote server's IP address.
local-user:local-server$ ssh remote-user@192.168.99.31
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-92-generic x86_64)
remote-user@linux:~$
all:
hosts:
remote:
ansible_connection: ssh
ansible_user: remote-user
ansible_host: 192.168.99.31
ansible_port: 22
---
# This playbook sets up whole stack.
- name: Configurations to "remote" host
hosts: remote
remote_user: remote-user
become: yes
tasks:
- name: Install tree
apt:
name: tree
state: present
update_cache: yes
tags:
- tree
local-user:local-server$ ansible-playbook site.yml -i hosts.yml -vvvv
PLAYBOOK: site.yml ************************************************************
1 plays in site.yml
PLAY [Configurations to "remote" host] ****************************************
TASK [Gathering Facts] ********************************************************
ok: [remote]
META: ran handlers
TASK [Install tree] ***********************************************************
META: ran handlers
META: ran handlers
PLAY RECAP ********************************************************************
remote : ok=2 changed=1 unreachable=0 failed=0
remote-user:remote-server$ tree --version
tree v1.7.0