Ansible check package installed in Linux

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

, , ,

About HuuPV

My name is Huu. I love technology and especially Devops Skill such as Docker, vagrant, git so forth. I likes open-sources. so I created DevopsRoles.com site to share the knowledge that I have learned. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

Leave a Reply

Your email address will not be published.

Comment moderation is enabled. Your comment may take some time to appear.

This site uses Akismet to reduce spam. Learn how your comment data is processed.