Table of Contents
# Introduction
How to check for a package in the system using Ansible. I use vagrant to create VMs. My example Ansible creates multiple servers here. I will check the Apache package installed in Centos. Now, let’s go Ansible check package installed in Linux.
Ansible check package installed
Ansible file and folder
[vagrant@ansible_controller ~]$ tree .
.
├── ansible
│ ├── ansible.cfg
│ └── hosts
└── check-package.yml
We use the ansible module package_facts
Ansible script
---
- hosts: apache-server
become: yes
tasks:
- name: "Check if APACHE is installed"
package_facts:
manager: "auto"
- name: "Apache result"
debug:
msg: "Apache found"
when: "'httpd' in ansible_facts.packages"
- name: "Apache result"
debug:
msg: "Apache NOT found"
when: "'httpd' not in ansible_facts.packages"
Ansible run command to check
[vagrant@ansible_controller ~]$ ansible-playbook -i ansible/hosts check-package.yml
The output terminal
[vagrant@ansible_controller ~]$ ansible-playbook -i ansible/hosts check-package.yml
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
PLAY [apache-server] ***************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok: [server1]
TASK [Check if APACHE is installed] *********************************************************************************************************************************************************************************************************
ok: [server1]
TASK [Apache result] ************************************************************************************************************************************************************************************************************************
ok: [server1] => {
"msg": "Apache found"
}
TASK [Apache result] ************************************************************************************************************************************************************************************************************************
skipping: [server1]
PLAY RECAP **********************************************************************************************************************************************************************************************************************************
server1 : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
I will remove the apache package on server1 apache.
[vagrant@server1 ~]$ sudo yum remove httpd -y
Ansible runs the command to check again:
[vagrant@ansible_controller ~]$ ansible-playbook -i ansible/hosts check-package.yml
The output terminal
[vagrant@ansible_controller ~]$ ansible-playbook -i ansible/hosts check-package.yml
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
PLAY [apache-server] ***************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok: [server1]
TASK [Check if APACHE is installed] *********************************************************************************************************************************************************************************************************
ok: [server1]
TASK [Apache result] ************************************************************************************************************************************************************************************************************************
skipping: [server1]
TASK [Apache result] ************************************************************************************************************************************************************************************************************************
ok: [server1] => {
"msg": "Apache NOT found"
}
PLAY RECAP **********************************************************************************************************************************************************************************************************************************
server1 : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
Conclusion
You have to use the Ansible check package installed in Linux. I hope will this your helpful. Thank you for reading the DevopsRoles page!
But how to check if a “package pattern” exists?
Eg: “openjdk-17-jdk-headless” is installed, but I just want to check for “openjdk”, any version.
Pattern match needed.
Hi @John,
You can use shell command is a pipeline to check for “openjdk”, any version.