help: install/upgrade a list of RPMs

139 views
Skip to first unread message

张斌

unread,
Dec 14, 2015, 11:40:52 PM12/14/15
to Ansible Project

 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

cmacrae

unread,
Dec 15, 2015, 7:30:56 AM12/15/15
to Ansible Project
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 :)

Kind Regards,
Calum

张斌

unread,
Dec 15, 2015, 11:14:17 AM12/15/15
to Ansible Project
 
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

Evgen Morokin

unread,
Dec 15, 2015, 12:00:05 PM12/15/15
to Ansible Project
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"

Vince Skahan

unread,
Dec 15, 2015, 12:00:48 PM12/15/15
to Ansible Project
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


cmacrae

unread,
Dec 15, 2015, 12:03:49 PM12/15/15
to Ansible Project
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

Dick Davies

unread,
Dec 16, 2015, 6:36:33 AM12/16/15
to ansible list
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.
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/98fea921-bbf5-461e-8e3f-3a7b5b3c75c6%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

张斌

unread,
Dec 16, 2015, 10:54:56 AM12/16/15
to Ansible Project, di...@hellooperator.net
  
  Ah, cool,  state=latest, thanks guys!!
Reply all
Reply to author
Forward
0 new messages