Skip to content

Instantly share code, notes, and snippets.

@advanceboy
Last active November 21, 2019 15:43
Show Gist options
  • Select an option

  • Save advanceboy/7fa56d574c40479a654563e8accde10e to your computer and use it in GitHub Desktop.

Select an option

Save advanceboy/7fa56d574c40479a654563e8accde10e to your computer and use it in GitHub Desktop.
AWX を docker-compose と 公式ビルド済みコンテナ を使ってインストールする Ansible Playbook。使用例: `ansible-playbook -i 'username@hostname,' install_awx.yml`
---
- hosts: all
vars:
venv_dir: /tmp/venv-awx
awx_repo: https://github.com/ansible/awx.git
awx_repo_dir: /tmp/awx
awx_version: 9.0.1
postgres_data_dir: /tmp/pgdocker
#http_proxy: http://proxy.example.com
#https_proxy: http://proxy.example.com
#no_proxy: localhost,127.0.0.1,company.example.com
envs:
http_proxy: "{{ http_proxy | default(ansible_env.http_proxy | default('')) }}"
https_proxy: "{{ https_proxy | default(ansible_env.https_proxy | default('')) }}"
no_proxy: "{{ no_proxy | default(ansible_env.no_proxy | default('localhost,127.0.0.1')) }}"
tasks:
- environment: "{{envs}}"
block:
- name: update pre-setting packages
become: yes
yum:
name:
- epel-release
- yum-utils
state: latest
- name: add docker-ce repo
become: yes
shell: "yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo"
args:
chdir: /etc/yum.repos.d
creates: docker-ce.repo
- name: remove conflict packages
become: yes
yum:
name:
# https://docs.docker.com/install/linux/docker-ce/centos/#uninstall-old-versions
- docker
- docker-engine
- docker-compose
state: absent
- name: ensure packages
become: yes
yum:
name:
- git
- python-pip
- python-virtualenv
- docker-ce
state: latest
register: result_pagkages
# to abort "Cannot uninstall 'requests'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall." error
# using virtualenv to avoid the conflict of python packages between yum and pip when installing docker-compose
# -> https://github.com/docker/compose/issues/5883
#
# to abort "Unable to load docker-compose. Try `pip install docker-compose`." error
# https://www.uramiraikan.net/Works/entry-3362.html
#
# create virtualenv with --system-site-packages option
# to make use of any libraries installed in the system’s Python
# https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#running-in-a-virtualenv
#
# to avoid docker/compose#7030 issue, fix docker-compose package version to 1.24.1
# https://github.com/docker/compose/issues/7030
- name: ensure pip packages
pip:
name:
- ansible
- docker-compose==1.24.1
state: present
virtualenv: "{{ venv_dir }}"
virtualenv_site_packages: yes
- block:
- name: create docker proxy settings directory
become: yes
file:
path: /etc/systemd/system/docker.service.d
state: directory
owner: root
group: root
mode: u=rwx,g=rx,o=rx
- name: create docker proxy settings
become: yes
copy:
dest: /etc/systemd/system/docker.service.d/http-proxy.conf
content: |
[Service]
Environment="HTTP_PROXY={{ envs.http_proxy }}" "HTTPS_PROXY={{ envs.https_proxy }}" "NO_PROXY={{ envs.no_proxy }}"
register: result_proxy
when: envs.http_proxy or envs.https_proxy
- block:
- name: get the current groups
command: groups
changed_when: false
register: original_groups
- name: ensure the connecting user is member of docker group
become: yes
user:
name: "{{ ansible_env.USER }}"
groups: docker
append: yes
register: result_user
# reset ssh connection for refletction the changed groups
# (`meta: reset_connection` occurs an error on current ansible version)
# https://stackoverflow.com/a/28213378
- name: kill open ssh sessions for force reconnection
shell: sleep 1; pkill -u {{ ansible_env.USER }} sshd
async: 3
poll: 2
when: result_user is changed
when: ansible_env.USER != 'root'
- name: enable and restart docker service
become: yes
systemd:
name: docker
state: restarted
enabled: yes
daemon_reload: yes
when: result_pagkages is changed or result_user is changed or result_proxy is changed
- name: git clone awx
git:
repo: '{{awx_repo}}'
dest: '{{awx_repo_dir}}'
version: '{{awx_version}}'
clone: yes
force: yes
- name: record playbook options
set_fact:
install_awx_playbook_opotions: >-
-e postgres_data_dir={{ postgres_data_dir }}
{% if envs.http_proxy or envs.https_proxy %}
-e http_proxy={{ envs.http_proxy }} -e https_proxy={{ envs.https_proxy }} -e no_proxy={{ envs.no_proxy }}
{% endif %}
- name: execute awx playbook
shell: "source {{ venv_dir+'/bin/activate' | quote }}; ansible-playbook -i inventory install.yml {{ install_awx_playbook_opotions }}"
args:
chdir: '{{awx_repo_dir}}/installer'
tags:
- inner-playbook
- name: ensure the connecting user is not member of docker group
become: yes
user:
name: "{{ ansible_env.USER }}"
groups: "{{ original_groups.stdout.split(' ') | join(',') }}"
append: no
when: result_user is changed
tags:
- revert-docker-group

Ansible Playbook for Installing AWX with docker-compose

How to use

Directory

$ ansible-playbook -i 'username@hostname,' install_awx.yml

Uses inventory file

Create the following inventory file:

hostname

[all:vars]
ansible_ssh_user=username

#http_proxy=http://proxy:3128
#https_proxy=http://proxy:3128
#no_proxy=mycorp.org

and run below

$ ansible-playbook -i inventory install_awx.yml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment