Ansible - Part 1
Published: 2023-10-25 23:58:39

### 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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k6v4qbzc919ctkjvl3og.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7sz06zech3joi62onnug.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2fd2drarvvosrcks34xc.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nwivlduryginr4sru217.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pokz8l3xw1f5x6wzqynf.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fnxay872mzfz48obv90l.png)

### Virtual Machine Manager

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zcwqndj08bs24sc5wmc2.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5oc5jinpjev9sskn0ygy.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/26reytdxknru75r7os39.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/922850pmnnyytw9hfp49.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dvbkscizmfpn2uwsgz2c.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/repmiyh1lx1voubhcsk6.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgtjtu0y42j05pbeic01.png)

> **Removed the space while giving the name of the VM.**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/djbwb79wdi92c0jaq5f7.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ljd31v9y5vt2z1uh3mt0.png)

> **PLEASE Check the ERROR**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cv9yay6j80g7iafaxwby.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a4jrrimc1knw8pudr32j.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w3w4rnysjao0qf0rhebm.png)

> **This may take sometime.**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nfucehas19d6aat9nclb.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/22fimo28dwjxr9lux4qq.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/igrscrduvukken3fcc8e.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5escgy80zhzbd0x83ozw.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o301psoehmush12nhq1n.png)

> **Make it ON**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxm7d1zbgik2wx5eghqr.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ecu3jg88na7yklvbk3fa.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5v2oi24d36lj69wud05e.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dlr26gmzdg36o0ui0d5c.png)

> **Enable both options**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x54pd35rvvpmq1vqmenf.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nfcyuo3k761eax4eci1u.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmrxb8oioirxpl00b8pu.png)

> **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**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdhv60s2molew3rpb3s8.png)

> **SSH Key Generation**

- ssh-keygen

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xo5fsq75n5wfr0zwkba8.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vq2687b5r521tmo7gin6.png)

> **Get the IP of the target server**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3vezeo0yfkxvv3j5js0x.png)

> **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
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u98hy1m6rn7gda8nptdq.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pxre978fvvyslsk69doi.png)

> **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n37ih07ns7irdald3ixy.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/umh18nxrczlry8pycebg.png)

```
[defaults]
inverntory = hosts
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1czrvz7zz963m7pb5ova.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16h9bqoe4tmxvdcgzvlu.png)

```
[centos]
192.168.122.96
```

> **Change the hostname of target machine for better understanding.**

```
sudo hostnamectl se-hostname centos-node-1
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uu5hir48szegs0m7iyt2.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/chs9ohckova7f4z35vnj.png)

> **Ping the target machine from ansible source machine and check.**

```
ansible all -m ping
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wglhjpdz2a59dl3d231k.png)

> **Get the OS version from ansible machine**

```
ansible all -a "cat /etc/os-release"
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l10ru5lpwypykecn160n.png)

> **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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0f8h1zctj89zpafs7qap.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ekzwdl6ui2trl6i8gsnf.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xixoho40wo7gtamwemqr.png)

> **Help command or Man Pages for Ansible**

```
man ansible
-b --> execute as root
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m00vw4ay6b1sit9ahyl9.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xd8k7eqq47kute7uk16q.png)

> **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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3y5d02isrwt4gg6s5yb4.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cqceqsqhwo2ljajq699w.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l2c9r53jvvtggli54g4e.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6gt9rrhcvc4w9cdo0tsl.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mpw1684ohtmu0f2kxrci.png)

- 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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t9dzmdfyprfu2o3pb7j7.png)

- 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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7xkqes3n5vl7t0gchlhu.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l3jyj2kospkz1j73u8lj.png)

> **List all servers in the Ansible**

```
ansible all --list-hosts
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f8j3ks5fb63h94xl07jp.png)


> **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/
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqwo75033q0b9msab5cc.png)

### Errors

Unable to get the version,

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/13szsnmw39q0c299qudu.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bysi8k12o0zn82za33rp.png)

```
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
```

![BEFORE](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/efkginrijolp3xpdl8eo.png)

```
installed using command : sudo apt install vim
```

![AFTER](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d19xhsbeaurypgi1u75g.png)

### 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"
```