Showing posts with label Ansible. Show all posts
Showing posts with label Ansible. Show all posts

Tuesday, December 8, 2020

jboss automation using ansible

 Here i am providing the ansible code for Jboss installation  automation . We have ansible tower server called unixchips-jbossserver and the client ( where the jboss needs to be installed ) unixchips-jbossclient. We are installing Jboss-EAP 7.3 using the ansible .

1. First copy the "jboss-eap-7.3.0.zip to the unixchips-jbossclient machine 

-rw-r--r--  1 root root 206430757 Nov 24 20:21 jboss-eap-7.3.0.zip

2. Let's create a role for jboss installation called jboss-standalone .

[unixchips@unixchips-jbossserver jboss-standalone]$ pwd

/etc/ansible/roles/jboss-standalone

3. We need java to be installed in the server as a prerequisite before the jboss installation. So let's create a yamal file to install the java (openjdk) if it is not installed already

**************************************************************************

[unixchips@unixchips-jbossserver tasks]$ cat javanew.yml


      - name: Check if java is installed

        command: java -version

        become: true

        register: java_result

        ignore_errors: True


      - debug:

          msg: "Failed - Java is not installed"

        when: java_result is failed

      - name:

        yum:

         name: java-1.8.0-openjdk.x86_64

         state: present

      - debug:

          msg: "Success - Java is installed"

        when:  java_result is success

**********************************************************************

4. Next we have to create the playbook.yml as below which we will call directly for the installation 

**********************************************************************

- name: main playbook

  hosts: all

  remote_user: unixchips

  become: yes

  vars_prompt:

    - name: instancename

      prompt: "please enter the instancename"

      private: no

    - name: admin_username

      prompt: "please enter the jboss admin name"

      private: no

    - name: admin_passwd

      prompt: "please enter the jboss admin password"

      private: yes

    - name: grp_name

      prompt: "please enter the admin group name"

      private: no


  vars:

    jboss_hostname: 'unixchips-jbossclient'

    cert_alias_name: 'unixchips-jbossclient'

    jboss_bind_address: '165.115.46.43'

    jboss_port_offset: '700'

    jboss_eap_version: 'jboss-eap-7.3'

    instance_name: 'JTS'

    java_home: '/usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64'

    offset_port: '700'

  roles:

    - jboss-standalone

******************************************************************************

5. The main yamal file for execution is given below with the detailed steps mentioned in it . This yamal file includes 
1. creating the jboss user & group
2. create the /opt/jboss/instance/instance name folder
3.check the java installation status 
4. extract the jboss zip file 
5.copy the required setup/startup/stop scripts 
6. pass the required variables to standalone-full.xml and copy the file to configuration directory
7. finally start the instance using the startup script as jboss user
8. add the admin user and password for jboss console access 

The main.yml file location is  /etc/ansible/roles/jboss-standalone/tasks/


[unixchips@unixchips-jbossserver tasks]$ cat main.yml

---

# - hosts:all


- name: "add user jboss"

  user:

    name: jboss

    group: jboss

    home: /home/jboss


- name: add group "jboss"

  group:

    name: jboss


- include: javanew.yml



- file:

    path: /opt/jboss/instances/{{instancename}}

    state: directory

    owner: jboss

    group: jboss


- file:

    path: /opt/jboss/bin

    state: directory

    owner: jboss

    group: jboss


- name: "extract the jboss.zip file"

  shell: |

    cd "/opt/jboss/instances/{{instancename}}"

    /bin/unzip /tmp/jboss-eap-7.3.0.zip

    chown -R jboss:jboss jboss-eap-7.3


- file:

    path: /opt/jboss/instances/{{instancename}}/jboss-eap-7.3/standalone/log

    state: directory

    owner: jboss

    group: jboss

    mode: 0775

- name: copy the scripts

  template:

    src: /etc/ansible/files/setup_jboss.sh.j2

    dest: /opt/jboss/bin/setup_{{instancename}}.sh

    owner: jboss

    group: jboss

    mode: 0755

#  delegate_to: mtl-jbosstest2.cn.ca


- name: copy the startup script

  template:

    src: /etc/ansible/files/startup_jboss.sh.j2

    dest: /opt/jboss/bin/startup_{{instancename}}.sh

    owner: jboss

    group: jboss

    mode: 0777


- name: copy the stop script

  template:

    src: /etc/ansible/files/stop_jboss.sh.j2

    dest: /opt/jboss/bin/stop_{{instancename}}.sh

    owner: jboss

    group: jboss

    mode: 0755



- name: configure the jboss properties file

  template:

         src: /etc/ansible/files/jboss.properties.j2

         dest: /opt/jboss/bin/jboss_{{instancename}}.properties

         owner: jboss

         group: jboss

         mode: 0755


- name: copy the standalone.xml file

  template:

         src: /etc/ansible/files/standalone-full.xml.j2

         dest: /opt/jboss/instances/{{instancename}}/jboss-eap-7.3/standalone/configuration/standalone-full.xml

         owner: jboss

         group: jboss

         mode: 0755


- name: start the jboss script

  become_user: jboss

  shell: |

    cd /opt/jboss/bin

    /bin/sh startup_{{instancename}}.sh


- name: add the jboss admin user and group

  become_user: jboss

  shell: |

    cd /opt/jboss/instances/{{instancename}}/jboss-eap-7.3/bin

    /bin/sh add-user.sh -u '{{admin_username}}' -p '{{admin_passwd}}' -g '{{grp_name}}'

*******************************************************************************

6. Let's check the files which we used as j2 templates along with this script 

  a. setup_jboss.sh.j2

The template location will be /etc/ansible/files and durring the execution it will pick the desired values from the playbook.yml and copy to the unixchips-jbossclient  (/opt/jboss/bin)

*********************************************************

source /opt/jboss/bin/jboss_{{instancename}}.properties

PATH=${JAVA_HOME}/bin:${PATH}

export JAVA_HOME EAP_HOME PATH

**********************************************************

b. Next template is the jboss.properties.j2 which is used to configure the jboss peoprty details along with the scripts. variables will be picked up from the playbook.yml file and the play book will attach te same to the property file and copy to the client location ( /opt/jboss/bin)

*******************************************************************
[unixchips@unixchips-jbossserver files]$ cat jboss.properties.j2
JBOSS_HOSTNAME={{ jboss_hostname }}
CERT_ALIAS_NAME={{ cert_alias_name }}
JBOSS_BIND_ADDR={{ jboss_bind_address }}
JBOSS_PORT_OFFSET={{ jboss_port_offset }}
GENERATE_CERT=true
LOCALITY=Bangalore
STATE=KA
COUNTRY=INDIA
JBOSS_PORT=$((JBOSS_PORT_OFFSET + 9990))
JBOSS_USER=jboss
USER_TFS=svc-teamfbuild
#svc-teamfprodbuild
JBOSS_ADMIN_USER=jbossadmin
#jboss-jondev-Administrator
#jboss-jonuat-Administrator
#jboss-jonstg-Administrator
#jboss-jonprd-Administrator
#Apigee Administrators
jbossAdminGrp=jboss-jonuat-Administrator
#ldapsrc-jbossndev
#ldapsrc-jbossnuat
#ldapsrc-jbossnstg
#ldapsrc-jbossnprd
#ldapsrc-jbossnppd
#ldapUser=ldapsrc-jbossnuat
JBOSS_EAP_VERSION={{ jboss_eap_version }}
JBOSS_ZIP=/opt/$JBOSS_USER/$JBOSS_EAP_VERSION.0.zip
EAP_HOME=/opt/jboss/instances/{{ instance_name }}/$JBOSS_EAP_VERSION
JAVA_HOME={{ java_home }}
USE_DYNATRACE=false

********************************************************************
c. startup_jboss.sh.j2  . This is the script which we are using to start the instances . Required parameters will be copied from the playbook.yml and main.yml file will copy the same to /opt/jboss/bin directory of the client machine 

******************************************************************

[unixchips@unixchips-jbossserver files]$ cat startup_jboss.sh.j2

source /opt/jboss/bin/jboss_{{instancename}}.properties

. /opt/jboss/bin/setup_{{instancename}}.sh

nohup ${EAP_HOME}/bin/standalone.sh -Djboss.bind.address=$JBOSS_BIND_ADDR -Djboss.bind.address.management=$JBOSS_HOSTNAME -Djboss.server.default.config=standalone-full.xml >> /opt/jboss/instances/{{ instance_name }}/$JBOSS_EAP_VERSION/standalone/log/jboss_Test.log 2>&1 &

***********************************************************

d. stop_jboss.sh.j2 , This is the script to stop the instance and as usual the parameters will be taken from the jboss.property file and playbook.yml. File location will be /opt/jboss/bin directory in client machine 

************************************************************************


[unixchips@unixchips-jbossserver files]$ cat stop_jboss.sh.j2

source /opt/jboss/bin/jboss_{{instancename}}.properties


. /opt/jboss/bin/setup_{{instancename}}.sh

${EAP_HOME}/bin/jboss-cli.sh --connect --controller=$JBOSS_HOSTNAME:$JBOSS_PORT -c --command=shutdown


sleep 10

JbossInstance_PID=`ps -ef | grep java | grep jboss | grep -w {{instancename}} | grep -v grep | awk '{print $2}'`

if [ -z $JbossInstance_PID ]; then

    echo "Jboss Instance Stopped"

else

    echo "Process still running... Kill process:" $JbossInstance_PID

    kill -9 $JbossInstance_PID

fi

****************************************************************

7. Now let's execute the ansible script and check the output ..i have made the small modifications in the playbook.yml as given the instance name as "ttk" and offset port as 750 . Now let's call the playbook 

***********************************************************************************

[unixchips@unixchips-jbossserver ~]$ ansible-playbook -i /etc/ansible/hosts playbook.yml
[DEPRECATION WARNING]: The TRANSFORM_INVALID_GROUP_CHARS settings is set to allow bad characters in group names by default, this will
change, but still be user configurable on deprecation. This feature will be removed in version 2.10. Deprecation warnings can be
disabled by setting deprecation_warnings=False in ansible.cfg.
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

please enter the instancename: ttk
please enter the jboss admin name: adminuser1
please enter the jboss admin password:
please enter the admin group name: mgmtuser

PLAY [main playbook] ********************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************
ok: [unixchips-jbosseclient]

TASK [jboss-standalone : add user jboss] ************************************************************************************************
ok: [unixchips-jbosseclient]

TASK [jboss-standalone : add group "jboss"] *********************************************************************************************
ok: [unixchips-jbosseclient]

TASK [jboss-standalone : Check if java is installed] ************************************************************************************
changed: [unixchips-jbosseclient] 

TASK [jboss-standalone : debug] *********************************************************************************************************
skipping: [unixchips-jbosseclient ]

TASK [jboss-standalone : yum] ***********************************************************************************************************
ok: [unixchips-jbosseclient ]

TASK [jboss-standalone : debug] *********************************************************************************************************
ok: [unixchips-jbosseclient ] => {
    "msg": "Success - Java is installed"
}

TASK [jboss-standalone : file] **********************************************************************************************************
changed: [unixchips-jbosseclient ]

TASK [jboss-standalone : file] **********************************************************************************************************
ok: [unixchips-jbosseclient ]

TASK [jboss-standalone : extract the jboss.zip file] ************************************************************************************
changed: [unixchips-jbosseclient ]

TASK [jboss-standalone : file] **********************************************************************************************************
changed: [unixchips-jbosseclient ]

TASK [jboss-standalone : copy the scripts] **********************************************************************************************
changed: [unixchips-jbosseclient ]

TASK [jboss-standalone : copy the startup script] ***************************************************************************************
changed: [unixchips-jbosseclient ]

TASK [jboss-standalone : copy the stop script] ******************************************************************************************
changed: [unixchips-jbosseclient ]

TASK [jboss-standalone : configure the jboss properties file] ***************************************************************************
changed: [unixchips-jbosseclient ]

TASK [jboss-standalone : copy the standalone.xml file] **********************************************************************************
changed: [unixchips-jbosseclient ]

TASK [jboss-standalone : start the jboss script] ****************************************************************************************
changed: [ unixchips-jbosseclient]

TASK [jboss-standalone : add the jboss admin user and group] ****************************************************************************
changed: [unixchips-jbosseclient ]

PLAY RECAP ******************************************************************************************************************************
unixchips-jbosseclient       : ok=17   changed=11   unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

8. Now if login to the client machine and check the "ttk" instance we can see jboss instance  is installed

 [root@unixchips-jbossclient instances]# ps -ef |grep -i ttk
jboss    26947     1  0 19:53 ?        00:00:00 /bin/sh /opt/jboss/instances/ttk/jboss-eap-7.3/bin/standalone.sh -Djboss.bind.address=165.115.46.43 -Djboss.bind.address.management=mtl-jbosstest2.cn.ca -Djboss.server.default.config=standalone-full.xml
jboss    27040 26947 82 19:53 ?        00:00:14 /usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.222.b10-0.el7_6.x86_64/bin/java -D[Standalone] -server -verbose:gc -Xloggc:/opt/jboss/instances/ttk/jboss-eap-7.3/standalone/log/gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=3M -XX:-TraceClassUnloading -Xms1303m -Xmx1303m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true -Dorg.jboss.boot.log.file=/opt/jboss/instances/ttk/jboss-eap-7.3/standalone/log/server.log -Dlogging.configuration=file:/opt/jboss/instances/ttk/jboss-eap-7.3/standalone/configuration/logging.properties -jar /opt/jboss/instances/ttk/jboss-eap-7.3/jboss-modules.jar -mp /opt/jboss/instances ttk/jboss-eap-7.3/modules org.jboss.as.standalone -Djboss.home.dir=/opt/jboss/instances/ttk/jboss-eap-7.3 -Djboss.server.base.dir=/opt/jboss/instances/ttk/jboss-eap-7.3/standalone -Djboss.bind.address=165.115.46.43 -Djboss.bind.address.management=mtl-jbosstest2.cn.ca -Djboss.server.default.config=standalone-full.xml

Thank you for reading 










Friday, June 7, 2019

Ansible modules and examples









Ansible modules are the small programs which will execute in remote systems through ssh protocol. The library of modules resides in any machines which is called by the playbooks on timely manner and executes it.  I will introduce some common modules which is used in ansible and its working examples ..

1. The setup module 

This module is used to pullout configuration details of remote machines which is called as facts.

Let's create a temp inventory file called inventory.1

[unixchips@unixchips-server ~]$ cat inventory.1
unixchips

use the below command to collect all the details from the remote host

[unixchips@unixchips-server ~]$ ansible -m setup -i inventory.1 all
[DEPRECATION WARNING]: Distribution Ubuntu 16.04 on host unixchips should use
/usr/bin/python3, but is using /usr/bin/python for backward compatibility with
prior Ansible releases. A future Ansible release will default to using the
discovered platform python for this host. See https://docs.ansible.com/ansible/
2.8/reference_appendices/interpreter_discovery.html for more information. This
feature will be removed in version 2.12. Deprecation warnings can be disabled
by setting deprecation_warnings=False in ansible.cfg.
unixchips | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.56.1",
            "192.168.2.16"
        ],
        "ansible_all_ipv6_addresses": [
            "fe80::800:27ff:fe00:0",
            "fe80::766b:d730:8352:3325"
        ],
        "ansible_apparmor": {
            "status": "enabled"
        },
        "ansible_architecture": "x86_64",
        "ansible_bios_date": "01/11/2011",
        "ansible_bios_version": "81ET49WW (1.25 )",
        "ansible_cmdline": {
            "BOOT_IMAGE": "/boot/vmlinuz-4.15.0-50-generic",
            "quiet": true,
            "ro": true,
            "root": "UUID=4f41e130-df1e-4a41-b2ae-2539efa5f605",
            "splash": true,
----------------<output omitted>------------------------------------------------------------

Use the below command to use the filter option to filter particular facts. Below command will provide the memory details 

[unixchips@unixchips-server ~]$ ansible -m setup -i inventory.1 unixchips -a 'filter=ansible_*_mb'
[DEPRECATION WARNING]: Distribution Ubuntu 16.04 on host unixchips should use /usr/bin/python3, but is using /usr/bin/python for backward 
compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See 
https://docs.ansible.com/ansible/2.8/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in 
version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
unixchips | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 3482, 
        "ansible_memory_mb": {
            "nocache": {
                "free": 5136, 
                "used": 2637
            }, 
            "real": {
                "free": 3482, 
                "total": 7773, 
                "used": 4291
            }, 
            "swap": {
                "cached": 0, 
                "free": 7986, 
                "total": 7986, 
                "used": 0
            }
        }, 
        "ansible_memtotal_mb": 7773, 
        "ansible_swapfree_mb": 7986, 
        "ansible_swaptotal_mb": 7986, 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

Now if we want too get the outputs of the facts in a play book we have use the facts like below . Below ply book will provide the memory details 

[unixchips@unixchips-server ~]$ cat os_mem.yml 
- name: print out operating system
  hosts: all
  gather_facts: True
  tasks:
  - debug: var=ansible_memory_mb

[unixchips@unixchips-server ~]$ ansible-playbook os_mem.yml 
[DEPRECATION WARNING]: The TRANSFORM_INVALID_GROUP_CHARS settings is set to allow bad characters in group names by default, this will change, 
but still be user configurable on deprecation. This feature will be removed in version 2.10. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.
 [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details


PLAY [print out operating system] *************************************************************************************************************

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

TASK [debug] **********************************************************************************************************************************
ok: [unixchips] => {
    "ansible_memory_mb": {
        "nocache": {
            "free": 4849, 
            "used": 2924
        }, 
        "real": {
            "free": 3193, 
            "total": 7773, 
            "used": 4580
        }, 
        "swap": {
            "cached": 0, 
            "free": 7986, 
            "total": 7986, 
            "used": 0
        }
    }
}

PLAY RECAP ************************************************************************************************************************************
unixchips                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

2.The File module 

first let's take the file details using below command 
---------------------------------------------------------------------------------
[unixchips@unixchips-server ~]$ ansible unixchips -m file -a 'path=/etc/fstab'
unixchips | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "gid": 0, 
    "group": "root", 
    "mode": "0664", 
    "owner": "root", 
    "path": "/etc/fstab", 
    "size": 594, 
    "state": "file", 
    "uid": 0
}

create a file using file module 
---------------------------------------------------------
[unixchips@unixchips-server ~]$ cat file.yml 
- hosts: all
  tasks:
  - name: Ansible create file if it doesn't exist example
    file:
      path: "/tmp/devops_server.txt"
      state: touch

[unixchips@unixchips-server ~]$ ansible-playbook file.yml 
[DEPRECATION WARNING]: The TRANSFORM_INVALID_GROUP_CHARS settings is set to allow bad characters in group names by default, this will change, 
but still be user configurable on deprecation. This feature will be removed in version 2.10. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.
 [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details


PLAY [all] ************************************************************************************************************************************

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

TASK [Ansible create file if it doesn't exist example] ****************************************************************************************
changed: [unixchips]

PLAY RECAP ************************************************************************************************************************************
unixchips                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

if we login to the unixchips machine we can see that file is created 

unixchips@unixchips:/tmp$ ls -lrt |grep *.txt
-rw-rw-r-- 1 unixchips unixchips    0 Jun  4 13:47 devops_server.txt
-------------------------------------------------------------------------------------
Now let's create a file with owner as root and permission as 777

[unixchips@unixchips-server ~]$ cat file.yml 
- hosts: all
  become: yes
  tasks:
  - name: Ansible create file if it doesn't exist example
    file:
      path: "/tmp/foo.txt"
      state: touch
      mode: 0777
      owner: root

[unixchips@unixchips-server ~]$ ansible-playbook file.yml 

PLAY [all] *******************************************************************************************************************************************

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

TASK [Ansible create file if it doesn't exist example] ***********************************************************************************************
changed: [unixchips]

PLAY RECAP *******************************************************************************************************************************************
unixchips                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

if we check the output 

unixchips@unixchips:/tmp$ ls -lrt |grep foo
-rwxrwxrwx 1 root      root         0 Jun  4 14:16 foo.txt


To create a file using a content we have to use copy module as below
-----------------------------------------------------------------------------------------
[unixchips@unixchips-server ~]$ cat text.yml
- hosts: all
  tasks:
  - name: Ansible create file with content example
    copy:
      dest: "/tmp/unixchips.txt"
      content: |
        welcome to unixchips 

unixchips@unixchips:/tmp$ cat unixchips.txt 
welcome to unixchips 
*************************************************************************

3. The Pause module 

If we are in a situation where certain actions needs to be performed  after the inputs from the user we have to pause the playbook execution till that time. The pause module is using for that

[unixchips@unixchips-server ~]$ cat pause.yml
- name: Pause prompt on localhost
  hosts: localhost
  tasks:
    - name: Prompt
      pause:
        prompt: "Do you want to install mysql (yes/no)?"

output
----------


[unixchips@unixchips-server ~]$ ansible-playbook pause.yml 

PLAY [Pause prompt on localhost] **************************************************************************************************************

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

TASK [Prompt] *********************************************************************************************************************************
[Prompt]
Do you want to install mysql (yes/no)?:
[ [ok: [localhost]

PLAY RECAP ************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

4. The WaitFor module 

The waitfor module is using to check some conditions , for example in below example we will wait the apache2 need to be installed and started to complete the execution

[unixchips@unixchips-server ~]$ cat waitfor.yml
--- # The wait for module
- hosts: all
  sudo: yes
  gather_facts: no
  tasks:
    - name: Installing Apache Tomcat
      action: apt name=apache2 state=installed
    - name: Waiting for the port 80 to listen
      wait_for:
        port: 80
        state: started


output
------------
[unixchips@unixchips-server ~]$ ansible-playbook waitfor.yml 
[DEPRECATION WARNING]: The TRANSFORM_INVALID_GROUP_CHARS settings is set to allow bad characters in group names by default, this will change, 
but still be user configurable on deprecation. This feature will be removed in version 2.10. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.
 [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo' (default). This feature will be
 removed in version 2.9. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

PLAY [all] ************************************************************************************************************************************

TASK [Installing Apache Tomcat] ***************************************************************************************************************
 [WARNING]: Could not find aptitude. Using apt-get instead

[DEPRECATION WARNING]: State 'installed' is deprecated. Using state 'present' instead.. This feature will be removed in version 2.9. 
Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
[DEPRECATION WARNING]: Distribution Ubuntu 16.04 on host unixchips should use /usr/bin/python3, but is using /usr/bin/python for backward 
compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See 
https://docs.ansible.com/ansible/2.8/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in 
version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
changed: [unixchips]

TASK [Waiting for the port 80 to listen] ******************************************************************************************************
ok: [unixchips]

PLAY RECAP ************************************************************************************************************************************
unixchips                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

We can see 2 tasks are get executed succesfully and ansible wait for the port 80 comes up to complete the task

from the client side we can see apache2 is installed and listening to the port

unixchips@unixchips:/tmp$ service apache2 status
● apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: active (running) since Thu 2019-06-06 00:14:46 IST; 1min 16s ago
     Docs: man:systemd-sysv-generator(8)
   CGroup: /system.slice/apache2.service
           ├─15058 /usr/sbin/apache2 -k start
           ├─15061 /usr/sbin/apache2 -k start
           └─15062 /usr/sbin/apache2 -k start

Jun 06 00:14:45 unixchips systemd[1]: Starting LSB: Apache2 web server...
Jun 06 00:14:45 unixchips apache2[15036]:  * Starting Apache httpd web server apache2
Jun 06 00:14:45 unixchips apache2[15036]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.
Jun 06 00:14:46 unixchips apache2[15036]:  *
Jun 06 00:14:46 unixchips systemd[1]: Started LSB: Apache2 web server.

***********************************************************

5. The yum module 

The yum module can be tested in local host ( ansible server) as the client is in ubuntu . We can install any packages using the yum module and the sample is below 

[unixchips@unixchips-server ~]$ cat waitfor.yml 
--- # The wait for module
- hosts: localhost
  sudo: yes
  gather_facts: no
  tasks:
    - name: Installing Ansible
      action: yum name=ansible state=installed
           

[unixchips@unixchips-server ~]$ ansible-playbook yum.yml 
[DEPRECATION WARNING]: The TRANSFORM_INVALID_GROUP_CHARS settings is set to allow bad characters in group names by default, this will change, 
but still be user configurable on deprecation. This feature will be removed in version 2.10. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.
 [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

PLAY [localhost] ******************************************************************************************************************************

TASK [Installing Ansible] *********************************************************************************************************************
ok: [localhost]

PLAY RECAP ************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

In the above example as we have already installed the ansible the task will be executed succesfully without any changes. If we want to update any packagesusing yum module instead of state installed we need to give as latest. It will search in the repository and update to the latest version 

6. The shell module 

If we need to execute multiple commands in a remote machine we can use the shell module. For example if we want to create multiple files in the client machine use the shell module as below

[unixchips@unixchips-server ~]$ cat shell.yml
- hosts: all
  sudo: yes
  tasks:
    - name: Ansible shell module multiple commands
      shell: "touch {{ item }} "
      with_items:
        - hello.txt
        - hello2.txt
        - hello3.txt
      args:
       chdir: /tmp

[unixchips@unixchips-server ~]$ ansible-playbook shell.yml 

PLAY [all] ************************************************************************************************************************************

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

TASK [Ansible shell module multiple commands] *************************************************************************************************
changed: [unixchips] => (item=hello.txt)
changed: [unixchips] => (item=hello2.txt)
changed: [unixchips] => (item=hello3.txt)
 [WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If you need to use command because file is
insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.

This module can be useful if we need to execute multiple commands in remote machine. For example 

[unixchips@unixchips-server ~]$ cat shell_new.yml 
- hosts: all
  sudo: yes
  tasks:
    - name: Ansible shell module multiple commands
      shell: |
        ls -lrt 
        touch "ubuntu.txt"
      args:
       chdir: /tmp
      register: SEQOUT
    - debug: msg={{SEQOUT.stdout_lines}}

[unixchips@unixchips-server ~]$ ansible-playbook shell_new.yml 

PLAY [all] ************************************************************************************************************************************

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

TASK [Ansible shell module multiple commands] *************************************************************************************************
changed: [unixchips]

TASK [debug] **********************************************************************************************************************************
ok: [unixchips] => {
    "msg": [
        "total 24", 
        "drwx------ 3 root      root      4096 Jun  5 11:38 systemd-private-7ccc3280b7b2446c8f016430e315661d-systemd-timesyncd.service-vM8Mv4", 
        "drwx------ 3 root      root      4096 Jun  5 11:38 systemd-private-7ccc3280b7b2446c8f016430e315661d-rtkit-daemon.service-t32t2T", 
        "-rw------- 1 unixchips unixchips    0 Jun  5 11:46 config-err-q2fjTB", 
        "-rw-rw-r-- 1 unixchips unixchips    0 Jun  5 11:46 unity_support_test.0", 
        "drwx------ 3 root      root      4096 Jun  5 11:46 systemd-private-7ccc3280b7b2446c8f016430e315661d-colord.service-YGKa2X", 
        "-rw-rw-r-- 1 unixchips unixchips   22 Jun  5 13:09 unixchips.txt", 
        "drwx------ 2 unixchips unixchips 4096 Jun  6 06:55 gnome-software-GHVV2Z", 
        "-rw-r--r-- 1 root      root         0 Jun  6 13:11 hello.txt", 
        "-rw-r--r-- 1 root      root         0 Jun  6 13:11 hello2.txt", 
        "-rw-r--r-- 1 root      root         0 Jun  6 13:11 hello3.txt", 
        "drwx------ 2 root      root      4096 Jun  6 13:58 ansible_command_payload_jmXznO"
    ]
}

PLAY RECAP ************************************************************************************************************************************
unixchips                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

7. The command module 

command module can also be used to execute system commands, but we cannot execute multiple commands using this. For example if we want to check the uptime of the unixchips node 

[unixchips@unixchips-server ~]$ cat command.yml 
- hosts: all
  sudo: yes
  tasks:
    - name: Ansible shell module multiple commands
      command: "uptime"
      register: SEQOUT
    - debug: msg={{SEQOUT.stdout_lines}}

[unixchips@unixchips-server ~]$ ansible-playbook command.yml 


PLAY [all] ************************************************************************************************************************************

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

TASK [Ansible shell module multiple commands] *************************************************************************************************
changed: [unixchips]

TASK [debug] **********************************************************************************************************************************
ok: [unixchips] => {
    "msg": [
        " 23:59:57 up 5 min,  2 users,  load average: 0.70, 1.38, 0.76"
    ]
}

PLAY RECAP ************************************************************************************************************************************
unixchips                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

8. The user module 

This module is used to create , delete users and also add/remove the users from the groups . For example , we can create the user using below playbook .

first we have to add the password in md5 format in the playbook, let's generate the password in encrypted format

[unixchips@unixchips-server ~]$ openssl passwd -1
Password:
Verifying - Password:
$1$C7ScacjV$xEeePriRC74kJlGdnT1jm.

Let's add the user first 

[unixchips@unixchips-server ~]$ cat useradd.yml 
- hosts: all
  sudo: yes
  tasks:
  - name: Ansible create user example.
    user:
      name: ansibletest
      password: $1$C7ScacjV$xEeePriRC74kJlGdnT1jm.

[unixchips@unixchips-server ~]$ ansible-playbook useradd.yml 

PLAY [all] ************************************************************************************************************************************

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

TASK [Ansible create user example.] ***********************************************************************************************************
changed: [unixchips]

PLAY RECAP ************************************************************************************************************************************
unixchips                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Now if we check the unixchips host we can see that the mentoned user is added 

root@unixchips:~# cat /etc/shadow | grep ansible
ansibletest:$1$C7ScacjV$xEeePriRC74kJlGdnT1jm.:18053:0:99999:7:::
-------------------------------------------------------------------------------------------
Let's add a user to an existing group as below 

[unixchips@unixchips-server ~]$ cat usrgrp.yml 
- hosts: all
  sudo: yes
  tasks:
  - name: Ansible create user example.
    user:
      name: ansibletest
      groups: testgroup

[unixchips@unixchips-server ~]$ ansible-playbook usrgrp.yml 

PLAY [all] ************************************************************************************************************************************

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

TASK [Ansible create user example.] ***********************************************************************************************************
changed: [unixchips]

PLAY RECAP ************************************************************************************************************************************
unixchips                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


output 

root@unixchips:~# groups ansibletest
ansibletest : ansibletest testgroup

Add a user to multiple groups 
------------------------------------
Ok let's add the ansible test user to 2 more groups called aws and azure as below 

[unixchips@unixchips-server ~]$ cat append.yml 
- hosts: all
  sudo: yes
  tasks:
  - name: Ansible create user example.
    user:
      name: ansibletest
      groups: aws, azure      
      append: yes

[unixchips@unixchips-server ~]$ ansible-playbook append.yml 

PLAY [all] ************************************************************************************************************************************

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

TASK [Ansible create user example.] ***********************************************************************************************************
changed: [unixchips]

PLAY RECAP ************************************************************************************************************************************
unixchips                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

root@unixchips:~# groups ansibletest
ansibletest : ansibletest testgroup aws azure

Now deleting the user 

[unixchips@unixchips-server ~]$ cat userdel.yml 
- hosts: all
  sudo: yes
  tasks:
  - name: Ansible create user example.
    user:
      name: ansibletest
      state: absent

[unixchips@unixchips-server ~]$ ansible-playbook userdel.yml 

PLAY [all] ************************************************************************************************************************************

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

TASK [Ansible create user example.] ***********************************************************************************************************
changed: [unixchips]

PLAY RECAP ************************************************************************************************************************************
unixchips                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

output
root@unixchips:~# id ansibletest
id: ‘ansibletest’: no such user

9.The file system & mount module 

This module is used to format the file system and mount it to the required mount point using the playbooks 

First i have created the disk as normal way

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2596473     1297213   83  Linux

now let's create a playbook to format the file system as ext3

[root@unixchips-server ~]# cat filesystem.yml 
--- #file system module example
- hosts: localhost
  sudo: yes
  gather_facts: no
  tasks:
    - name: format the disk
      filesystem: fstype=ext4 dev=/dev/sdb1 opts="-cc"

[root@unixchips-server ~]# ansible-playbook filesystem.yml 

PLAY [localhost] ***************************************************************

TASK [format the disk] *********************************************************

changed: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

we can check the status by manually mounting the file system to a directory.

Now let's mount it to the mount point using the mount module 

[unixchips@unixchips-server ~]$ cat mount.yml 
--- #file system module example
- hosts: localhost
  sudo: yes
  gather_facts: no
  tasks:
    - name: mount the file system 
      mount: name=/tmp/test src=/dev/sdb1 fstype=ext4 opts=rw state=present

[unixchips@unixchips-server ~]$ ansible-playbook mount.yml 

PLAY [localhost] ******************************************************************************************************************************

TASK [mount the file system] ******************************************************************************************************************
changed: [localhost]

PLAY RECAP ************************************************************************************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

if we check the /etc/fstab the entry is added automatically 

[unixchips@unixchips-server ~]$ cat /etc/fstab |grep /dev/sdb
/dev/sdb1 /tmp/test ext4 rw 0 0

we can see the file system is mounted succesfully 

[unixchips@unixchips-server ~]$ mount |grep /dev/sdb
/dev/sdb1 on /tmp/test type ext4 (rw,relatime,seclabel,data=ordered)

10 The temple module 

The temple module is used when we need to copy any configuration files with in the nodes where any of the variables need to be changed automatically according the node details . We are using a temple called jinja2  temple with an extension of j2.

Let's create a jinja temple as beow inside the file directory of ansible server

[root@unixchips-server files]# cat test.conf.j2
# configuration file for testing

<connectivity>
  connection type {{ connectionType }}
</connectivity>


<accountInfo>
username {{ username }}
password {{ password }}
</accountInfo>

<system_info>
DistributionType {{ ansible_os_family }}
</DistributionType>

Now we will call this jinja temple inside the playbook using temple module . Lets create the playbook as below 

[unixchips@unixchips-server ~]$ cat templete.yml 
--- # Testing the J2 templete module
- hosts: localhost,ansible-test
  sudo: yes
  gather_facts: yes
  vars:
    username: unixchips
    password: Onm0bile
    connectionType: ssh
  tasks:
    - name: install the test config file 
      template: src=/etc/ansible/files/test.conf.j2 dest=/home/unixchips/test.conf owner=unixchips group=unixchips


[unixchips@unixchips-server ~]$ ansible-playbook templete.yml 

PLAY [localhost,ansible-test] *****************************************************************************************************************

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

TASK [install the test config file] ***********************************************************************************************************
changed: [localhost]
changed: [unixchips]

PLAY RECAP ************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
unixchips                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

if we check the test.conf in both the nodes we can see the output as below

in localhost ( ansible-server)
------------------------------------------------
[unixchips@unixchips-server ~]$ cat test.conf
# configuration file for testing

<connectivity>
  connection type ssh
</connectivity>


<accountInfo>
username unixchips
password Onm0bile
</accountInfo>

<system_info>
DistributionType RedHat
</DistributionType>

in client ( unixchips)
-----------------------------------------
unixchips@unixchips:~$ cat test.conf 
# configuration file for testing 

<connectivity>
  connection type ssh
</connectivity>


<accountInfo>
username unixchips
password Onm0bile
</accountInfo>

<system_info>
DistributionType Debian
</DistributionType>




We have many more modules which can be useful along with ansible for system operations and i will discus that in another part

Thank you for reading