#configure target server (ubuntu instance using ansible server)
-
in the Ansible server, install Ansible
sudo apt update
sudo apt install ansible
-
configure passwordless authentication so that the Ansible server can access target servers without any password
-
in the ansible server, generate public/private key:
ssh-keygen
this will generate public, and private keys and store them in /home/ubuntu/.ssh folder -
do the same in the target server, ssh-keygen
-
add the public key in the target server--> authorised_keys folder to implement passwordless authentication by default the .pem key details will be there in this server (keypair used while creating the server) -- overwrite this public key on that
-
now from the Ansible server, SSH into the target server
ssh target-server-private-ipv4
-
we are now logged in to the target server without a password
-
now we can write an Ansible playbook to manage the target servers
- inventory file: a simple txt file with private IP addresses of target servers
-
add the target server's private IP in this file
-
Ansible ADHOC commands
ansible -i inventory.txt all -m "shell" -a "touch devops calls"
this command will execute shell command [-m "shell" indicates use shell module to execute the later commands] [-a indicates to execute the command in target server ] -
for managing different types of servers like webserver/ db servers/ test server/ staging servers etc group them in the inventory file using []
-
at runtime give the specific server group name to configure the changes in the specific server group
here, we are creating demo files, in db servers with dbserver name and in webserver with webserver name The same is visible on the respective servers
-
we write Ansible playbook when there are multiple tasks to be performed if one or two are to then go with Ansible ad-hoc commands
-
- playbook is a Yaml.manifest file
-
run the playbook file using
ansible-playbook -i inventory.txt my-first-playbook
-
in real-time usecase ansible-playbook will be used to manage Kubernetes clusters