Need help on Ansbile yum_repository bultin module

I am creating .repo files using the below code in a yum ansible role. Though I mentioned sslverify as true/false it’s adding as 0 or 1. But in Ansible docs also it’s mentioned as true or false.

Also, it’s adding an extra blank line at the end and also not adding parameters in the order I mentioned.

- name: Create YUM repositories from dictionary
  ansible.builtin.yum_repository:
    name: "{{ item.value.name }}"
    description: "{{ item.value.name }}"
    baseurl: "{{ item.value.repository_server }}{{ item.value.baseurl }}"
    gpgcheck: "{{ item.value.gpgcheck }}"
    gpgkey: "{{ (item.value.repository_server + item.value.gpgkey) if item.value.gpgkey is defined else omit }}"
    enabled: yes
    sslverify: "{{ item.value.sslverify | ternary('yes', 'no') }}"
  loop: "{{ rockyglobal | dict2items }}"
  loop_control:
    loop_var: item
    label: "{{ item.key }}" 

variables:

rockyglobal:
  testrocky:
    name: testrocky
    repository_server: https://example.com/artifactory/
    baseurl: scgp-repos-rpm-rocky92
    gpgcheck: yes
    gpgkey: scgp-repos-key
    sslverify: true
  testrocky_2023_q2:
    name: testrocky_2023_q2
    repository_server: https://example.com/artifactory/
    baseurl: scgp-repos-rpm-x86_64/os/
    gpgcheck: yes
    gpgkey: scgp-repos-key
    sslverify: true

Result:

cat testrocky_2023_q2.repo
[testrocky_2023_q2]
baseurl = https://example.com/artifactory/scgp-repos-rpm-x86_64/os/
enabled = 1
gpgcheck = 1
gpgkey = https://example.com/artifactory/scgp-repos-key
name = testrocky_2023_q2
sslverify = 1

Please Advise!

The module does the conversion automatically. I believe both integer and bool versions are valid.

In the documentation it is explicitly mentioned that the parameters are ordered alphabetically, see ansible.builtin.yum_repository module – Add or remove YUM repositories — Ansible Community Documentation.

In any case, none of these should affect the correctness of the generated configuration. Is that not a case?

If you want the style of the configuration to be generated differently you would have to use a different module, perhaps ini, or a template.

Thanks a lot, @mkrizek. Now I am seeing about the order of attributes. But not sure if both integer and bool versions are valid or not since I am not able to find documentation about that.

Also, is there any specific reason it’s adding an extra blank line at the end?

That is the behavior of configparser.RawConfigParser that implements the underlying functionality of handling INI files in the module - the blank line is a delimiter between individual sections; it does not seem to care for a situation where there is only one section and puts the delimiter in such a case as well.

1 Like