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

table of contents
Hello everyone,
I'm Okazaki, a member of the SRE team in the System Solutions Department
Last time I wrote about the Ansible tool and how to install it, so this time I would like to finally introduce how to 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 in which you enter the target hosts.
An example of how to enter them 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 execution hosts in each [] part.
Save the above with any name, and when executing Ansible, add the option -i and specify the saved file, and each command will be executed for the entered hosts.
What is a playbook?
This is a file where you can enter the commands you want to run on the target host and the workflow.
Below is a playbook to install 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
You can specify the group part explained above in the hosts section.
In this case, we specify [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 run the command with root privileges in remote_user.
Since installation is required in this case, root privileges are required, so we set it to yes.
The part below task is the command that will actually be executed.
In this case, it is the command to install Apache using the yum command.
What is a module?
You may have seen a command section in the playbook section above, and that section is each module.
Last time, we introduced modules as one of their features, but modules are currently being developed in a variety of languages, and
some not only configure the server itself, but also configure cloud platforms.
For more details, please see the link below to the 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 run Ansible.
First, enter the target host in 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, use the ansible-playbook command to execute the command you want to run on the target host.
*The SSH private key for logging in to the target is specified with --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 would like to write about how to set up roles, etc.
I hope that you will continue to be interested in Ansible and operation/construction automation.
That's all for now.
1