0% found this document useful (0 votes)
11 views11 pages

Ansible Setup for Server 192.168.8.138

The document provides a comprehensive guide for setting up Ansible on Debian servers, including SSH server preparation, repository configuration, and inventory setup. It details various Ansible playbooks for tasks such as checking system information, installing Apache, and replacing the index page with a dynamic template. Additionally, it includes commands for testing connectivity and system status checks across multiple servers.

Uploaded by

santosadidik543
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views11 pages

Ansible Setup for Server 192.168.8.138

The document provides a comprehensive guide for setting up Ansible on Debian servers, including SSH server preparation, repository configuration, and inventory setup. It details various Ansible playbooks for tasks such as checking system information, installing Apache, and replacing the index page with a dynamic template. Additionally, it includes commands for testing connectivity and system status checks across multiple servers.

Uploaded by

santosadidik543
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ANSIBLE BASIC

Persiapan SSH Server

Server1 dan Server2


Permit Root Login SSH

[Link] /etc/ssh/sshd_config
PermitRootLogin yes

siapkan repository

disable repo cdrom

[Link] /etc/apt/[Link]

deb [Link] bookworm main non-


free-firmware
deb-src [Link] bookworm main
non-free-firmware

deb [Link] bookworm-security


main non-free-firmware
deb-src [Link] bookworm-
security main non-free-firmware

# bookworm-updates, to get updates before a point release is made;

# see
[Link]
updates_and_backports
deb [Link] bookworm-updates
main non-free-firmware
deb-src [Link] bookworm-
updates main non-free-firmware

apt update

Reboot server1 dan server2

Node Control:

ssh-keygen

ssh-copy-id root@[Link]
ssh root@[Link]

ssh-copy-id root@[Link]
ssh root@[Link]
Setup Inventory

vim [Link]

[server]
server1 ansible_host=[Link] ansible_user=root
server2 ansible_host=[Link] ansible_user=root

Test dan cek koneksi


budi@tusirah:~/workflow/ansible-basic$ ansible all -i
[Link] -m ping
server2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
server1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}

Cek OS dan Sistem


budi@tusirah:~/workflow/ansible-basic$ ansible all -i
[Link] -m command -a "uname -a"
server1 | CHANGED | rc=0 >>
Linux test1 6.1.0-32-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.129-1
(2025-03-06) x86_64 GNU/Linux
server2 | CHANGED | rc=0 >>
Linux test2 6.1.0-32-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.129-1
(2025-03-06) x86_64 GNU/Linux

budi@tusirah:~/workflow/ansible-basic$ ansible all -i


[Link] -m command -a "whoami"
server1 | CHANGED | rc=0 >>
root
server2 | CHANGED | rc=0 >>
root

budi@tusirah:~/workflow/ansible-basic$ ansible all -i


[Link] -m command -a "ip a"
server2 | CHANGED | rc=0 >>
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state
UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet [Link]/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host noprefixroute
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc
fq_codel state UP group default qlen 1000
link/ether 08:00:27:76:de:d5 brd ff:ff:ff:ff:ff:ff
inet [Link]/24 brd [Link] scope global dynamic
enp0s3
valid_lft 84965sec preferred_lft 84965sec
inet6 fd14:7740:664d:1c00:a00:27ff:fe76:ded5/64 scope global
dynamic mngtmpaddr
valid_lft 6782sec preferred_lft 3182sec
inet6 fe80::a00:27ff:fe76:ded5/64 scope link
valid_lft forever preferred_lft forever
server1 | CHANGED | rc=0 >>
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state
UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet [Link]/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host noprefixroute
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc
fq_codel state UP group default qlen 1000
link/ether 08:00:27:e0:b7:7c brd ff:ff:ff:ff:ff:ff
inet [Link]/24 brd [Link] scope global dynamic
enp0s3
valid_lft 84832sec preferred_lft 84832sec
inet6 fd14:7740:664d:1c00:a00:27ff:fee0:b77c/64 scope global
dynamic mngtmpaddr
valid_lft 6782sec preferred_lft 3182sec
inet6 fe80::a00:27ff:fee0:b77c/64 scope link
valid_lft forever preferred_lft forever

budi@tusirah:~/workflow/ansible-basic$ ansible all -i


[Link] -m command -a "free"
server2 | CHANGED | rc=0 >>
total used free shared
buff/cache available
Mem: 2014496 225552 1812088 544
104636 1788944
Swap: 998396 0 998396
server1 | CHANGED | rc=0 >>
total used free shared
buff/cache available
Mem: 2014496 226128 1810488 528
106672 1788368
Swap: 998396 0 998396
budi@tusirah:~/workflow/ansible-basic$

budi@tusirah:~/workflow/ansible-basic$ ansible all -i


[Link] -m command -a "hostname"
server2 | CHANGED | rc=0 >>
test2
server1 | CHANGED | rc=0 >>
test1

Ansible Playbook

vim [Link]
- name: Test Ping and Uname
hosts: all
gather_facts: no
tasks:
- name: Ping target hosts
[Link]:

- name: Get system information


[Link]: uname -a
register: uname_output

- name: Show system information


[Link]:
msg: "{{ uname_output.stdout }}"

budi@tusirah:~/workflow/ansible-basic$ ansible-playbook -i
[Link] [Link]

PLAY [Test Ping and Uname]


******************************************************************
******************************************************************
*****************************

TASK [Ping target hosts]


******************************************************************
******************************************************************
*******************************
ok: [server1]
ok: [server2]

TASK [Get system information]


******************************************************************
******************************************************************
**************************
changed: [server2]
changed: [server1]
TASK [Show system information]
******************************************************************
******************************************************************
*************************
ok: [server1] => {
"msg": "Linux test1 6.1.0-32-amd64 #1 SMP PREEMPT_DYNAMIC
Debian 6.1.129-1 (2025-03-06) x86_64 GNU/Linux"
}
ok: [server2] => {
"msg": "Linux test2 6.1.0-32-amd64 #1 SMP PREEMPT_DYNAMIC
Debian 6.1.129-1 (2025-03-06) x86_64 GNU/Linux"
}

PLAY RECAP
******************************************************************
******************************************************************
*********************************************
server1 : ok=3 changed=1 unreachable=0
failed=0 skipped=0 rescued=0 ignored=0
server2 : ok=3 changed=1 unreachable=0
failed=0 skipped=0 rescued=0 ignored=0

vim check_system.yml
- name: Cek Sistem Server
hosts: all
gather_facts: yes
tasks:

- name: Tampilkan Hostname


debug:
msg: "Hostname: {{ ansible_hostname }}"

- name: Tampilkan OS & Kernel


debug:
msg: "OS: {{ ansible_distribution }}
{{ ansible_distribution_version }} | Kernel: {{ ansible_kernel }}"

- name: Tampilkan Waktu Aktif (Uptime)


command: uptime -p
register: uptime_result

- name: Uptime Server


debug:
msg: "Uptime: {{ uptime_result.stdout }}"

- name: Tampilkan Penggunaan CPU & RAM


shell: "top -bn1 | grep 'Cpu\\|Mem' | sed 's/^ //g'"
register: cpu_mem_usage

- name: CPU & RAM Usage


debug:
msg: "{{ cpu_mem_usage.stdout_lines }}"

- name: Cek Penggunaan Disk


shell: df -h | awk '{print $5 \" \" $6}' | sed 's/%//g' |
tail -n +2
register: disk_usage

- name: Disk Usage


debug:
msg: "{{ disk_usage.stdout_lines }}"

- name: Cek Status Service (nginx, mysql, apache2)


systemd:
name: "{{ item }}"
state: started
register: service_status
with_items:
- nginx
- mysql
- apache2
ignore_errors: yes

- name: Status Service


debug:
msg: "{{ [Link] }}: {{ 'RUNNING' if [Link] ==
false else 'STOPPED' }}"
loop: "{{ service_status.results }}"

budi@tusirah:~/workflow/ansible-basic$ ansible-playbook -i
[Link] check_system.yml

Apache Install
vim apache_install.yml
---
- name: Install dan Konfigurasi Apache Web Server di Debian/Ubuntu
hosts: all
become: yes
tasks:

- name: Update package repository


apt:
update_cache: yes

- name: Install Apache


apt:
name: apache2
state: present

- name: Pastikan Apache berjalan dan aktif saat boot


systemd:
name: apache2
enabled: yes
state: started

- name: Buka firewall untuk HTTP (port 80)


ufw:
rule: allow
port: "80"
proto: tcp

- name: Salin halaman indeks default


copy:
dest: /var/www/html/[Link]
content: "<h1>Apache Web Server Berhasil Diinstal di
Debian/Ubuntu!</h1>"

- name: Restart Apache untuk menerapkan perubahan


systemd:
name: apache2
state: restarted

ansible-playbook -i [Link] apache_install.yml

PLAY [Install dan Konfigurasi Apache Web Server di Debian/Ubuntu]


******************************************************************
********************************************************

TASK [Gathering Facts]


******************************************************************
******************************************************************
*********************************
ok: [server1]
ok: [server2]

TASK [Update package repository]


******************************************************************
******************************************************************
***********************
ok: [server2]
changed: [server1]

TASK [Install Apache]


******************************************************************
******************************************************************
**********************************
changed: [server2]
changed: [server1]

TASK [Pastikan Apache berjalan dan aktif saat boot]


******************************************************************
******************************************************************
****
ok: [server2]
ok: [server1]

TASK [Buka firewall untuk HTTP (port 80)]


******************************************************************
******************************************************************
**************
fatal: [server2]: FAILED! => {"changed": false, "msg": "Failed to
find required executable \"ufw\" in paths:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}
fatal: [server1]: FAILED! => {"changed": false, "msg": "Failed to
find required executable \"ufw\" in paths:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}

PLAY RECAP
******************************************************************
******************************************************************
*********************************************
server1 : ok=4 changed=2 unreachable=0
failed=1 skipped=0 rescued=0 ignored=0
server2 : ok=4 changed=1 unreachable=0
failed=1 skipped=0 rescued=0 ignored=0

ansible all -i [Link] -m command -a "systemctl status


[Link]"

server2 | CHANGED | rc=0 >>


● [Link] - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/[Link]; enabled;
preset: enabled)
Active: active (running) since Fri 2025-03-21 14:27:17 WIB;
3min 9s ago
Docs: [Link]
Main PID: 2331 (apache2)
Tasks: 55 (limit: 2304)
Memory: 9.4M
CPU: 61ms
CGroup: /[Link]/[Link]
├─2331 /usr/sbin/apache2 -k start
├─2333 /usr/sbin/apache2 -k start
└─2334 /usr/sbin/apache2 -k start

Mar 21 14:27:17 test2 systemd[1]: Starting [Link] - The


Apache HTTP Server...
Mar 21 14:27:17 test2 apachectl[2330]: AH00558: apache2: Could not
reliably determine the server's fully qualified domain name, using
[Link]. Set the 'ServerName' directive globally to
suppress this message
Mar 21 14:27:17 test2 systemd[1]: Started [Link] - The
Apache HTTP Server.
server1 | CHANGED | rc=0 >>
● [Link] - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/[Link]; enabled;
preset: enabled)
Active: active (running) since Fri 2025-03-21 14:27:19 WIB;
3min 7s ago
Docs: [Link]
Main PID: 2377 (apache2)
Tasks: 55 (limit: 2304)
Memory: 9.4M
CPU: 57ms
CGroup: /[Link]/[Link]
├─2377 /usr/sbin/apache2 -k start
├─2379 /usr/sbin/apache2 -k start
└─2380 /usr/sbin/apache2 -k start

Mar 21 14:27:19 test1 systemd[1]: Starting [Link] - The


Apache HTTP Server...
Mar 21 14:27:19 test1 apachectl[2376]: AH00558: apache2: Could not
reliably determine the server's fully qualified domain name, using
[Link]. Set the 'ServerName' directive globally to
suppress this message
Mar 21 14:27:19 test1 systemd[1]: Started [Link] - The
Apache HTTP Server.

Replace Index Apache dengan Template

vim replace_index_apache.yml

---
- name: Ganti [Link] dengan template dinamis
hosts: all
become: yes
tasks:

- name: Pastikan Apache sudah terinstal


apt:
name: apache2
state: present

- name: Pastikan Apache berjalan


systemd:
name: apache2
enabled: yes
state: started

- name: Salin template [Link] ke server


template:
src: templates/[Link].j2
dest: /var/www/html/[Link]
owner: www-data
group: www-data
mode: "0644"

- name: Restart Apache untuk menerapkan perubahan


systemd:
name: apache2
state: restarted

mkdir templates
vim templates/[Link].j2

<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Apache Web Server</title>
</head>
<body>
<h1>Hallo dari {{ ansible_hostname }} !</h1>
</body>
</html>

ansible-playbook -i [Link] replace_index_apache.yml

PLAY [Ganti [Link] dengan template dinamis]


******************************************************************
******************************************************************
********

TASK [Gathering Facts]


******************************************************************
******************************************************************
*********************************
ok: [server1]
ok: [server2]

TASK [Pastikan Apache sudah terinstal]


******************************************************************
******************************************************************
*****************
ok: [server2]
ok: [server1]

TASK [Pastikan Apache berjalan]


******************************************************************
******************************************************************
************************
ok: [server1]
ok: [server2]

TASK [Salin template [Link] ke server]


******************************************************************
******************************************************************
*************
changed: [server1]
changed: [server2]

TASK [Restart Apache untuk menerapkan perubahan]


******************************************************************
******************************************************************
*******
changed: [server2]
changed: [server1]

PLAY RECAP
******************************************************************
******************************************************************
*********************************************
server1 : ok=5 changed=2 unreachable=0
failed=0 skipped=0 rescued=0 ignored=0
server2 : ok=5 changed=2 unreachable=0
failed=0 skipped=0 rescued=0 ignored=0

Common questions

Powered by AI

The 'apt' module in Ansible is used for managing Debian-based package installations and updates, enabling operations like installing or removing packages and updating repositories. It provides interface-level interactions with APT, facilitating package management in a straightforward manner. In contrast, the 'systemd' module manages services, leveraging capabilities of the systemd init system to control service states like start, stop, and enable. These modules are utilized to automate package management and service operations, respectively, ensuring software consistency and service availability .

Dynamic templates in Ansible playbooks, executed with the 'template' module, allow for on-the-fly customization of file content based on system-specific variables. They improve server management flexibility by providing a way to manage configurations through templated files—like index.html—that can incorporate host-specific data such as hostname or IP addresses. This eliminates the need for unique static configurations for each server, reducing manual effort and errors, and ensuring configurations remain consistent and adaptable to changes across different environments .

The ansible-command module enables users to run arbitrary commands on managed nodes, such as checking the status of a service using 'systemctl status apache2.service'. This is important for system diagnostics as it provides immediate feedback about the service's running status, including errors or warnings that may affect overall functionality. For instance, identifying issues like missing dependencies or configuration errors can expedite troubleshooting processes. Consistent monitoring through automated checks ensures systems remain operational and reduces downtime by swiftly addressing potential issues .

Logistical challenges of using Ansible for system backup include ensuring network reliability for playbook execution, managing differences in regional data retention requirements, and adapting playbooks to accommodate various system architectures. However, benefits outweigh challenges as Ansible automates and standardizes backup configurations across systems, thereby reducing human error and ensuring consistency in backup operations. It allows scaling processes to support hundreds of servers simultaneously, providing fault-tolerant solutions while simplifying disaster recovery through straightforward playbooks and version control .

SSH keys provide a more secure mechanism for logging into servers compared to passwords, which can be subject to guessing or brute force attacks. An SSH key-based authentication requires a pair of cryptographic keys: a private key that the user keeps secure, and a public key that is placed on the server. Only when these keys match does the login succeed, which prevents unauthorized access. This method is considered more secure because it avoids password transmission over potentially insecure networks and minimizes human error associated with password management .

Failing to open necessary firewall ports, such as HTTP port 80, for web server operations results in the web server being inaccessible to clients, effectively causing a service outage. This restriction disrupts application accessibility, impacting user experience and potentially leading to business losses. Mitigation strategies include ensuring that firewall management tools like 'ufw' are properly installed and configured. Automated scripts or configuration management tools should be employed to maintain consistent and secure firewall settings, accommodating legitimate traffic while blocking unauthorized access .

Setting up a new Debian APT repository involves editing the sources.list file, typically located at /etc/apt/sources.list. You need to specify new repository URLs that point to Debian package mirrors, allowing your system to fetch updated software packages. After adding the relevant entries, such as deb http://kartolo.sby.datautama.net.id/debian/ for the Debian distribution and its security updates, running 'apt update' will refresh the package list and make these updates available. Regularly updating repository sources ensures that systems can access the latest security patches and software versions, which is crucial for maintaining overall system security and performance .

To allow SSH access for the root user on a Linux server, the SSH configuration file found at /etc/ssh/sshd_config would need to include the line 'PermitRootLogin yes'. This change allows direct root access, which can be useful for administrative purposes but poses significant security risks as it grants extensive system control to anyone who breaches this line of defense. Consequently, it's recommended to disable root SSH access by default, and instead use sudo for administrative tasks when logged in as a non-root user, enhancing security by employing a principle of least privilege .

The 'gather_facts' option in Ansible playbooks automatically collects system information from managed nodes, including hardware profiles such as CPU, memory, and disk usage. Enabling fact gathering ensures accurate targeting of playbook actions, adapting tasks to specific server capabilities and conditions, such as adjusting resource allocation for intensive applications. It also aids compliance with infrastructure requirements by maintaining system configuration baselines. Disabling it might speed up playbook execution in scenarios where this data is unnecessary but risks potential misconfigurations if hardware profiles are not defined beforehand .

Ansible facilitates configuration management by allowing users to automate systems setup and repetitive IT tasks across multiple servers via ansible-playbooks. Ansible's agentless architecture utilizes SSH for connectivity, enabling operations without the need for additional software installation on client systems. Its YAML playbooks provide a human-readable framework, enhancing ease of use. Key advantages include its simplicity, scalability, and the ability to maintain consistent configurations across numerous systems, reducing overhead and human error .

You might also like