DEV Community

Kannan
Kannan

Posted on • Updated on

Ansible

Ansible Installation

  • Ansible Installation steps on Ubuntu machine
sudo apt update
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
apt install ansible -y
sudo apt install ansible -y
ansible --version
Enter fullscreen mode Exit fullscreen mode

If not able to get the ansible version follow the below steps

Image description

  • Virtual Machine setup

Download and install kvm and install ubuntu 22.04 server edition and centos 7 minimal server with 1GB RAM.

https://ubuntu.com/download/server#downloads
http://isoredirect.centos.org/centos/7/isos/x86_64/

  1. create a new virtual machine

  2. Select Local install media and select the downloaded ISO Package.

  3. set memory as "1024" CPU as "1"

  4. set Disk image as "25" modify the name and Click Finish to complete the process.

  5. Once done select the "Network&Hostname" Enable the Ethernet port.create the user account and set the root password.
    Follow the below cmds to assign different IP when clone

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 Config for target machine

  • Enter the inventory and hosts entry on the ansible machine.

Image description

  • Get the ssh keygen (.ssh) and find the target machine IP using (hostname -I)

  • Copy the source machine ssh public key to the target machine

ssh-copy-id -i id_rsa.pub kannan@192.168.122.133
Enter fullscreen mode Exit fullscreen mode
  • If you want to change the hostname of the target machine go with the below cmd
sudo hostnamectl set-hostname ubuntu-node-1
exec bash
Enter fullscreen mode Exit fullscreen mode
  • Ping the target machine from ansible machine
ansible all -m ping
Enter fullscreen mode Exit fullscreen mode
  • Find the OS version of the target machine from ansible machine
ansible all -a "cat /etc/os-release"
Enter fullscreen mode Exit fullscreen mode
  • To provide root privileges for the users

Image description

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
Enter fullscreen mode Exit fullscreen mode
sudo su-
usermod -aG sudo kannan
sudo su - kannan
Enter fullscreen mode Exit fullscreen mode
  • Let install Apache server(httpd) using adhoc cmds
ansible ubuntu -i hosts -m apt -a "name=apache2 state=present" --sudo
ansible all --list-hosts

ansible all -m package -a "name=httpd state=present"
Enter fullscreen mode Exit fullscreen mode
ansible centos -m package -a "name=httpd state=present" -b

 ansible centos -m ansible.builtin.service -a "name=httpd state=started" -b

ansible ubuntu -m file -a "path=/home/kannan/test mode=755 state=directory" -b

Enter fullscreen mode Exit fullscreen mode
  • Once Apache is installed on the Target machine bydefault its not active we need to enable,start and find the staus from ansible machine.
systemctl enable httpd
systemctl start httpd
systemctl status https
Enter fullscreen mode Exit fullscreen mode

Now apache is running on the target machine.
We have done these all from ansible machine and get the output on target machine via ansible.

Top comments (0)