Starting Infrastructure as Code with Ansible (Practical Edition)
table of contents
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.