Instead of installing software manually in client/server machine manually will be using ansible for that in automated way.
For example: Need to install java and Maven in multiple machin, we can use ansible.
But with ansible just do the
>> ssh setup between master and client machine where you want to do the configuration in automated way.
>> create inventory file or host file which will be understanable by ansible and it will connect to those server and perform tasks.
Creating three servers:
====================
server 1 ===> master
server 2 ,3 ==> node
Step 1: Generate ssh key on master and copy pub key of master to all node/client/slave machines.
Check if you are able to login node1 and node2 from master with ssh:
Step 2: Install ansible only on master and not on nodes
In master:
amazon-linux-extras install ansible2
check the ansible installation is done properly:
Step 3: create inventory file(either in /stc/ansible/ or in your local path-->name of the file -hosts) for mentioning the ipaddr of node machine
/etc/ansible/hosts -- is called inventory file
-- We will create a grouping
[webservers]
node1ip
node2ip
-- its possible to give domain name in inventory file but location should be placed in /etc/hosts
Step 4: Check with ping command in ansible for webservers connection
Step 5: Play with Ansible adhoc commands for better understanding
(Sample adhocs are >> Rebooting server >>managing files >> Managing Packages >> Managing user & groups >> Managing services >>Gathering facts)
Sample adhoc for java installation:
command: ansible -m yum -a 'name=java-1.8.0-openjdk state=present' webservers
get_url: ansible -m get_url -a 'url=https://downloads.apache.org/tomcat/tomcat-8/v8.5.61/bin/apache-tomcat-8. dest=/opt mode=0777' webservers
unarchive: ansible -m unarchive -a 'src=/opt/apache-tomcat-8.5.61.tar.gz dest=/opt remote_src=yes' webservers
- create install_httpd.yml and check the syntax check
After correction:
2 Run :
ansible-playbook install_httpd.yml
- Service start added in install_httpd.yml:
Or else will get the below error,
- name: sample
hosts: webservers
remote_user: root ===> remote user is root
become: true ===> allowing root user to execute this playbook, if false then permission denied to remote user for execution
- name: sample
hosts: webservers
remote_user: root
become: false
tasks:
- name: install yum : name: maven state: latest
- name: Checking command: echo 'Heloo'
Command: ansible-palybook -v ansible_practice1.yml --syntax-check
Run https://github.com/logambigaik/ansible/blob/main/install-dockerpkg-in-ansible.yml to fix the issue.
Instal docker with pip -for docker login issue
- hosts: localhost
tasks:
- name: create
docker_container:
name: webappcontainer
image: klogambigai/stackdemo:latest
state: present
exposed_ports:
- 8000:80