Wednesday, February 28, 2018

Ansible playbook for Java Installation on Ubuntu | Install Java using Ansible Playbook

1. Login to Ansible management server/machine. Create SSH keys in Ansible host machine by executing the below command: (if you already have keys created, please skip this step)
ssh-keygen 

enter three times..now you will see keys successfully created.
2.  Execute the below command on Ansible management node and copy the public key content:
 sudo cat ~/.ssh/id_rsa.pub

copy the above output.
3. Now login to target node, execute the below command to open the file
 sudo vi /home/ubuntu/.ssh/authorized_keys
type shift A and then enter now 
    and paste the key in the above file. please do not delete any existing values in this file.

4. go back to ansible mgmt node, make sure you are able to ssh from ansible mgmt node after copying the keys above:
  ssh private_ip_of_target_node
now type exit to come out of the target node.
5. Now in ansible mgmt node, now make changes in /etc/ansible/hosts file to include the node you will be installing software. Make sure you add public IP address as highlighted below in red color:
   sudo vi /etc/ansible/hosts
[My_Group]  
xx.xx.xx.xx ansible_ssh_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa  ansible_python_interpreter=/usr/bin/python3

6. make changes in playbooks as given below,
cd ~/playbooks

sudo vi installJava.yml

---
- hosts: My_Group

  tasks:
  - name: Update APT package manager repositories cache
    become: true
    apt:
      update_cache: yes

  - name: Install OpenJDK Java
    become: yes
    apt:
      name: "{{ item }}"
      state: present
    with_items:
     openjdk-8-jdk

7. Execute Ansible playbook
ansible-playbook installJava.yml

now after successfully executing, enter below command to make sure Java is installed in target node:

java -version

Note:

If you have issues in installing Java on target node, please do the following on target node to install python modules:

sudo apt-get update
sudo apt-get install python-software-properties

2 comments:

  1. Hi, in the above playbook apt need to become sudo inorder to run python-software-properties installation.

    ReplyDelete