Devops Commands—> Ansible

////Installation

/* https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-ansible-on-ubuntu-18-04

sudo apt-add-repository ppa:ansible/ansible -y

sudo apt-get update && sudo apt-get install ansible -y

ansible –version
/*ansible 2.9.26 latest version

/////////Creating passwd less authentication for asible servers to host servers

/* create an user in ansible server and also on all host servers later convert those users as super users through sudo visudo

sudo adduser ansadmin
sudo visudo
ansadmin ALL=(ALL) NOPASSWD: ALL

sudo vi /etc/ssh/sshd_config
/*change the passwd authentication to ‘yes’
sudo service ssh restart

/*Now switch to ansadmin and generate a public key using ssh-keygen and copy that key to host servers using ssh-copy-id

ssh-keygen

ifconfig -a
/*to check the private ip address

ssh-copy-id @
/* to copy ssh keys to client servers

/*Now add all hosts ip addresses in below path unders specified groups

vi /etc/ansible/hosts
/* add all clients/hosts here
[Hosts Group name]
ansible_user=

ssh @< ip address>
/* now you can login to host ip w/o asking passwd

ansible -m ping -u —-> through this command we can check pings through specified user.
/* to check the pings from the clients

//////////
cat /etc/ansible/ansible.cfg
/Forks determines how many managed nodes (e.g. the target systems) a task will be run against simultaneously by deafault is set to 5 example: ansible-playbook example.yml –forks 10 /here forks is set to 10, so that each task in the playbook is executed against 10 managed nodes simultaneously.

//////////////////AD hoc commands

ansible all -a ‘any command’
/* we can execute any command in host servers

ansible -m copy -a ‘src=/path/ dest=’ -b
/* to copy a file/dir to host servers (here -b will execute as a super user)

ansible client -m apt -a “name=maven state=present” -b
/*to install packages

ansible client -m apt -a “name=maven state=absent” -b
/*to uninstall packages

///Play books

to create multiple directories

  • hosts: all become: true tasks:
    • name: Create multiple directories
      file: path={{item}} state=directory
      with_items:
    • ‘/home/vn1’
    • ‘/home/vn2’
    • ‘/home/vn3’