diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000..05512b4 --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,8 @@ +[defaults] +roles_path = ./roles +inventory = ./hosts/ +host_key_checking = False +retry_files_enabled = False + +[ssh_connection] +ssh_args = -o ControlMaster=auto -o ControlPersist=300s -o ForwardAgent=yes diff --git a/ansible/group_vars/all.yml b/ansible/group_vars/all.yml index 1220252..687e747 100644 --- a/ansible/group_vars/all.yml +++ b/ansible/group_vars/all.yml @@ -2,3 +2,4 @@ # This should contain default variables that are for the entire project # across all roles, groups, and hosts. Overrides should be done in # a `globals.yml` located in the projects configuration dir (i.e. /etc/servus/globals) +vars_files_relative: ".." diff --git a/ansible/site.yml b/ansible/site.yml index e69de29..849584b 100644 --- a/ansible/site.yml +++ b/ansible/site.yml @@ -0,0 +1,24 @@ +# vim: set ft=ansible +# site.yml +--- + # add any additional mirrors if you need to otherwise leave default so it will only deploy on localhost + - hosts: repo_mirrors + become: true + vars_files: + - "./vars/main.yml" + tasks: + - include: prepare_system.yml + tags: + - prepare_system + - include: create_mirror.yml + tags: + - create_mirror + - include: prepare_cron.yml + when: repo_autosync|bool == True + tags: + - prepare_cron + - include: remove_cron.yml + when: repo_autosync|bool == False + tags: + - prepare_cron + \ No newline at end of file diff --git a/create_mirror.yml b/create_mirror.yml new file mode 100644 index 0000000..efb8319 --- /dev/null +++ b/create_mirror.yml @@ -0,0 +1,13 @@ +- block: + - name: Create folder for the mirror + file: + path: "/var/www/html/{{ item['folder'] }}" + state: directory + mode: 0755 + with_items: "{{ mirrors }}" + + - name: Execute repository creation script + script: "./scripts/create_repo.sh {{ item['folder'] }} {{ item['items']|join(',') }}" + with_items: "{{ mirrors }}" + + become: true diff --git a/prepare_cron.yml b/prepare_cron.yml new file mode 100644 index 0000000..29b9c3f --- /dev/null +++ b/prepare_cron.yml @@ -0,0 +1,11 @@ +- name: Prepare cronjob for autosync + cron: + day: "{{ crontab_day }}" + hour: "{{ crontab_hour }}" + minute: "{{ crontab_minute }}" + month: "{{ crontab_month }}" + weekday: "{{ crontab_weekday }}" + name: "Cronjob to autosync repo" + state: present + job: "sudo ansible-playbook ~/ayr/site.yml --tags=create_mirror -i ~/ayr/hosts.ini > /dev/null" + become: true diff --git a/prepare_system.yml b/prepare_system.yml new file mode 100644 index 0000000..962c2e3 --- /dev/null +++ b/prepare_system.yml @@ -0,0 +1,37 @@ +- block: + - name: Install needed packages for reposync + package: + name: "{{ item }}" + state: present + with_items: + - createrepo + - yum-utils + - httpd + + - name: Install needed packaged for firewalld + package: + name: "{{ item }}" + state: present + with_items: + - python-firewall + + - name: Check if firewalld is running + command: systemctl is-active firewalld + ignore_errors: yes + changed_when: false + register: firewalld_service_status + + - name: Open firewall port for http (firewall) + firewalld: + service: http + permanent: true + state: enabled + immediate: true + when: firewalld_service_status.stdout == 'active' + + - name: Create directory to contain mirrors + file: + path: "/var/www/html" + state: directory + mode: 0755 + become: true diff --git a/remove_cron.yml b/remove_cron.yml new file mode 100644 index 0000000..c927d75 --- /dev/null +++ b/remove_cron.yml @@ -0,0 +1,5 @@ +- name: Remove cronjob for autosync + cron: + name: "Cronjob to autosync repo" + state: absent + become: true diff --git a/scripts/create_repo.sh b/scripts/create_repo.sh new file mode 100644 index 0000000..66f9cdf --- /dev/null +++ b/scripts/create_repo.sh @@ -0,0 +1,32 @@ +REPO_FOLDER=$1 +REPO_ITEMS=$2 + +REPOPATH="/var/www/html/" +REPOFILE="${REPOPATH}/${REPO_FOLDER}/local.repo" + +mkdir -p $REPOPATH +rm $REPOFILE 2> /dev/null +touch $REPOFILE + +echo "${REPOPATH}${REPO_FOLDER}" + +for i in $(echo $REPO_ITEMS | sed "s/,/ /g") +do + reposync -n -l --repoid=$i --download_path=/var/www/html/general_mirror/ --downloadcomps --download-metadata + wait + + createrepo -v /var/www/html/general_mirror/$i -g comps.xml + wait +done + +for DIR in `find ${REPOPATH}/general_mirror/ -maxdepth 1 -mindepth 1 -type d`; do + REPO_ITEM=$(basename $DIR) + if [[ "${REPO_ITEMS}" =~ "${REPO_ITEM}" ]]; then + echo -e "[${REPO_ITEM}]" >> $REPOFILE + echo -e "name=${REPO_ITEM}" >> $REPOFILE + echo -e "baseurl=http://[your.ip.or.url.goes.here]/general_mirror/${REPO_ITEM}/" >> $REPOFILE + echo -e "enabled=1" >> $REPOFILE + echo -e "gpgcheck=0" >> $REPOFILE + echo -e "\n" >> $REPOFILE + fi +done; diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..4d0cb13 --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,17 @@ +repo_autosync: false +crontab_day: '*' +crontab_hour: '*' +crontab_minute: '*' +crontab_month: '*' +crontab_weekday: '*' +mirrors: + - name: rhel.repo + folder: general_mirror + items: + - rhel-7-server-rpms + - rhel-7-server-extras-rpms + - rhel-7-server-rh-common-rpms + - rhel-ha-for-rhel-7-server-rpms + - epel + - group_rocknsm-rocknsm-2.1 + - atomic