# \[anisible\]Install package

**URL:** <https://forum.ansible.com/t/anisible-install-package/38327>\
**Category:** Ansible Project\
**Tags:** rhel\
**Created:** [August 14, 2023, 9:21am UTC](https://forum.ansible.com/t/anisible-install-package/38327 "2023-08-14T09:21:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [August 14, 2023, 9:21am UTC](https://forum.ansible.com/t/anisible-install-package/38327/1 "2023-08-14T09:21:06Z")

</div>

Hi folks

I want to optimize the code here

Existing code:  
package:  
name: “{{ item }}”  
state: present  
with\_items:

- vim
- lvm2
- xyz  
when: is\_USA

Now I don’t want to install xyz PACKAGE for RHEL 9 server only then I ve to rewrite the separate code for xyz package like below or any optimization is possible in the existing code

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [August 14, 2023, 11:06am UTC](https://forum.ansible.com/t/anisible-install-package/38327/2 "2023-08-14T11:06:58Z")

</div>

Here’s _my_ solution. Whether you wish to go there is a different question.

```auto
---
- name: Prady A example 1
  hosts: localhost
  vars:
    is_usa: true
  tasks:
    - name: Install packages with logical list
      ansible.builtin.package:
        # requires "ansible-galaxy collection install utoddl.logical"
        name: "{{ rpms | utoddl.logical.logical }}"
        state: present
      vars:
        rpms:
          - vim
          - lvm2
          - if:
              - and:
                  - "{{ is_usa }}"
                  - "{{ ansible_distribution_major_version | int <= 9 }}"
              - xyz

```

See [https://galaxy.ansible.com/utoddl/logical](https://galaxy.ansible.com/utoddl/logical) for details and examples.

---

<div class="post-metadata">

**Author:** ![Prady\_A](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/prady_a/32/4792_2.png) [@Prady\_A](https://forum.ansible.com/u/Prady_A)\
**Post date:** [August 14, 2023, 1:07pm UTC](https://forum.ansible.com/t/anisible-install-package/38327/3 "2023-08-14T13:07:42Z")

</div>

Super cool . That’s what I was try in  
pseudo code…

Thanks a ton
