help: install/upgrade a list of RPMs

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

Hey,

I’d recommend you read fully through the ‘Getting Started’ 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 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’.

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

Kind Regards,
Calum

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

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”

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

Ah okay, cool :slight_smile:

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

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.

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