Can't get playbooks to work for creating yum repositories

I want to automate yum repository configuration using an ansible playbook. For the most part, these systems won’t have internet access and need to access an internal yum repository.

I initially copied the content of a yum playbook from this link - https://docs.ansible.com/ansible/2.5/modules/yum_repository_module.html

and copied this content:

- name: Add repository
  yum_repository:
    name: epel
    description: EPEL YUM repo
    baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/

I added the leading

--- 
- hosts: client01,

And ended up with this (baseurl modified to internal yum server) - other edits based on error messages I was getting

---
- hosts: client01
  tasks:
   - name: Add repository
      yum_repository:
       name: epel
       description: EPEL YUM repo
       baseurl: http://10.0.0.1/CentOS7-EPEL

I'm getting this error now, and not sure where to go with it from here.

[admin@kickstart ansible]$ ansible-playbook repo-playbook3.yml 
ERROR! Syntax Error while loading YAML.
  mapping values are not allowed in this context

The error appears to have been in '/etc/ansible/repo-playbook3.yml': line 5, column 21, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

   - name: Add repository
      yum_repository:
                    ^ here

(I should add that I'm new to ansible and am a little fuzzy on if this kind of thing is more appropriate for a playbook or a role.)

PG

You have an indentation error in the following:

  • name: Add repository
    yum_repository:

yum_repository should be indented to the same level as name:

  • name: Add repository
    yum_repository:

I think that might have fixed the immediate problem (I get the impression ansible is quite picky about indentation mistakes!), but I’m getting this new error:

fatal: [client01]: FAILED! => {“changed”: false, “details”: “[Errno 13] Permission denied: ‘/etc/yum.repos.d/epel.repo’”, “msg”: “Cannot open repo file /etc/yum.repos.d/epel.repo.”}

The admin user is in /etc/sudoers as being able to use sudo without entering a password. But I’m guessing you have to tell the playbook to use sudo?

What is the correct way to enter that “become-user” parameter?

I think that might have fixed the immediate problem (I get the impression
ansible is quite picky about indentation mistakes!), but I'm getting this
new error:

It's actually YAML that is strict on it, and that's because the indentation have a meaning in the language.

fatal: [client01]: FAILED! => {"changed": false, "details": "[Errno 13]
Permission denied: '/etc/yum.repos.d/epel.repo'", "msg": "Cannot open repo
file /etc/yum.repos.d/epel.repo."}

The admin user is in /etc/sudoers as being able to use sudo without
entering a password. But I'm guessing you have to tell the playbook to use
sudo?

Yes.

What is the correct way to enter that "become-user" parameter?

become_user (with an underscore) is default root so you don't need to change that.
To enable become (become_method default is sudo) you can add -b or --become on the command line or you can add "become: yes" (or true if you prefer) in the play like so:

- hosts: client01
   become: yes
   tasks:

Ok, thanks all. That did the trick. I also needed to add two other repos (CentOS-Base and CentOS-Updates), and once the first one was working correctly, it was easy to duplicate it for the next two.