DEV Community

technonotes-hacker
technonotes-hacker

Posted on

Ansible - Part 1

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
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Image description

Image description

Image description

Virtual Machine Manager

Image description

Image description

Image description

Image description

Image description

Image description

Image description

Removed the space while giving the name of the VM.

Image description

Image description

PLEASE Check the ERROR

Image description

Image description

Image description

This may take sometime.

Image description

Image description

Image description

Image description

Image description

Make it ON

Image description

Image description

Image description

Image description

Enable both options

Image description

Image description

Image description

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
Enter fullscreen mode Exit fullscreen mode

Ansible Configurations for the Target servers

/etc/ansible

Image description

SSH Key Generation

  • ssh-keygen

Image description

Image description

Get the IP of the target server

Image description

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
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

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 )

Image description

Image description

[defaults]
inverntory = hosts
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

[centos]
192.168.122.96
Enter fullscreen mode Exit fullscreen mode

Change the hostname of target machine for better understanding.

sudo hostnamectl se-hostname centos-node-1
Enter fullscreen mode Exit fullscreen mode

Image description

  • exec bash ( once you execute this , you can see the difference in the screen )

Image description

Ping the target machine from ansible source machine and check.

ansible all -m ping
Enter fullscreen mode Exit fullscreen mode

Image description

Get the OS version from ansible machine

ansible all -a "cat /etc/os-release"
Enter fullscreen mode Exit fullscreen mode

Image description

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
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Help command or Man Pages for Ansible

man ansible
-b --> execute as root
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

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
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Image description

Image description

time ansible centos -m package -a "name=httpd state=present" -b
- b ---> root user ( become TRUE )
Enter fullscreen mode Exit fullscreen mode

Image description

  • 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
Enter fullscreen mode Exit fullscreen mode

Image description

  • 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
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

List all servers in the Ansible

ansible all --list-hosts
Enter fullscreen mode Exit fullscreen mode

Image description

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/
Enter fullscreen mode Exit fullscreen mode

Image description

Errors

Unable to get the version,

Image description

https://askubuntu.com/questions/1460877/gitgit-ansible-version-error-ansible-requires-the-locale-encoding-to-be-u

Image description

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

BEFORE

installed using command : sudo apt install vim
Enter fullscreen mode Exit fullscreen mode

AFTER

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"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)