Q: How to add repository by ansible playbook I want to do below command by ansible playbook
add-apt-repository cloud-archive:rocky

after some search I find this solution
- name: Add repositories
  apt_repository: repo='cloud-archive:rocky' state=present

But it does not work.What is the correct way to do that command by ansible playbook?

A: You can use Ansible's Shell module passing the -y switch to add-apt-repository
- name: Ensure to add repository
  shell: sudo add-apt-repository cloud-archive:rocky -y


A: For ubuntu this repository module can be used, this does work and you can proceed with rest of the installation
 - name: Add php repository
   apt_repository:
      repo: ppa:ondrej/php
      state: present

Ref: https://docs.ansible.com/ansible/2.9/modules/apt_repository_module.html
Shell can definately do it but when you start working on production environment having specific module for the job is advisable to avoid any security/best practices violations
