top of page

Posts

Getting Started with Ansible for Infrastructure as Code (Practice)



Last time, I wrote about introducing the Ansible tool and its installation. In this article, I'd like to delve into how to actually run Ansible.


Ansible execution configuration

Requirements

  • hosts file (inventory file)

  • playbook

  • module

I’m going to explain each of the details below.


What is the hosts file (inventory file)?

The hosts file (inventory file) is a file in which the hosts to be worked on are entered.

An example 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 in the [] part of each file.

If you save it under an arbitrary name and specify the saved file with the option -i when executing Ansible, each command will be executed on the host you specified.



What is playbook?

This is the file where you enter the commands you want to execute on the host.

The following is a playbook for installing apache.


---
# Main Play operation playbook
- name: apply common configuration to all nodes
  hosts: all
  remote_user: [User name]
  sudo: yes
 
  tasks:
    - name: apache-install
      yum:
        name=httpd
        state=presen

The -name part indicates the beginning of this process, and the name part is the name of this process.


The "hosts" part can specify the group part described above.

In this case, [all] is specified to mean all hosts.


The remote_user part specifies the user on the target host.

If the user on the remote host is not specified, the command will not be executed and an error will occur.


The sudo part specifies whether to execute the command with root privileges on the remote_user.

In this case, the command requires root privileges because installation is necessary, so put “yes”..


The part below "task" is the actual command to be executed.

This time, the command to install apache is by yum command.


What is module?

The command part of the playbook is each module.

I introduced modules in the last article. Modules have been developed in a variety of languages and are used not only to configure the server itself, but also to configure the cloud platform.

Some modules not only configure the server itself, but also configure the cloud platform.


Let’s try it!

Now it is time to run Ansible.

First, enter the hosts to be worked on in the hosts file

# vi hosts
 
========================
[all]
XXX.XXX.XXX.XXX
========================

Then 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, we execute the commands we want to run on the target host using the ansible-playbook command.

The SSH private key for logging in to the target host is specified in the --private-key= field


# 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

Double 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

It’s done!


Summary

In this article, I have described how to run Ansible.

In the next article, I would like to describe how to set up more roles.



This blog post is translated from a blog post written by Junichiro Okazaki on our Japanese website Beyond Co..

Comments


bottom of page