Wednesday, February 20, 2019

introduction to ansible

Advertisements

Ansible is an automation tool or a configuration management tool. Mostly used by linux system administrators. Usually admins use ansible to deploy same things to many servers. It saves a lot of time and it doesn't need any agents to be installed on the remote servers.

Ansible has mainly 3 parts

1. yaml (the language playbooks will be written) yaml stands for yet another programming language
2. engine
3. tower (UI for Ansible management)

These are the main fundamental units in Ansible:
Inventory: Inventory file will have the servers on which the mentioned tasks to be executed. We can have groups in the inventory file. For example, if you have a lot of webservers, you can create a group webserver and put their hostnames or ip addresses in that.

Tasks : smallest executable unit in ansible
Playbook : A group of tasks specified with the inventory they need to run on.

So here we will create a inventory file named hosts. In this example, I have a group named git_server and there is one host with the ip address 172.31.20.156. I am using the user ansible with the specified key.

[ansible@ip-172-31-20-156 ~]$ cat hosts
[git_server]
172.31.20.156

[all:vars]
ansible_user=ansible
ansible_ssh_private_key_file=~/.ssh/id_rsa

Now we will check all the servers listed in the invetory files are up or not.
[ansible@ip-172-31-20-156 ~]$ ansible all -i hosts -m ping
172.31.20.156 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
we got response for pong. Which means the servers are up.

If you don't want to check the status of all the hosts mentioned in the inventory file but the hosts in the git_server group, you can do that also.
[ansible@ip-172-31-20-156 ~]$ ansible git_server -i hosts -m ping
172.31.20.156 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

You can check the uptime of the hosts in the groups also.
[ansible@ip-172-31-20-156 ~]$ ansible git_server -i hosts -m command -a "uptime"
172.31.20.156 | SUCCESS | rc=0 >>
 13:50:35 up 35 min,  2 users,  load average: 0.00, 0.01, 0.05

We can check the current ansible parameters of the localhost by using the following command.
[ansible@ip-172-31-20-156 ~]$ ansible localhost  -m setup

Now we will install git on the servers in the group git_server.
For that we need to use the module yum and pass name and state are variables to the ym module. We use -b options to rum the command as root. (sudo user)

[ansible@ip-172-31-20-156 ~]$ ansible git_server -i hosts -m yum -a "name=git state=present" -b
172.31.20.156 | SUCCESS => {
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.unifiedlayer.com\n * epel: mirrors.kernel.org\n * extras: centos.unixheads.org\n * updates: mirrors.kernel.org\nResolving Dependencies\n--> Running transaction check\n---> Package git.x86_64 0:1.8.3.1-6.el7_2.1 will be installed\n--> Processing Dependency: perl-Git = 1.8.3.1-6.el7_2.1 for package: git-1.8.3.1-6.el7_2.1.x86_64\n--> Processing Dependency: perl(Term::ReadKey) for package: git-1.8.3.1-6.el7_2.1.x86_64\n--> Processing Dependency: perl(Git) for package: git-1.8.3.1-6.el7_2.1.x86_64\n--> Processing Dependency: perl(Error) for package: git-1.8.3.1-6.el7_2.1.x86_64\n--> Processing Dependency: libgnome-keyring.so.0()(64bit) for package: git-1.8.3.1-6.el7_2.1.x86_64\n--> Running transaction check\n---> Package libgnome-keyring.x86_64 0:3.8.0-3.el7 will be installed\n---> Package perl-Error.noarch 1:0.17020-2.el7 will be installed\n---> Package perl-Git.noarch 0:1.8.3.1-6.el7_2.1 will be installed\n---> Package perl-TermReadKey.x86_64 0:2.30-20.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package                Arch         Version                   Repository  Size\n================================================================================\nInstalling:\n git                    x86_64       1.8.3.1-6.el7_2.1         base       4.4 M\nInstalling for dependencies:\n libgnome-keyring       x86_64       3.8.0-3.el7               base       109 k\n perl-Error             noarch       1:0.17020-2.el7           base        32 k\n perl-Git               noarch       1.8.3.1-6.el7_2.1         base        53 k\n perl-TermReadKey       x86_64       2.30-20.el7               base        31 k\n\nTransaction Summary\n================================================================================\nInstall  1 Package (+4 Dependent packages)\n\nTotal download size: 4.6 M\nInstalled size: 23 M\nDownloading packages:\n--------------------------------------------------------------------------------\nTotal                                              4.2 MB/s | 4.6 MB  00:01     \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : 1:perl-Error-0.17020-2.el7.noarch                            1/5 \n  Installing : libgnome-keyring-3.8.0-3.el7.x86_64                          2/5 \n  Installing : perl-TermReadKey-2.30-20.el7.x86_64                          3/5 \n  Installing : git-1.8.3.1-6.el7_2.1.x86_64                                 4/5 \n  Installing : perl-Git-1.8.3.1-6.el7_2.1.noarch                            5/5 \n  Verifying  : perl-Git-1.8.3.1-6.el7_2.1.noarch                            1/5 \n  Verifying  : perl-TermReadKey-2.30-20.el7.x86_64                          2/5 \n  Verifying  : libgnome-keyring-3.8.0-3.el7.x86_64                          3/5 \n  Verifying  : 1:perl-Error-0.17020-2.el7.noarch                            4/5 \n  Verifying  : git-1.8.3.1-6.el7_2.1.x86_64                                 5/5 \n\nInstalled:\n  git.x86_64 0:1.8.3.1-6.el7_2.1                                                \n\nDependency Installed:\n  libgnome-keyring.x86_64 0:3.8.0-3.el7  perl-Error.noarch 1:0.17020-2.el7     \n  perl-Git.noarch 0:1.8.3.1-6.el7_2.1    perl-TermReadKey.x86_64 0:2.30-20.el7 \n\nComplete!\n"
    ]
}

You can see that ansbile installed git on the specified sever.
[ansible@ip-172-31-20-156 ~]$ rpm -qa | grep git
net-tools-2.0-0.17.20131004git.el7.x86_64
git-1.8.3.1-6.el7_2.1.x86_64
python-pillow-2.0.0-19.gitd1c6db8.el7.x86_64
crontabs-1.11-6.20121102git.el7.noarch
linux-firmware-20160830-49.git7534e19.el7.noarch

Now let us uninstall it.
[ansible@ip-172-31-20-156 ~]$ ansible git_server -i hosts -m yum -a "name=git state=absent" -b
172.31.20.156 | SUCCESS => {
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Loaded plugins: fastestmirror\nResolving Dependencies\n--> Running transaction check\n---> Package git.x86_64 0:1.8.3.1-6.el7_2.1 will be erased\n--> Processing Dependency: git = 1.8.3.1-6.el7_2.1 for package: perl-Git-1.8.3.1-6.el7_2.1.noarch\n--> Running transaction check\n---> Package perl-Git.noarch 0:1.8.3.1-6.el7_2.1 will be erased\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package          Arch           Version                    Repository     Size\n================================================================================\nRemoving:\n git              x86_64         1.8.3.1-6.el7_2.1          @base          22 M\nRemoving for dependencies:\n perl-Git         noarch         1.8.3.1-6.el7_2.1          @base          57 k\n\nTransaction Summary\n================================================================================\nRemove  1 Package (+1 Dependent package)\n\nInstalled size: 22 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Erasing    : git-1.8.3.1-6.el7_2.1.x86_64                                 1/2 \n  Erasing    : perl-Git-1.8.3.1-6.el7_2.1.noarch                            2/2 \n  Verifying  : perl-Git-1.8.3.1-6.el7_2.1.noarch                            1/2 \n  Verifying  : git-1.8.3.1-6.el7_2.1.x86_64                                 2/2 \n\nRemoved:\n  git.x86_64 0:1.8.3.1-6.el7_2.1                                                \n\nDependency Removed:\n  perl-Git.noarch 0:1.8.3.1-6.el7_2.1                                           \n\nComplete!\n"
    ]
}

Now its gone.
[ansible@ip-172-31-20-156 ~]$ rpm -qa | grep git
net-tools-2.0-0.17.20131004git.el7.x86_64
python-pillow-2.0.0-19.gitd1c6db8.el7.x86_64
crontabs-1.11-6.20121102git.el7.noarch
linux-firmware-20160830-49.git7534e19.el7.noarch
[ansible@ip-172-31-20-156 ~]$

Here we will create a playbook with a task of installing git.
[ansible@ip-172-31-20-156 ~]$ cat myinventory
[gitserver]
172.31.20.156

[all:vars]
ansible_user=ansible
ansible_ssh_private_key_file=~/.ssh/id_rsa
[ansible@ip-172-31-20-156 ~]$ cat  git.yaml
- name: Install git
  hosts: gitserver
  remote_user: ansible
  become: yes

  tasks:
  - name: Install git
    yum:  name=git state=present
[ansible@ip-172-31-20-156 ~]$ ansible-playbook -i myinventory git.yaml

PLAY [Install git] *************************************************************

TASK [setup] *******************************************************************
ok: [172.31.20.156]

TASK [Install git] *************************************************************
changed: [172.31.20.156]

PLAY RECAP *********************************************************************
172.31.20.156              : ok=2    changed=1    unreachable=0    failed=0

[ansible@ip-172-31-20-156 ~]$ rpm -qa | grep git
net-tools-2.0-0.17.20131004git.el7.x86_64
python-pillow-2.0.0-19.gitd1c6db8.el7.x86_64
crontabs-1.11-6.20121102git.el7.noarch
linux-firmware-20160830-49.git7534e19.el7.noarch
git-1.8.3.1-6.el7_2.1.x86_64
[ansible@ip-172-31-20-156 ~]$

Now we will create a playbook to install some essential softwares on a server.
[ansible@ip-172-31-20-156 ~]$ cat essentials.yaml
---
- name: Install softwares which are esessential for system administrators
  hosts: localhost
  remote_user: ansible
  become: yes

  tasks:
  - name: Install packages
    yum : name={{item}} state=latest
    with_items:
        - vim
        - wget
        - telnet
        - sysstat
        - htop

Running the playbook:
[ansible@ip-172-31-20-156 ~]$ ansible-playbook -i myinventory essentials.yaml
PLAY [Install softwares which are esessential for system administrators] *******
TASK [setup] *******************************************************************
ok: [172.31.20.156]
TASK [Install packages] ********************************************************
changed: [172.31.20.156] => (item=[u'vim', u'wget', u'telnet', u'sysstat', u'htop'])
PLAY RECAP *********************************************************************
172.31.20.156              : ok=2    changed=1    unreachable=0    failed=0

No comments:

Post a Comment

Be nice. That's all.