Getting started with Infrastructure as Code using Ansible (Practical Guide)

table of contents
Hello everyone,
the System Solutions Department
I'm Okazaki from the SRE team in
Last time, I wrote about introducing the Ansible tool and how to install it, so this time I'd like to introduce how to actually run Ansible.
Ansible runtime configuration
The following is what you need to run Ansible:
- hosts file (inventory file)
- playbook
- Module
We will explain each one
What is a hosts file (inventory file)?
The hosts file (inventory file) is a file used to list the hosts to be worked on.
An example of how to fill it out is shown below.
[all] XXX.XXX.XXX.XXX XXX.XXX.XXX.XXX [web] XXX.XXX.XXX.XXX [db] XXX.XXX.XXX.XXX
You can specify the group of hosts to execute the commands on within each of the brackets [].
Save the above with any name, and when running Ansible, specify the saved file with the -i option, and each command will be executed on the specified hosts.
What is a playbook?
This file contains the commands and workflow you want to execute on the target host.
Below is a playbook for installing Apache.
--- # Main Play operation playbook - name: apply common configuration to all nodes hosts: all remote_user: [username] sudo: yes tasks: - name: apache-install yum: name=httpd state=present
The - name part indicates the beginning of this process, and the name part is the name of this process
The `hosts` section can be used to specify the group described above.
In this case, we've specified `[all]` to represent all hosts.
The `remote_user` part specifies the user on the target host.
If you do not specify a user that exists on the remote destination, the command will not be executed and an error will occur.
The `sudo` part specifies whether to execute the command with root privileges using `remote_user`.
In this case, root privileges are required because installation is necessary, so we set it to `yes`.
The part following "task" is the command that will actually be executed.
In this case, it's the command to install Apache using the yum command.
What is a module?
In the playbook section we just looked at, you saw a command section; that section consists of individual modules.
We introduced modules as one of Ansible's key features last time, and they are currently being developed in various languages.
They handle not only server configuration but also cloud platform configuration.
For details, please see the link below for a list of actual modules.
Link:https://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html
Let's actually try it out!!
Now, let's finally try running Ansible.
First, we'll add the target host to the hosts file.
# vi hosts ======================== [all] XXX.XXX.XXX.XXX ========================
Next, we will create the main playbook
# vi operation.yml ========================= --- # Main Play operation playbook - name: apply common configuration to all nodes hosts: all remote_user: ec2-user sudo: yes tasks: - name: apache-install yum: name=httpd state=present ========================
Finally, the ansible-playbook command is used to execute the desired commands on the target host.
*The SSH private key for logging in is specified using --private-key=.
# ansible-playbook --private-key=key/id_rsa -i hosts operation.yml PLAY [apply common configuration to all nodes] ************************************************ TASK [Gathering Facts] ***************************************************************** ok: [XXX.XXX.XXX.XXX] TASK [apache-install] **************************************************************** changed: [XXX.XXX.XXX.XXX] PLAY RECAP **************************************************************** XXX.XXX.XXX.XXX : ok=2 changed=1 unreachable=0 failed=0
I will check if it is installed
$ rpm -qa | grep httpd httpd-tools-2.2.34-1.16.amzn1.x86_64 httpd-2.2.34-1.16.amzn1.x86_64
The installation was successful!!!
summary
This time, I wrote about how to run Ansible.
Next time, I hope to write about how to set roles and other related topics.
I hope this will continue to pique your interest in Ansible and operational/construction automation.
That's all for now.
1
