Skip to content

Instantly share code, notes, and snippets.

@algotrader-dotcom
Last active October 21, 2021 23:08
Show Gist options
  • Select an option

  • Save algotrader-dotcom/a66e9736217c5843a12c to your computer and use it in GitHub Desktop.

Select an option

Save algotrader-dotcom/a66e9736217c5843a12c to your computer and use it in GitHub Desktop.
Ansible Kickstart
##What's 'Ansible' ?
Ansible is configuration management tool like SaltStack, Puppet but it works through SSH, no need to install agent on managed nodes.
##Why's Ansible ?
As my point, i love ansible for:
- No need to install agent on agent nodes
##How's Ansible?
### Install Ansible on master node
#####Red Hat/CentOS/Fedora
```
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum install ansible -y
```
#####Ubuntu/Debian
```
apt-get install python-software-properties
apt-add-repository ppa:ansible/ansible
apt-get update
apt-get install ansible
```
#####Setup ansible
All nodes (server) managed throush file `/etc/ansible/hosts`
#####My ansible lab for 2 nodes:
**OS :**| Ubuntu 14.04
---|-----
**eth0:**| 192.168.1.73
**SSH Port:**| 2246
**SSH Pass:**| Ubuntu121
**OS:**| CentOS 6.5
---|-----
**eth0:**| 192.168.1.74
**SSH Port:**| 5918
**SSH Pass:**| Centos313
File `/etc/ansible/hosts`
```
[ansibleLab]
192.168.1.73 ansible_ssh_port=2246 ansible_ssh_user=ansible ansible_ssh_pass=Ubuntu121
192.168.1.74 ansible_ssh_port=5918 ansible_ssh_user=root ansible_ssh_pass=Centos313
```
#####Check if it works
```
ansible ansibleLab -m ping
```
```
192.168.1.73 | success >> {
"changed": false,
"ping": "pong"
}
192.168.1.74 | success >> {
"changed": false,
"ping": "pong"
}
```
#####Get distro information
```
ansible ansibleLab -m setup -a 'filter=ansible_distribution'`
```
```
192.168.1.74 | success >> {
"ansible_facts": {
"ansible_distribution": "CentOS"
},
"changed": false
}
192.168.1.73 | success >> {
"ansible_facts": {
"ansible_distribution": "Ubuntu"
},
"changed": false
}
```
###Ansible playbook
Ansible playbook is configuration for a node (packages installed, service, cron...)
Below is a playbook install apache on managed nodes.
`vim /srv/playbook.yml`
```
---
- hosts: ansibleLab
gather_facts: yes
tasks:
- name: Install apache Ubuntu
apt: name=apache2 state=present
when: ansible_os_family == "Debian"
- name: Install apache CentOS
yum: name=httpd state=present
when: ansible_os_family == "RedHat"
- name: Start apache2
service: name=apache2 state=started enabled=yes
when: ansible_os_family == "Debian"
- name: Start httpd
service: name=httpd state=started enabled=yes
when: ansible_os_family == "RedHat"
```
##### Execute playbook
```
ansible-playbook /srv/playbook.yml -v
```
```
PLAY RECAP ********************************************************************
192.168.1.73 : ok=3 changed=1 unreachable=0 failed=0
192.168.1.74 : ok=3 changed=2 unreachable=0 failed=0
```
Để ý trường `failed=0` là okie. Lần lượt vào 2 host để kiểm tra bằng lệnh `netstat -nltp`
##TroubleShooting
```
192.168.5.119 | FAILED => Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host.
192.168.5.149 | FAILED => Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host.
```
Sửa file `/etc/ansible/ansible.cfg`
```
[defaults]
host_key_checking = False
```
Trên đây chỉ là 1 số thông tin rất cơ bản về ansible, các bạn có thể vào link http://docs.ansible.com/ tìm hiểu thêm về ansible để phát triển theo ý muốn.
#References
https://valdhaus.co/writings/ansible-post-install/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment