[Pulp-list] Create and remove repo with ansible

Juan Cabrera juan.cabrera at unamur.be
Tue Apr 9 13:43:54 UTC 2019


Hi,

Is there an ansible module to add or remove a rpm repository ?

I send in attachment the scripts I use for the moment to create and
remove the rpm repositories.

I use this:

vars:

   - pulp_repos:
      - repo: epel_ptci
        remote: epel
        remote_url: https://epel.mirror.it2go.eu/7/x86_64/
        state: present

TODO: to become idempotent, check if repo already exists

- name: config | Add repositories.
  command: ~/create_repo --repo "{{ item.repo }}" --remote "{{
item.remote }}" --href "{{ item.remote_url }}"
  loop: "{{ pulp_repos }}"
  when: item.state is not defined or item.state == 'present'
  become: yes
  become_user: "{{ pulp_repo_user }}"


- name: config | Remove repositories
  command: ~/remove_repo --repo "{{ item.repo }}" --remote "{{
item.remote }}"
  loop: "{{ pulp_repos }}"
  when: item.state is defined and item.state == 'absent'
  become: yes
  become_user: "{{ pulp_repo_user }}"

What is the best way to check if a repo already exist?

Juan

-- 

Juan CABRERA
Correspondant informatique
Département de Mathématiques

T. 081724919
juan.cabrera at unamur.be <mailto:juan.cabrera at unamur.be>
http://staff.unamur.be/jbcabrer

Université de Namur ASBL
Rue de Bruxelles 61 - 5000 Namur
Belgique

Let’s respect the environment together.
Only print this message if necessary!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/pulp-list/attachments/20190409/0aa5d8a0/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: lhmcljcamdohejfn.png
Type: image/png
Size: 9356 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/pulp-list/attachments/20190409/0aa5d8a0/attachment.png>
-------------- next part --------------
#!/bin/bash

#!/bin/bash

# Usage info
show_help() {
cat << EOF
Usage: ${0##*/} [-h] --repo REPO_NAME --remote REMOTE_NAME [--port PORT]
    -h                   Display this help and exit
    --repo REPO_NAME     Name of the local repo
    --remote REMOTE_NAME Name of the remote repo to be sync with local one
    --port PORT          (Optional) API port number default to :8000
EOF
}

for i in "$@"; do
    case $i in
        --repo)
        REPO_NAME="$2"
        shift # past argument
        shift # past value
        ;;
        --remote)
        REMOTE_NAME="$2"
        shift # past argument
        shift # past value
        ;;
        --port)
        PORT="$2"
        shift # past argument
        shift # past value
        ;;
        -h|-\?|--help)
        show_help    # Display usage 
        exit
        ;;
     esac
done

if [ -z ${REPO_NAME+x} ]; then
   echo "A variable is unset";
   show_help
   exit
fi
if [ -z ${REMOTE_NAME+x} ]; then
   echo "A variable is unset";
   show_help
   exit
fi

PORT=${PORT-:8000}

export REPO_HREF=$(http $PORT/pulp/api/v3/repositories/ | jq -r --arg REPO_NAME "$REPO_NAME" '.results[] | select(.name=$REPO_NAME) | ._href')
export REMOTE_HREF=$(http $PORT/pulp/api/v3/remotes/rpm/rpm/ | jq -r --arg REMOTE_NAME "$REMOTE_NAME" '.results[] | select(.name=$REMOTE_NAME) | ._href')
export PUBLISHER_HREF=$(http $PORT/pulp/api/v3/publishers/rpm/rpm/ | jq -r --arg REMOTE_NAME "$REMOTE_NAME" '.results[] | select(.name=$REMOTE_NAME) | ._href')
export PUBLICATION_HREF=$(http $PORT/pulp/api/v3/publications/ | jq -r --arg PUBLISHER_HREF "$PUBLISHER_HREF" '.results[] | select(.publisher==$PUBLISHER_HREF) | ._href')
export DISTRI_HREF=$(http $PORT/pulp/api/v3/distributions/ | jq -r --arg PUBLICATION_HREF "$PUBLICATION_HREF" '.results[] | select(.publication==$PUBLICATION_HREF) | ._href')

echo "removing DISTRIBUTION"
http DELETE $PORT$DISTRI_HREF
echo "removing PUBLICATION"
http DELETE $PORT$PUBLICATION_HREF
echo "removing PUBLISHER"
http DELETE $PORT$PUBLISHER_HREF
echo "removing REMOTE"
http DELETE $PORT$REMOTE_HREF
echo "removing REPO"
http DELETE $PORT$REPO_HREF
-------------- next part --------------
#!/bin/bash

# Usage info
show_help() {
cat << EOF
Usage: ${0##*/} [-h] --repo REPO_NAME --remote REMOTE_NAME --href [--port PORT]
    -h                   Display this help and exit
    --repo REPO_NAME     Name of the local repo
    --remote REMOTE_NAME Name of the remote repo to be sync with local one
    --href REMOTE_URL   URL of the remote repository
    --port PORT          (Optional) API port number default to $PORT
EOF
}

wait_task() {

    echo $2
    state=$(http $PORT$1 | jq -r '.state')
    while [ "$state" == "running" ]; do
        sleep 5
        state=$(http $PORT$1 | jq -r '.state')
        echo $state
    done

}

for i in "$@"; do
    case $i in
        --repo)
        REPO_NAME="$2"
        shift # past argument
        shift # past value
        ;;
        --remote)
        REMOTE_NAME="$2"
        shift # past argument
        shift # past value
        ;;
        --href)
        REMOTE_URL="$2"
        shift # past argument
        shift # past value
        ;;
         --port)
        PORT="$2"
        shift # past argument
        shift # past value
        ;;
        -h|-\?|--help)
        show_help    # Display usage 
        exit
        ;;
     esac
done

# Check variables
if [ -z ${REPO_NAME+x} ]; then
   echo "A variable is unset";
   show_help
   exit
fi
if [ -z ${REMOTE_NAME+x} ]; then
   echo "A variable is unset";
   show_help
   exit
fi
if [ -z ${REMOTE_URL+x} ]; then
   echo "A variable is unset";
   show_help
   exit
fi

# set default if null
PORT=${PORT-:8000}

# Create a new local repository and get its HREF
export REPO_HREF=$(http POST $PORT/pulp/api/v3/repositories/ name=epel_ptci | jq -r '._href')
# Add a new remote repository and get its HREF
export REMOTE_HREF=$(http POST $PORT/pulp/api/v3/remotes/rpm/rpm/ name="$REMOTE_NAME" url="$REMOTE_URL" policy='immediate' | jq -r '._href')
# Synchronize repos
export TASK=$(http $PORT${REMOTE_HREF}sync/ repository=$REPO_HREF | jq -r '.task')

wait_task $TASK "Waiting for sync to finish ..."

# Create a rpm Publisher and get its HREF
export PUBLISHER_HREF=$(http POST $PORT/pulp/api/v3/publishers/rpm/rpm/ name="$REMOTE_NAME" | jq -r '._href')
# Create a Publication of local repository and get its local HREF
export TASK=$(http POST $PORT${PUBLISHER_HREF}publish/ repository="$REPO_HREF" | jq -r '.task')

wait_task $TASK "Waiting for Publication to finish ..."

# Get HREF of Local Publication
export PUBLICATION_HREF=$(http $PORT/pulp/api/v3/publications/ | jq -r --arg PUBLISHER_HREF "$PUBLISHER_HREF" '.results[] | select(.publisher==$PUBLISHER_HREF) | ._href')

# Distribute the repo
http POST $PORT/pulp/api/v3/distributions/ name='epel_ptci' base_path='epel' publication=$PUBLICATION_HREF



More information about the Pulp-list mailing list