Importing/adding a yum .repo file using Ansible

It appears that you are correct, they don't offer what you are after. Their model is such that you would call yum_repository: 3 times, once with each of the baseurl= values you already have in your .repo file.

Thus, given your circumstance, I'd recommend just using command: to run the yum-config-manager --add-repo just as you are in shell. The only catch to that would be if yum-config-manager --add-repo= isn't idempotent, in which case you'd have to guard that command: by hand to keep it from adding that same repo file over and over with each run.


Use the shell command with the creates flag. That will skip the step if the repo file exists. You'll need to make sure you know what the repo file is called.

- name: Add CentOS_o repository
  shell: yum-config-manager --add-repo=http://example.net/mirror/centos_o.repo
  args:
    creates: /etc/yum.repos.d/centos_o.repo 

If you need to add any architecture to the url use something like

- name: Add CentOS_7_speciality repository
  shell: yum-config-manager --add-repo=http://example.net/{{ ansible_distribution | lower }}/{{ ansible_distribution_major_version }}/{{ ansible_architecture }}/
centos_o.repo
  args:
    creates: /etc/yum.repos.d/centos_o.repo 

Ansible will replace the variables with

{{ ansible_distribution | lower }} == centos
{{ ansible_distribution_major_version }} == 7
{{ ansible_architecture }} == x86_64