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
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:
`
`
Or, for a list of packages:
`
`
`
`
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
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:
`
`
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
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!!