Thursday, November 18, 2021

[SOLVED] What is the difference between two "state" option values, "present" and "installed", available in Ansible's yum module?

Issue

I have the following task in my ansible playbook:

- name: Install EPEL repo.
  yum:
    name: "{{ epel_repo_url }}"
    state: present
    register: result
    until: '"failed" not in result'
    retries: 5
    delay: 10

Another value I can pass to state is "installed". What is the difference between the two? Some documentation available here: http://docs.ansible.com/ansible/yum_module.html


Solution

They do the same thing, they are aliases to eachother, see this comment in the source code of the yum module:

# removed==absent, installed==present, these are accepted as aliases

And how they are used in the code:

if state in ['installed', 'present']:
    if disable_gpg_check:
        yum_basecmd.append('--nogpgcheck')
    res = install(module, pkgs, repoq, yum_basecmd, conf_file, en_repos, dis_repos)
elif state in ['removed', 'absent']:
    res = remove(module, pkgs, repoq, yum_basecmd, conf_file, en_repos, dis_repos)
elif state == 'latest':
    if disable_gpg_check:
        yum_basecmd.append('--nogpgcheck')
    res = latest(module, pkgs, repoq, yum_basecmd, conf_file, en_repos, dis_repos)
else:
    # should be caught by AnsibleModule argument_spec
    module.fail_json(msg="we should never get here unless this all"
            " failed", changed=False, results='', errors='unexpected state')

return res

https://github.com/ansible/ansible-modules-core/blob/devel/packaging/os/yum.py



Answered By - Zlemini