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.

Leave a Reply

Your email address will not be published.