I am trying to get all the installed yum package on an RHEL machine. I can easily get it through using shell commands which is not idempotent and would like to use the yum command instead.
Shell command works fine:
- name: yum list packages
shell: yum list installed > build_server_info.config
But when I try to use the yum command it just executes but do not give any results:
- name: yum_command
action: yum list=${pkg} list=available
Since Ansible 2.5, you can also use the
package_facts
module: it will gather the list of installed packages as Ansible facts.Example from the docs:
Well, the official Ansible documentation for the yum
module
describes list as:so you're going to be out of luck with finding an idempotent
list
invocation.If you just want to suppress the
changed
output, set thechanged_when
parameter toFalse
.(Also, having the duplicate
list
parameter is suspicious.)You can't really talk about idempotence, when you are querying the current state of a machine.
"Idempontent" means that the task will ensure the machine is in the desired state no matter how many times you run a certain task.
When you query current state, you don't describe the desired state. No matter what you do, what method you use, the term "idempotent" is just not applicable.
Regarding your example, which does not give you results - you have repeated twice the same argument
list
and the task should fail (it doesn't, which looks like an Ansible quirk).To get a list of installed packages, you should use:
It saves a list of dictionaries describing each package to a variable
yum_packages
.You can then use a JSON Query Filter to get a single package (
tar
):to get a result like this:
Or only its version: