# help: install/upgrade a list of RPMs

**URL:** <https://forum.ansible.com/t/help-install-upgrade-a-list-of-rpms/20015>\
**Category:** Ansible Project\
**Created:** [December 15, 2015, 4:22am UTC](https://forum.ansible.com/t/help-install-upgrade-a-list-of-rpms/20015 "2015-12-15T04:22:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![user46](https://avatars.discourse-cdn.com/v4/letter/u/c37758/32.png) [@user46](https://forum.ansible.com/u/user46)\
**Post date:** [December 15, 2015, 4:22am UTC](https://forum.ansible.com/t/help-install-upgrade-a-list-of-rpms/20015/1 "2015-12-15T04:22:43Z")

</div>

hi,

i’m new to ansible. i want to install a list of RPMs, and upgrade if already installed, otherwise just install it.

How to do this with ansible?

Thanks

---

<div class="post-metadata">

**Author:** ![cmacrae](https://avatars.discourse-cdn.com/v4/letter/c/2acd7d/32.png) [@cmacrae](https://forum.ansible.com/u/cmacrae)\
**Post date:** [December 15, 2015, 12:30pm UTC](https://forum.ansible.com/t/help-install-upgrade-a-list-of-rpms/20015/2 "2015-12-15T12:30:56Z")

</div>

Hey,

I’d recommend you read fully through the ‘[Getting Started](http://docs.ansible.com/ansible/intro_getting_started.html)’ guide, and carry on reading through the other docs - reading these and implementing/playing with what they teach is the quickest, most efficient way to learn.

That said, if the RPMs you’re talking about are hosted in a repository, you can use the ‘yum’ module (or ‘package’, if you’re running Ansible 2.0 - not yet released).

See [here](http://docs.ansible.com/ansible/yum_module.html) for the yum modules documentation, it has a ‘state’ value.  
Setting this to ‘latest’ would do what you’re wanting to achieve, like so:

`

- name: Ensure packages are installed and the latest available version  
yum: name=some\_package state=latest

`

Or, for a list of packages:

`

`

- name: Ensure packages are installed and the latest available version  
yum: name=“{{ item }}” state=latest  
with\_items:
- some\_package
- some\_other\_package
- a\_third\_package

`

`

This example shows [iterating over a list of values, using ‘with\_items’.](http://docs.ansible.com/ansible/playbooks_loops.html)

Hope this helps - make sure you read through the docs and try and learn these things yourself 🙂

Kind Regards,  
Calum

---

<div class="post-metadata">

**Author:** ![user46](https://avatars.discourse-cdn.com/v4/letter/u/c37758/32.png) [@user46](https://forum.ansible.com/u/user46)\
**Post date:** [December 15, 2015, 4:14pm UTC](https://forum.ansible.com/t/help-install-upgrade-a-list-of-rpms/20015/3 "2015-12-15T16:14:17Z")

</div>

Thanks Calum. I already went through the docs, and googled. I know how to loop with items and install with yum.

But i want to a list of RPMs to be check and install/upgrade, not sure if there is a way to do so.

how to loop with\_items: for the three steps ? Perhaps something like the following

with\_items: {{items}}

- name: Check if Package is installed  
command: rpm -q {{item}}  
register: rpm\_check  
failed\_when: rpm\_check.rc \> 1  
changed\_when: no

- name: Install Package only if not installed  
yum: name={{item}} state=present  
when: rpm\_check.rc == 1

- name: Update Package only if installed  
yum: name={{item}} state=latest update\_cache=yes  
when: rpm\_check.rc == 0

---

<div class="post-metadata">

**Author:** ![Evgen\_Morokin](https://avatars.discourse-cdn.com/v4/letter/e/b77776/32.png) [@Evgen\_Morokin](https://forum.ansible.com/u/Evgen_Morokin)\
**Post date:** [December 15, 2015, 5:00pm UTC](https://forum.ansible.com/t/help-install-upgrade-a-list-of-rpms/20015/4 "2015-12-15T17:00:04Z")

</div>

Hi,

example from previous reply will do all stages for you:

`

- name: Ensure packages are installed and the latest available version  
yum: name=“{{ item }}” state=latest  
with\_items:
- some\_package
- some\_other\_package
- a\_third\_package

`

This will check package presence, then install it if it is not installed and update if it is. This is the key: “state=latest”

---

<div class="post-metadata">

**Author:** ![Vince\_Skahan](https://avatars.discourse-cdn.com/v4/letter/v/5e9695/32.png) [@Vince\_Skahan](https://forum.ansible.com/u/Vince_Skahan)\
**Post date:** [December 15, 2015, 5:00pm UTC](https://forum.ansible.com/t/help-install-upgrade-a-list-of-rpms/20015/5 "2015-12-15T17:00:48Z")

</div>

Perhaps you could do a little test to verify the original answer you got worked (or did not).

Install an old rpm manually  
Install a current rpm manually  
Write a playbook using ensure=latest referencing both  
You should see it updated the old one to current, and left the already current one alone

---

<div class="post-metadata">

**Author:** ![cmacrae](https://avatars.discourse-cdn.com/v4/letter/c/2acd7d/32.png) [@cmacrae](https://forum.ansible.com/u/cmacrae)\
**Post date:** [December 15, 2015, 5:03pm UTC](https://forum.ansible.com/t/help-install-upgrade-a-list-of-rpms/20015/6 "2015-12-15T17:03:49Z")

</div>

Ah okay, cool 🙂

Hmm, well, does my solution above still not stand?  
I mean, with your above pseudo code, the second task will install the package if it’s not present, in doing so, it will install the latest package.  
The third task will just ensure any packages in the list installed are at their latest available version.

This achieves exactly the same as my suggested task above, also making the initial task (the rpm command) pointless.

Kind Regards,  
Calum

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [December 16, 2015, 11:36am UTC](https://forum.ansible.com/t/help-install-upgrade-a-list-of-rpms/20015/7 "2015-12-16T11:36:22Z")

</div>

I think yours is bang on, if 张斌 really wants to keep his attempt they can  
just remove all but the 'Update Package only if installed' yum task,  
the other 2 are  
redundant.

---

<div class="post-metadata">

**Author:** ![user46](https://avatars.discourse-cdn.com/v4/letter/u/c37758/32.png) [@user46](https://forum.ansible.com/u/user46)\
**Post date:** [December 16, 2015, 3:54pm UTC](https://forum.ansible.com/t/help-install-upgrade-a-list-of-rpms/20015/8 "2015-12-16T15:54:56Z")

</div>

Ah, cool, state=latest, thanks guys!!
