Create docker repository using ansible

Problem:
Error while creating a docker repository through playbook
Expected outcome:
Docker repository should be added
Foreman and Proxy versions:
2.2.5
Foreman and Proxy plugin versions:

Distribution and version:
RHEL 7
Other relevant data:
I am getting error while creating a alpine docker repository using ansible playbook. Yum repository is working fine. Here’s the yml file I tried.

    - name: "Create repository"
      theforeman.foreman.repository:
        username: "admin"
        password: "admin"
        server_url: "https:myworld.com"
        name: "dockerproduct1"
        state: present
        content_type: "docker"
        organization: "Test Organization"
        docker_upstream_name: "alpine"
        url: "https://registry-1.docker.io/"
        mirror_on_sync: true
        download_policy: immediate

Anything I’m doing wrong here or any fields missing?

It’s hard to say without the error you’re getting :wink:

Oh sheesh. My bad.

fatal: [121.234.44.54]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to be in '/root/foreman_playbooks/create_docker_repository.yml': line 17, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: \"Create repository\"\n      ^ here\n"}


@evgeni

There is no {{ item }} in the YAML you posted. Can you show us the real playbook?

But this looks like a general Ansible/YAML syntax error, unrelated to the Docker repository.

Tried out a few things and still facing the same error. Here’s the full preview of the playbook

---
- hosts: foreman
  vars_prompt:
    - name: "Product"
      prompt: "Which Product Do you want to install?"
      private: "No"
  tasks:
    - name: "Create product with a sync plan"
      theforeman.foreman.product:
        username: "admin"
        password: "admin"
        server_url: "https://myworld.com/"
        name: "{{ Product }}"
        organization: "Test Organization"
        state: present

    - name: "Create repository"
      theforeman.foreman.repository:
        username: "admin"
        password: "admin"
        server_url: "https://myworld.com/"
        name: "{{ item }}"
        state: present
        content_type: "docker"
        product: "{{ Product }}"
        organization: "Test Organization"
        docker_upstream_name: "alpine"
        url: "https://registry-1.docker.io/"
        mirror_on_sync: true
        download_policy: immediate

You never define item (and there is no with_items or loop on that task…)

In your original post, you had name: "dockerproduct1", which is valid.

Have you got any sample/example for creating a docker repository using playbook?

The one in your first post looks valid to me?

Here is one from our integration tests:

- name: "Create container repository"
  theforeman.foreman.repository:
    username: "admin"
    password: "changeme"
    server_url: "https://foreman.example.com"
    organization: "Test Organization"
    name: "Test Docker Repository"
    product: "Test Product"
    content_type: "docker"
    url: "https://docker.io"
    docker_upstream_name: "busybox"
    docker_tags_whitelist: 
      - latest

Let me try this out

@evgeni That worked. How can we sync the repository along with this playbook?

Yes, you can: theforeman.foreman.repository_sync – Sync a Repository or Product — Foreman Ansible Modules documentation

Alright. Thank you!

@evgeni getting too many requests error when I remove tag limit

Yeah, Docker Hub limits requests these days. I think you can set upstream_username and upstream_password to authenticate with your paid account.