Getting Started with Ansible

Why Ansible?

If you have tens, hundreds or thousands of servers, you will need a solution like Ansible or Puppet. These products allow you to define how each server is configured in declarative languages and they can control thousands of servers.

Starting with Ansible

I provisioned an ARM64 host for Ansible yesterday. I wrote an article that explains how to install Ansible on it here. Now I want to test it.

So the Ansible host has to know about Jenkins host. I have added the following lines in /etc/ansible/hosts file.

[jenkins]
jenkins.pub.ashburn.oraclevcn.com

The hosts file can have IP address or FQDN, so I added the FQDN on OCI.

Now I should be able to ssh into the Jenkins host from Ansible host, so I added the public key of Ansible to Jenkins’ authorized_keys. Now I can ssh into the Jenkins host from the Ansible host.

On the Ansible host, test the configuration.

ansible-3 all -m ping

Output:

jenkins.pub.ashburn.oraclevcn.com | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

Test running a command on the client host.

ansible all -a "/bin/echo hello"

Output:

jenkins.pub.ashburn.oraclevcn.com | CHANGED | rc=0 >>
hello

Creating a Playbook

I am creating the following file jenkins_playbook.yaml with the content below.

- name: Jenkins Playbook
  hosts: all
  tasks:
    - name: Create a file
      shell: |
       echo 'hoge hoge hoge' >> ~/test.txt

Execute the playbook.

ansible-playbook-3 jenkins_playbook.yaml

Output:

PLAY [Jenkins Playbook] **************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************
[WARNING]: Platform linux on host jenkins.pub.ashburn.oraclevcn.com is using the discovered Python interpreter at /usr/bin/python, but future
installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [jenkins.pub.ashburn.oraclevcn.com]

TASK [Create a file] *****************************************************************************************************************************
changed: [jenkins.pub.ashburn.oraclevcn.com]

PLAY RECAP ***************************************************************************************************************************************
jenkins.pub.ashburn.oraclevcn.com : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

We’ll ignore the warning for now. Now I am going to ssh into the Jenkins host and check the ~/test.txt file.

[opc@jenkins ~]$ cat test.txt
hoge hoge hoge

This concludes the very basics of how Ansible works. I am planning to dig into it more as I have time during the end of year holidays.

How to List Listening Ports on Linux

When you start daemons or Docker containers that listen to certain ports, you want to make sure they are actually listening. You can use netstat to list listening ports.

netstat -tulpn

As you can see, we can’t tell the process or process ID for each port. If you just add sudo, you can see the process information.

This is an essential tool when you diagnose what’s going on on the host where you want to host daemons/services.

2021 at a Glance

I have been blogging since June 2018. This year has been rocky in my personal life (health wise) but I accomplished something great in this space. I have taken full control over my blog engine and DNS by moving my domain name to DNSimple and by provisioning WordPress engine on Docker on OCI. That was some struggle but it was totally worth it for me because I’ve got to learn so much from it.

After all I am a software engineer and I should be able to do it but if you don’t try to dig into it, I would never have accomplished it. This blog is hosted in OCI where the ARM64 host is free of charge, so I am only spending the money for the DNSimple service which is about $60 annually. $60 for your own domain and a free hosting for your blog site is totally worth it if you don’t mind spending some hours to get it up and running.

My passion is in automation, so I will blog my findings in that area in the next year or whatever is related to software engineering. This is my space after all. Thank you for the all visitors and the ones who left comments in this site.

I look forward to interacting with more people next year.