# Install Ansible di CentOS 7

<figure><img src="https://nos.wjv-1.neo.id/cdn.medusa.my.id/Automation.png" alt=""><figcaption></figcaption></figure>

## Introduction

[**Ansible**](https://www.ansible.com) adalah sebuah provisioning tool yang dikembangkan oleh [**RedHat**](https://www.redhat.com). Yang mana tool ini berfungsi untuk mencatat setiap proses deployment atau pun konfigurasi pada beberapa server secara bersamaan.

Berikut ini adalah contoh skenario dari penggunaan tool Ansible, yang digunakan untuk otomasi proses instalasi dan konfigurasi LAMP (Linux Apache MariaDB PHP), serta CMS WordPress pada CentOS 7 dan untuk artikelnya dibahas terpisah [**disini**](https://blog.madfxr.my.id/documentation/category/devops/automation-installation-lamp-dan-cms-wordpress-dengan-menggunakan-ansible).

## Scenario

* **123.45.67.87 (Master)**.
* **123.45.67.21 (Node 1)**.
* **123.45.67.31 (Node 2)**.
* **123.45.67.41 (Node 3)**.

## Installation

### Menambahkan Repository EPEL

Sebelum kita meng-install paket software Ansible pada **123.45.67.87 (Master)**, terlebih dahulu kita install repository paket software EPEL pada CentOS 7, berikut perintah yang digunakan:

{% code overflow="wrap" lineNumbers="true" %}

```bash
yum install epel-release -y
```

{% endcode %}

### System Update

Setelah repository paket software EPEL telah ter-install, langkah selanjutnya kita lakukan pembaruan pada sistem, berikut perintah yang digunakan:

{% code overflow="wrap" lineNumbers="true" %}

```bash
yum update -y
```

{% endcode %}

### Install Paket Software Ansible

Sebelum Ansible dapat digunakan di CentOS 7, terlebih dahulu kita harus meng-install paket software-nya, berikut perintah yang digunakan:

{% code overflow="wrap" lineNumbers="true" %}

```bash
yum install ansible -y
```

{% endcode %}

### Melihat Versi dari Ansible

Untuk melihat versi dari Ansible yang telah ter-install, berikut perintah yang digunakan:

{% code overflow="wrap" lineNumbers="true" %}

```bash
ansible --version
```

{% endcode %}

{% hint style="success" %}
{% code title="Output" %}

```bash
ansible 2.6.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Jul 13 2018, 13:06:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
```

{% endcode %}
{% endhint %}

### Melakukan Generate SSH Key

Sebelum menjalankan Ansible untuk otomasi proses instalasi dan konfigurasi LAMP (Linux Apache MariaDB PHP), serta CMS WordPress pada CentOS 7. Terlebih dahulu kita harus melakukan generate SSH Key, hal ini dilakukan untuk menghasilkan Private Key dan Public Key yang nantinya akan digunakan sebagai autentikasi ketika terjadi komunikasi antar Master dengan Node, berikut perintah yang digunakan:

{% code overflow="wrap" lineNumbers="true" %}

```bash
ssh-keygen -t rsa -b 4096
```

{% endcode %}

{% hint style="success" %}
{% code title="Output" %}

```bash
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:NHEVLUATliGY19iuIYP1DkIB21QkE4wvY5PlWUeBdgs root@ansible.localhost
The key's randomart image is:
+---[RSA 4096]----+
|  .+*=o==*O*oo   |
|  .++oE ==+.. .  |
|  .*.* *oo   .   |
|  * * +.+..      |
| . + . =So       |
|    	o         |
|                 |
|                 |
|                 |
+----[SHA256]-----+
```

{% endcode %}
{% endhint %}

{% hint style="info" %}

```markdown
-t = Spesifikasi tipe autentikasi untuk file Private Key dan Public Key dari SSH yang dibuat
rsa = Tipe autentikasi keamanan komunikasi data yang digunakan oleh SSH
-b = Spesifikasi nilai atau angka untuk ukuran file Private Key dan Public Key dari SSH yang dibuat
4096 = Ukuran file Private Key dan Public Key dari SSH yakni 4096 bit
```

{% endhint %}

### Autentikasi Akses SSH ke Setiap Node

Proses selanjutnya yakni melakukan autentikasi login pada setiap Node, hal ini dilakukan agar Master yang terpasang oleh Ansible dapat menjalankan perintah secara otomatis pada setiap Node tanpa memerlukan autentikasi password lagi, berikut perintah yang digunakan:

{% code overflow="wrap" lineNumbers="true" %}

```bash
ssh-copy-id root@123.45.67.21
ssh-copy-id root@123.45.67.31
ssh-copy-id root@123.45.67.41
```

{% endcode %}

{% hint style="success" %}
{% code title="123.45.67.21 (Node 1)" %}

```bash
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '123.45.67.21 (123.45.67.21)' can't be established.
ECDSA key fingerprint is SHA256:8ZhrtOBj9Ei+y+gTQbqg0p1t4YwVHw+I0VB9qhONNjA.
ECDSA key fingerprint is MD5:93:be:a2:b2:31:ac:fb:89:32:d3:79:35:b4:7b:9a:74.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@123.45.67.21's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@123.45.67.21'"
and check to make sure that only the key(s) you wanted were added.
```

{% endcode %}
{% endhint %}

{% hint style="success" %}
{% code title="123.45.67.31 (Node 2)" %}

```bash
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '123.45.67.31 (123.45.67.31)' can't be established.
ECDSA key fingerprint is SHA256:5wLAxUbZ5Nt5/98jAaGlwfUrEkX3wknCo9ndSwkoW3U.
ECDSA key fingerprint is MD5:48:c3:d5:3e:10:11:62:94:88:85:05:dd:d5:31:a6:1c.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@123.45.67.31's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@123.45.67.31'"
and check to make sure that only the key(s) you wanted were added.
```

{% endcode %}
{% endhint %}

{% hint style="success" %}
{% code title="123.45.67.41 (Node 3)" %}

```bash
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '123.45.67.41 (123.45.67.41)' can't be established.
ECDSA key fingerprint is SHA256:PfPAuId7qvbvSjvBjcgcQ1ggYIANqHWXpIndH0E/wfo.
ECDSA key fingerprint is MD5:e2:be:d5:98:c3:77:56:c8:24:b5:c1:e8:cd:a1:94:93.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@123.45.67.41's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@123.45.67.41'"
and check to make sure that only the key(s) you wanted were added.
```

{% endcode %}
{% endhint %}

### Menambahkan List Host untuk Setiap Node

Melakukan konfigurasi pada file hosts dari Ansible, hal ini dilakukan agar Master dapat mengenali IP Address dari setiap Node dan dapat berkomunikasi satu sama lain, berikut perintah yang digunakan:

{% code overflow="wrap" lineNumbers="true" %}

```bash
vi /etc/ansible/hosts
```

{% endcode %}

{% hint style="warning" %}
{% code title="/etc/ansible/hosts" %}

```markdown
[ansiblehost]
123.45.67.21
123.45.67.31
123.45.67.41
```

{% endcode %}
{% endhint %}

Untuk menyimpan konfigurasi file hosts tekan tombol **Esc**, kemudian tekan kombinasi huruf **`:wq`**.

## Testing

### Verifikasi Host

Untuk melakukan pengecekan koneksi pada setiap Node dari Master, berikut perintah yang digunakan:

{% code overflow="wrap" lineNumbers="true" %}

```bash
ansible -m ping ansiblehost
```

{% endcode %}

{% hint style="success" %}
{% code title="Output" %}

```bash
123.45.67.21 | SUCCESS => {
	"changed": false,
	"ping": "pong"
}
123.45.67.31 | SUCCESS => {
	"changed": false,
	"ping": "pong"
}
123.45.67.41 | SUCCESS => {
	"changed": false,
	"ping": "pong"
}
```

{% endcode %}
{% endhint %}

{% hint style="info" %}

```markdown
-m = Opsi yang digunakan untuk menjalankan modul dari Ansible
ping = Nama modul yang digunakan untuk melakukan pengecekan koneksi
ansiblehost = Nama grup host dari yang digunakan untuk setiap Node
```

{% endhint %}

Demikian sedikit pengetahuan dan pengalaman yang dapat saya bagikan, semoga apa yang telah saya sampaikan dapat bermanfaat bagi kita semua.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.madfxr.my.id/documentation/categories/devops/install-ansible-di-centos-7.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
