[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

Starting Infrastructure as Code with Ansible (Practical Edition)

Hello everyone, this

is Okazaki from the SRE team of the System Solutions Department

Last time I wrote about introducing and installing Ansible tools, so this time I would like to finally introduce how to run Ansible.

Ansible runtime configuration

The things required to run Ansible are as follows.

  • hosts file (inventory file)
  • playbook
  • module

I will explain each of them.

What is the hosts file (inventory file)? ?

The hosts file (inventory file) is a file in which you enter the hosts you want to work with.
An example entry is as follows.

[all] XXX.XXX.XXX.XXX XXX.XXX.XXX.XXX [web] XXX.XXX.XXX.XXX [db] XXX.XXX.XXX.XXX

You can specify the execution host group in each [] part.
If you save the above file with any name, add the option -i and specify the saved file when running Ansible, each command will be executed for the host you entered.

What is playbook? ?

This is a file in which you can write the commands and work flow you want to execute on the target host.
Below is the 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

- In the name part, the - part represents the start of this process, and the name part is the name of this process.

The hosts part can specify the group part explained above.
This time, we have specified [all] for all hosts.

The remote_user part specifies the user on the host you are working with.
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.
This time, we will need to install it, so we will need root privileges, so we will select yes.

The part below task is the command that will actually be executed.
This time, we will use the yum command to install apache.

What is a module? ?

I think there was a command part in the playbook part earlier, and that part becomes each module.
Last time, we introduced modules as one of the features, but modules are still being developed in various languages ​​and
are used not only to configure the server itself, but also to configure the cloud platform.
For 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! !

Now let's finally try running Ansible.
First, enter the host you want to work on 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 execute on the target host.
*--private-key= specifies the SSH private key for logging in to the target.

# 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

We will check to see if it has been installed.

$ rpm -qa | grep httpd httpd-tools-2.2.34-1.16.amzn1.x86_64 httpd-2.2.34-1.16.amzn1.x86_64

It was installed successfully! ! !

summary

This time I wrote about how to run Ansible.
Next time, I would like to write more about how to set roles.
We hope that you will continue to have even the slightest interest in Ansible and operation/construction automation.
That concludes this.

If you found this article helpful , please give it a like!
1
Loading...
1 vote, average: 1.00 / 11
394
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

Junichiro Okazaki

Extensive experience in relocating and operating smartphone games.

He handles multi-cloud operations, server construction and relocation on a daily basis. As the number of cases has increased, I am considering how to improve the efficiency of my work. We often consider methods for relocating servers based on the merits of each cloud.

While we were relocating between clouds and from physical to cloud, we achieved two consecutive victories in a competition held by the Japan MSP Association.