Monday, January 13, 2020

Ansible in OCI

Ansible is  an orchestration tool which helps in provisioning/Configuration of infrastructure resources. Also it doesn't require any agents to be installed on the targets. Ansible uses ssh to connect to the host and execute command via the shell.

Okay, lets come down to the point.


How to install ansible

Create a directory ansible.
[opc@cloud ~]$ cd ansible/
[opc@cloud ansible]$ sudo yum install -y ansible


Once ansible is installed, we can check the version as

[opc@cloud ansible]$ ansible --version
ansible 2.7.9
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/opc/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  7 2019, 08:19:52) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39.0.1)]

Install the ansible OCI module:-
[opc@cloud ansible]$ git clone https://github.com/oracle/oci-ansible-modules.git
Cloning into 'oci-ansible-modules'...
remote: Enumerating objects: 147, done.
remote: Counting objects: 100% (147/147), done.
remote: Compressing objects: 100% (109/109), done.
remote: Total 3221 (delta 83), reused 64 (delta 35), pack-reused 3074
Receiving objects: 100% (3221/3221), 9.94 MiB | 4.40 MiB/s, done.
Resolving deltas: 100% (2581/2581), done.
[opc@cloud ansible]$



Once these are installed, please make sure /home/opc/.oci file and crosscheck the details. When we run ansible, it is going to get the details from this file.

Test:-

Using OCI CLI, list the compartment ids in the tenancy.


[opc@cloud list_objects]$  oci compute instance list --compartment-id ocid1.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXirdc7bfecxavf2wu7q
WARNING: Python 2 support is ending on December 31, 2019. Future versions of OCI CLI after January 2020, will not be compatible with Python 2. To avoid interruption at that time, please install OCI CLI in a compatible Python 3 environment.
{
  "data": [
    {
      "agent-config": {
        "is-monitoring-disabled": false
      },
      "availability-domain": "yjnv:AP-SYDNEY-1-AD-1",
      "compartment-id": "ocid1.tenancy.oc1..XXXXXXXXXXXXXXXXXXXXXXXkkmredbjrirdc7bfecxavf2wu7q",
      "dedicated-vm-host-id": null,
      "defined-tags": {},
      "display-name": "cloud",
      "extended-metadata": {},
      "fault-domain": "FAULT-DOMAIN-1",
      "freeform-tags": {},
      "id": "ocid1.instance.XXXXXXXXXXXXXXXXXXXXXXXe7gpqcuexcxpa4dciogwvxgiwwufxmv7aqdnxb6ajhsuaflqhq",
      "image-id": "ocid1.image.ocXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXqmmehrprg6gdjxs4on5lpzwiv64przksa",
      "ipxe-script": null,
      "launch-mode": "PARAVIRTUALIZED",
      "launch-options": {
        "boot-volume-type": "PARAVIRTUALIZED",
        "firmware": "UEFI_64",
        "is-consistent-volume-naming-enabled": true,
        "is-pv-encryption-in-transit-enabled": true,
        "network-type": "PARAVIRTUALIZED",
        "remote-data-volume-type": "PARAVIRTUALIZED"
      },
      "lifecycle-state": "RUNNING",
      "metadata": {
        "ssh_authorized_keys": "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAvh9K2yIdfa3XtbmK3hrE2YmIx7dijhP8pLGrIkM2cuevLe+GCE4wc9mzpsDXxobLGTtIvjTVqbj+ZaznzLxSKJtUD8AMRQW1yajM+55YTC9H8XUO4WloXUDOavhrpyrnl4OV69t1raZ7W9tJqtsxdFElNutq1QKP39X1HoUVCVqXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXQK3R+IjjzIXoODWXNdh48GFYkLK1K7ULvbtS2MPQ== rsa-key-20191215"
      },
      "region": "ap-sydney-1",
      "shape": "VM.Standard.E2.1.Micro",
      "source-details": {
        "boot-volume-size-in-gbs": null,
        "image-id": "ocid1.image.oc1..aaaaaXXXXXXXXXXXXXXXXXXXehrprg6gdjxs4on5lpzwiv64przksa",
        "kms-key-id": null,
        "source-type": "image"
      },
      "system-tags": {
        "orcl-cloud": {
          "free-tier-retained": "true"
        }
      },
      "time-created": "2019-12-15T18:07:45.645000+00:00",
      "time-maintenance-reboot-due": null
    }
  ]
}




Now we will verify if ansible is returning the same value or not.

Create a ansible playbook list_buckets.yml

---
- name : List summary of existing buckets in OCI object storage
  connection: local
  hosts: localhost
  tasks:
    - name: List bucket facts
      oci_bucket_facts:
         namespace_name: 'sd4XXXX'
         compartment_id: 'ocid1.tXXXXXXXXXXXmy5ox5j7o7w64v44kjpnjkkmredbjrirdc7bfecxavf2wu7q'
      register: result
    - name: Dump result
      debug:
        msg: '{{result}}'



----------------------



Please note:namespace name will be retirved from the tenancy page details.

[opc@cloud ~]$ ansible-playbook list_buckets.yml
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [List summary of existing buckets in OCI object storage] *******************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [localhost]

TASK [List bucket facts] ********************************************************************************************************************************************************************
ok: [localhost]

TASK [Dump result] **************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "buckets": [
            {
                "compartment_id": "ocXXXXXXXXXXXXXX5j7o7w64v44kjpnjkkmredbjrirdc7bfecxavf2wu7q",
                "created_by": "ocid1.saml2idp.oc1..aaaaaaaawh2o7c7azqcmc23kzme7frbsmidnmses2ci7oif2jyjb2qegfvxq/XXXXXXXX@gmail.com",
                "defined_tags": null,
                "etag": "4c3cc183-10c4-4095-ba0c-c754cdc17b1c",
                "freeform_tags": null,
                "name": "bucket-20200105-2041",
                "namespace": "sd4XXXXXX",
                "time_created": "2020-01-05T15:11:44.180000+00:00"
            }
        ],
        "changed": false,
        "failed": false
    }
}

PLAY RECAP **********************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0


Thus we can see that the values returned through ansible and OCI cli are same. For more information please visit https://docs.cloud.oracle.com/iaas/Content/API/SDKDocs/ansiblegetstarted.htm#samples


Hope it helps someone. Happy learning.


1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete