AAtsushi's Blog
DevOps

Ansible Basic Notes

→ 日本語版を読む

Overview

I read the Ansible Practical Guide 3rd Edition and am noting only the very basics here. These are rough notes, so please re-read the book if you need to verify accuracy.

By the way, the Ansible Practical Guide 3rd Edition was available on Kindle Unlimited as of April 2023.

What is Ansible?

  • A configuration management tool (can automatically install software on remote machines, etc.)
  • Some tools (Puppet, Chef) use an agent-based Pull model where agent software installed on the machine pulls configuration from a configuration management server, but Ansible uses an agentless Push-based approach
  • Specifically, the Ansible control server connects to remote machines via SSH and executes Python to perform installations and configuration
  • Therefore, Ansible requires that remote machines are accessible via SSH and that Python is available

Installing Ansible

Since I am using Windows, I work in the WSL Ubuntu distribution. If Python and PIP are already installed, you can install Ansible with just the following command.

# pip install ansible

Creating a Remote Machine

Create a remote machine on AWS or Azure. In this case, I created the remote machine on Azure.

Follow the steps below to get to the point where you can connect via SSH.

https://www.furukawa.co.jp/fitelnet/product/setting/pdf/azure-3_virtual-machine.pdf

Ansible Commands

The command to run operations on remote machines with Ansible is as follows.

# ansible-playbook -i invenory.ini playbook.yml

inventory.ini is the inventory file, which contains a list of IP addresses or domain names of remote machines.

[azure]
xxx.xxx.xxx.xxx

[azure:vars]
ansible_user=azureuser
ansible_ssh_private_key_file=~/ansible.pem

[all:vars]
ansible_port=22

The [azure] section is called a "group" and lists the IP addresses and domain names of remote machines. By referencing this in the playbook file described below, you specify the target remote machines.

[azure:vars] is called group variables and defines variables that can be shared across the host group. Group variables are defined in a section named [group-name:vars]. Variables can be user-defined or predefined "special variables." ansible_user and ansible_ssh_private_key_file are special variables that specify the SSH connection username and the path to the SSH private key file, respectively.

For other special variables, refer to the official documentation.

The all in [all:vars] is a special group that is implicitly defined and does not need to be declared. Variables listed under [all:vars] apply to all hosts in the inventory file.

playbook.yml is the playbook file and describes the tasks to be executed on remote machines.

---
 - hosts: azure
   vars:
     message: Hello Japan
   tasks:
   - name: Command test
     command:
       cmd: 'echo {{ message }}'
     register: command_return
   - name: Show command result
     debug:
       msg: "{{ command_return.stdout }}"
 - hosts: azure
   tasks:
   - name: Command test2
     command:
       cmd: 'echo End the processing'
 - hosts: azure
   tasks:
   - name: Install jinja2
     pip:
       name: jinja2

The --- on the first line is the symbol indicating the start of a YAML file and can be omitted.

The file consists of multiple plays. Each play contains a hosts section, a vars section, a tasks section, and a handlers section. In the example above, the handlers section is not included.

The hosts section specifies the group defined in the inventory file. The vars section allows you to define variables, which can be used within the play in the form {{ variable_name }}. The tasks section specifies modules. Modules include command for executing commands and file for file operations. For commonly used modules, refer to the following.

Summary of frequently used Ansible modules - Qiita

How Ansible Works

Based on the inventory file and the playbook file, Ansible generates Python execution code, transfers the file to the remote machine via SFTP, executes the Python code on the remote machine and retrieves the result, and finally deletes the Python execution code.

That's all.