Ansible - Part 1
technonotes-hacker
Posted on October 25, 2023
Lets Install Ansible
sudo apt install software-properties-common
sudo add-apt-repository --yes ppa:ansible/ansible
apt update
sudo apt install ansible
or
If any issues faced , just follow below steps according to the error message which is received in your screen.
sudo apt install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt remove ansible
sudo apt -y install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt install ansible -y
ansible --version
sudo apt remove --purge ansible
sudo add-apt-repository --remove ppa:ansible/ansible
sudo apt update
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo cp /etc/apt/trusted.gpg /etc/apt/trusted.gpg.d
sudo apt update
sudo apt install ansible
ansible --version
Virtual Machine Manager
Removed the space while giving the name of the VM.
PLEASE Check the ERROR
This may take sometime.
Make it ON
Enable both options
Required "To enable different IP when its CLONED"
echo -n > /etc/machine-id
rm /var/lib/dbus/machine-id
ln -s /etc/machine-id /var/lib/dbus/machine-id
Ansible Configurations for the Target servers
/etc/ansible
SSH Key Generation
- ssh-keygen
Get the IP of the target server
Add the source Ansible Machine Public key to the target servers here I have only CENTOS
sh-copy-id -i id_rsa.pub sathishpy1808@192.168.122.96
Edit the files in the Ansible machine to say these are the target machines or slave machines. ( Even this can be done in HOME directory too )
[defaults]
inverntory = hosts
[centos]
192.168.122.96
Change the hostname of target machine for better understanding.
sudo hostnamectl se-hostname centos-node-1
- exec bash ( once you execute this , you can see the difference in the screen )
Ping the target machine from ansible source machine and check.
ansible all -m ping
Get the OS version from ansible machine
ansible all -a "cat /etc/os-release"
To avoid users to enter SUDO each time , add the existing user to the SUDO.
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
sathishpy1808 ALL=(ALL) NOPASSWD: ALL
Help command or Man Pages for Ansible
man ansible
-b --> execute as root
Lets Install once HTTPD server i.e Apache using ADHOC command ( Not using PLAY book )
ansible centos -m package -a "name=httpd state=present" --> Throwing error like " need root access to run the command.
ansible centos -m package -a "name=httpd state=present" -b --> again it fails with some other permission
time ansible centos -m package -a "name=httpd state=present" -b
- b ---> root user ( become TRUE )
- Now check whether Apache installed in Target server.
ssh sathishpy1808@192.168.122.96
Last login: Thu Oct 26 04:20:47 2023 from 192.168.122.1
[sathishpy1808@centos-node-1 ~]$ systemctl status httpd
- Installed but NOT STARTED
Lets start from Source Ansible machine ONLY , you should not touch the target servers at any cost. All changes must be done only in source machine i.e ansible machine.
Actually why it's not started ? it needs to call the modules NEXT for starting the httpd in Centos , which we can give from source ansible machine.
time ansible centos -m ansible.builtin.service -a "name=httpd state=started" --> NOT WORKING
time ansible centos -m ansible.builtin.service -a "name=httpd state=started" -b
List all servers in the Ansible
ansible all --list-hosts
File creation or Directories in all the target systems
time ansible centos -m file -a "path=/home/sathishpy1808/test mode=755 state=directory" -b
ssh sathishpy1808@192.168.122.96
stat -c %a test/
Errors
Unable to get the version,
sudo nano /etc/default/locale
LANG="en_US.UTF-8"
LC_CTYPE="en.US.UTF-8"
sudo update-locale LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8
Important Points :
- Download and install kvm and install ubuntu 22.04 server edition and centos 7 minimal server with 1GB RAM / 2CPU
https://ubuntu.com/download/server#downloads
http://isoredirect.centos.org/centos/7/isos/x86_64/
- All changes should be ONLY in Ansible Machine ( don't touch the Target machine )
https://docs.ansible.com/ansible/latest/command_guide/intro_adhoc.html#managing-services
- Editor always goes with NANO , how to change ?
sudo update-alternatives --config editor
installed using command : sudo apt install vim
URL's
https://docs.ansible.com/ansible/latest/command_guide/intro_adhoc.html
https://www.devopsschool.com/tutorial/ansible/ansible-linux-adhoc-commands.html#Program1
Commands to Recollect
ansible all -m ping
sudo apt-add-repository ppa:ansible/ansible
sudo apt install ansible -y
sudo apt update
sudo apt install software
sudo add-apt-repository --remove ppa:ansible/ansible
sudo update-alternatives --config editor
ansible all --list-hosts
ansible all -m package -a "name=httpd state=present"
ansible ubuntu -m file -a "path=/home/kannan/test mode=755 state=directory" -b
ansible webservers -m ansible.builtin.service -a "name=httpd state=started"
Posted on October 25, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.