ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub username@host
# in case you cannot do the pubkey auth, do this and it may help:
chmod g-w /home/your_user
chmod 700 /home/your_user/.ssh
chmod 600 /home/your_user/.ssh/authorized_keys
Ansible Inventory Configuration
in /etc/ansible, edit hosts :
[apache] # it is a group name of these servers
# you can specify the user and ssh port number of this server
# Inventory Parameters
# ansible_connection=ssh/winrm/local
# ansible_port=22
# ansible_user=root/administrator
# ansible_ssh_pass=password
apache2 ansible_host=syrus@192.168.1.108 ansible_port=22
*Try to ping the remote server
# you can ping the group of server by using the group name
# you can also ping all the host by using all option
# ansible all -m ping
# you can specify a different file for hosts by using -i <path/file>
$ ansible apache -m ping
apache2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
SUCCESS means we can ping the remote server.
*Check date of all remote servers
$ ansible all -i hosts -m "command" -a "date"
apache2 | CHANGED | rc=0 >>
Wed May 29 18:06:47 UTC 2019
Writing Ansible Playbook
let's take an easy one for writing the playbook!
Create a .yml file in /etc/ansible first. For example, I am going to create a apache.ymlfile:
- hosts: apache # the group for excuting the commands.
become: true # use sudo
tasks:
- name: install apache2
apt: name=apache2 update_cache=yes state=latest
#update_cache stands for sudo apt-get update
Run the .yml file
ansible-playbook apache.yml --ask-become-pass
# --ask-become-pass stands for using sudo
# type host password
BECOME password:
PLAY [apache] *******************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************
ok: [apache2]
TASK [install apache2] **********************************************************************************************
[WARNING]: Updating cache and auto-installing missing dependency: python-apt
[WARNING]: Could not find aptitude. Using apt-get instead
ok: [apache2]
PLAY RECAP **********************************************************************************************************
apache2 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0