Notice
Recent Posts
Recent Comments
Link
«   2026/04   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30
Archives
Today
Total
관리 메뉴

제니 블로그

Using Ansible 본문

Building Blocks

Using Ansible

jennystar 2025. 5. 12. 14:22

We installed Ansible on the previous post. Now it's time to use it in real application. 

I have currently set up a new inventory file in YAML format is usually the preferred way of setting up an inventory file. 

# /home/user/ansible/inventory.yml
# Organized in YAML format

all:
  vars:
    ansible_user: user
    ansible_ssh_private_key_file: ~/.ssh/id_rsa 
    # Define the python interpreter
    ansible_python_interpreter: /usr/bin/python3

  children:
    # Group for test servers
    test-servers:
      hosts:
        192.168.0.1:
        192.168.0.2:
        192.168.0.3:
        192.168.0.4:
        192.168.0.5:
        192.168.0.6:
        # Can add things multiple variables for a certain host, like go_version:v1.23.7..
      

    # Group for production servers
    production:
      hosts:
        192.168.0.7:
        192.168.0.8:
        192.168.0.9:
        192.168.0.10:
        192.168.0.11:
        192.168.0.12:

 

The above format contains what is called the children group, which can be defined as a subgroup. It enables an hierachical organization and variable inheritence based on that hierachy. 

 

The child group inherit properties from the parent group. The `test-servers` and `production` is an example of the child group. 

The `all` part is the parent group, which is a built-in group that includes every managed host and serves as the global variable inheritance. 

 

Ok now let's make a very simple script that checks for uptime on each test-server host. 

# File: /home/user/ansible/check_uptime.yml

---
# This playbook checks the uptime of the servers in the 'test-servers' group.
- name: Check uptime of test servers
  hosts: test-servers
  gather_facts: no

  tasks:
    # Use the 'command' module to run the 'uptime' command on each host.
    - name: Get server uptime
      ansible.builtin.command: uptime
      register: uptime_output # Store the command output in a variable

    # Display the output of the uptime command for each host.
    - name: Display uptime
      ansible.builtin.debug:
        msg: "Uptime for {{ inventory_hostname }}: {{ uptime_output.stdout }}"

 

The result would look like : 

Checking for uptime isn't very useful for Ansible, but it was a simple example on how to use Ansible! 

'Building Blocks' 카테고리의 다른 글

Setting up SSH Configurations  (0) 2025.05.26
Creating a Partition + Using LVM  (0) 2025.05.26
Installing Ansible  (0) 2025.05.12
Solving Leetcode : Longest common prefix  (0) 2023.03.15
Using .gitignore  (0) 2023.03.09