How do Linux Package Managers works?
Every Linux Operating system allow us to install any software using package managers such as yum, dnf, apt, deb or apk any other option.
Here I've explored more details about this package mangers how they are working. If we take RedHat flavor Linux systems such as CentOS, SuSe, RHEL uses actually RPM as package manager. But the CLI clients are available such as yum(Yellowdog updater modified) and in the latest versions using improved yum that is dnf command utility which is known as "Dandified Yum".
The service or systemctl commands
After installation we need to start, stop or restart or check status that service using systemctl or service command as per the System availability.
First we will experiment with package managers dnf usage in Ansible. We can target simple two playbooks where you should have inventory groups defined for webserver, database.
To check already httpd is installed on a machine:
Ansible package manager modules connection with front-backend utilities |
First we will experiment with package managers dnf usage in Ansible. We can target simple two playbooks where you should have inventory groups defined for webserver, database.
Prerequisite:
The inventory file content with the webserver and database groups as following[ansible@master qa]$ cat hostqa.yml all: children: qa: children: qawebserver: hosts: node[1:2]-vt: qadbserver: hosts: node3-vt: qalb: hosts: node4-vt:
How to install packages using ansible yum module?
The Ansible yum module is allow us to install the packages on the target hosts. where you can tell the action using state.
--- # File: nginx_yum_installation.yml - name: install and start nginx hosts: "{{ targets | default ('webserver') }}" become: yes tasks: - name: install nginx yum: name: nginx state: present update_cache: true - name: start nginx service: name: nginx state: startedThe execution of the above playbook output as:
ansible-playbook nginx_yum_installation.yml
Ansible yum module for install nginx and start the service |
How to uninstall package using ansible yum module?
The following ansible playbook code will stop the service and remove the package from the target box.--- # Filename: nginx_stop_yumremove.yml - name: stop and remove nginx hosts: "{{ targets | default('localhost') }}" become: yes gather_facts: no tasks: - name: stop nginx server service: name: nginx state: stopped - name: remove nginix yum: name: nginx state: absentExecution outcome
ansible-playbook -e targets=qawebserver nginx_stop_yumremove.yml --check ansible-playbook -e targets=qawebserver nginx_stop_yumremove.yml
Ansible yum module to remove nginx package |
How to install packages using ansible apt module?
The Ansible apt module is allow us to install the packages on the target hosts. where you can tell the action using state.
--- # Filename: nginx_apt_installation.yml - name: install and start nginx hosts: "{{ targets | default ('loadbalancer') }}" become: yes tasks: - name: install nginx apt: name: nginx state: present update_cache: false - name: start nginx service: name: nginx state: startedThe execution of the above playbook output as:
ansible-playbook nginx_apt_installation.yml
How to install a package with ansible dnf module?
If you are working on CentOs8 or Oracle Linux 8 or RHEL 8 then you can use dnf module. The web group target to install nginx webserver, and database target to install with mysql database.--- - hosts: webserver tasks: - name: install nginx dnf: name=nginx state=present update_cache=truePackage manager module can be executed on the target machine with ansible user generally, but it requires sudo access so we need to use become parameter value as 'yes'. In adhoc command execution we can use -b or --become option.
ansible webserver -m yum -a "name=httpd state=latest" -b
How to list out the package is installed?
The yum module can be used to determine if a package is available and installed on the managed node (e.g. the target VM). This ansible module execution is similar to the `yum info` command in CLI. Let's examine "nginx" installed on the web boxes with the followng playbook.- name: List out the yum installed packages hosts: "{{ targets | default ('loadbalancer') }}" gather_facts: false #remote_user: root become: yes tasks: - name: determine if a package is installed yum: list: "{{ package }}" register: out - debug: msg: - "package: {{ package }}" - "yumstate: {{ out.results[0].yumstate }}" - "yumstate: {{ out.results[1].yumstate }}" - "version: {{ out.results[1].version }}"Executed with the following command :
ansible-playbook -e targets=node1-vt -e package=nginx yum_list.ymlThe screen screen will look like this:
Ansible yum module listing out about a package |
To check already httpd is installed on a machine:
rpm -qa|grep httpd
No comments:
Post a Comment