DevOps Classroomnotes 11/Aug/2022

Ansible Facts

  • Refer Here for facts
  • Facts are collected by using setup module Refer Here
  • Refer Here for the changes done to make the playbook work on centos as well along with ubuntu
  • Now execute this playbook and on centos as well as ubuntu the tomcat 9 with jdk 11 should be setup.
  • Lets add a message to stop execution when the os is neither Ubuntu nor Centos Refer Here
  • Now lets add debug message for the user to understand the execution Refer Here
  • Sample output
PLAY [install tomcat 9] **************************************************************************

TASK [Gathering Facts] ***************************************************************************
ok: [172.31.9.68]
ok: [172.31.6.58]

TASK [Fail on unsupported operating systems] *****************************************************
skipping: [172.31.6.58]
skipping: [172.31.9.68]

TASK [ansible.builtin.debug] *********************************************************************
ok: [172.31.6.58] => {
    "msg": "Installing Java 11 with package openjdk-11-jdk "
}
ok: [172.31.9.68] => {
    "msg": "Installing Java 11 with package java-11-openjdk-devel "
}

TASK [update ubuntu packages and install java 11] ************************************************
skipping: [172.31.9.68]
ok: [172.31.6.58]

TASK [install java 11 on centos] *****************************************************************
skipping: [172.31.6.58]
ok: [172.31.9.68]

TASK [ansible.builtin.debug] *********************************************************************
ok: [172.31.6.58] => {
    "msg": "Creating a user  tomcat with home directory /opt/tomcat and shell /bin/false "
}
ok: [172.31.9.68] => {
    "msg": "Creating a user  tomcat with home directory /opt/tomcat and shell /bin/false "
}

TASK [Create a system user] **********************************************************************
ok: [172.31.6.58]
ok: [172.31.9.68]

TASK [ansible.builtin.debug] *********************************************************************
ok: [172.31.6.58] => {
    "msg": "Downloading tomcat from https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-9.0.65.tar.gz to /tmp/apache-tomcat-9.0.65.tar.gz"
}
ok: [172.31.9.68] => {
    "msg": "Downloading tomcat from https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-9.0.65.tar.gz to /tmp/apache-tomcat-9.0.65.tar.gz"
}

TASK [download tomcat] ***************************************************************************
ok: [172.31.9.68]
ok: [172.31.6.58]

TASK [Flush handlers] ****************************************************************************

TASK [create symbolic link] **********************************************************************
ok: [172.31.6.58]
ok: [172.31.9.68]

TASK [Recursively change ownership of a tomcat home directory] ***********************************
ok: [172.31.6.58]
ok: [172.31.9.68]

TASK [Give Execute permissions to shell files in tomcat's bin directory] *************************

TASK [copy the tomcat service unit file] *********************************************************
ok: [172.31.6.58]
ok: [172.31.9.68]

TASK [ansible.builtin.debug] *********************************************************************
ok: [172.31.6.58] => {
    "msg": "Reloading daemon and ensuring tomcat service is running"
}
ok: [172.31.9.68] => {
    "msg": "Reloading daemon and ensuring tomcat service is running"
}

TASK [reload daemon] *****************************************************************************
ok: [172.31.9.68]
ok: [172.31.6.58]

TASK [copy the tomcat users xml] *****************************************************************
ok: [172.31.6.58]
ok: [172.31.9.68]

TASK [copy manager context xml] ******************************************************************
ok: [172.31.6.58]
ok: [172.31.9.68]

TASK [copy host manager context xml] *************************************************************
ok: [172.31.6.58]
ok: [172.31.9.68]

PLAY RECAP ***************************************************************************************
172.31.6.58                : ok=15   changed=0    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0
172.31.9.68                : ok=15   changed=0    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0
  • Consider the following tasks. Both are almost the same apart from apt or yum. Ansible has a module called as package Refer Here
- name: update ubuntu packages and install java 11
      ansible.builtin.apt:
        name: "{{ java_package_name }}"
        update_cache: yes
        state: present
      when:
        - ansible_facts["distribution"] == "Ubuntu"
- name: install java 11 on centos
    ansible.builtin.yum:
    name: "{{ java_package_name }}"
    state: present
    when:
    - ansible_facts["distribution"] == "CentOS"
  • Refer Here for the changes after including the package module

Jinja Templates

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner