added readme for setup and more needs longer msg

This commit is contained in:
Expand 2023-09-24 08:23:51 +10:00
parent 04fd57aafd
commit d75756dad4
6 changed files with 106 additions and 18 deletions

View file

@ -1,2 +1,12 @@
# caddy configure
Configures caddy with ansible and reverts changes should there be an issue
Configures caddy with ansible and reverts changes should there be an issue.
ONLY COMPATIBLE WITH BINARY VERSION FOR NOW, DOCKER COMPAT WILL COME LATER
## How to use
1. install ansible on any linux machine or WSL install, it can even be the machine you are planning to install Caddy on. https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html
2. `git clone https://github.com/Expand-sys/caddyconfigure && cd caddyconfigure`
3. `cp caddyfile/Caddyfile.rename-me caddyfile/Caddyfile`
4. edit ./inventory with your favourite text editor e.g. `nano ./inventory` to have the ip address of your target server, can be 127.0.0.1 or localhost if it is running on the same machine, by default it is 127.0.0.1
5. edit vars/default.yml with your favourite text editor e.g. `nano vars/default.yml` if you already have an install of caddy(not docker) set install caddy to false, it wont harm anything to keep it set to true either way it will just update your caddy binary to the latest. and make sure if you want a custom location for your caddy file to change it here aswell.
6. run `ansible-playbook -i inventory main.ansible.yml -k` This script must be run as root user on the target machine so the `-k` will ask for the ssh password for the root user __OF THE TARGET SERVER__ if you have ssh keys set up for the root user you may omit the -k

View file

@ -1,3 +1,9 @@
# DO NOT EDIT BELOW
localhost {
respond "200"
}
# DO NOT EDIT ABOVE
# V edit here V #
example.test {
reverse_proxy 10.10.0.24:8787

53
caddyinstall.ansible.yml Normal file
View file

@ -0,0 +1,53 @@
---
- name: install caddy from binary
ansible.builtin.get_url:
url: https://caddyserver.com/api/download?os=linux&arch=amd64&p=github.com%2Fcaddy-dns%2Fcloudflare&idempotency=54951177807414
dest: /usr/bin/caddy
mode: '0751'
- name: create caddyfile
ansible.builtin.blockinfile:
block: |
localhost {
respond "200"
}
####
path: "{{ CADDYFILE_LOC }}"
create: true
- name: create Caddy systemd file
ansible.builtin.copy:
content: |
[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target
[Service]
Type=notify
User=root
Group=root
ExecStart=/usr/bin/caddy run --environ --config {{ CADDYFILE_LOC }}
ExecReload=/usr/bin/caddy reload --config {{ CADDYFILE_LOC }} --force
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
dest: /etc/systemd/system/caddy.service
- name: Start Caddy on startup
ansible.builtin.systemd_service:
name: 'caddy'
state: 'started'
enabled: yes
daemon_reload: true
- name: install caddy trust
ansible.builtin.shell: "caddy trust"

View file

@ -1,3 +1,6 @@
[caddyserver]
10.10.0.24 ansible_ssh_user=root
hostnode:
hosts:
127.0.0.1: #change me to your caddy servers ip address or hostname
vars:
ansible_ssh_user: root #changing this will require changes to the playbook, currently the playbook must be run as root.
ansible_ssh_common_args: '-o StrictHostKeyChecking=no' #This stops hostkey checking useful if like me you destroy vms and build new ones all the time

View file

@ -1,14 +1,23 @@
---
- name: Configure caddy
hosts: caddyserver
hosts: hostnode
vars:
become: false
pre_tasks:
- name: Load variables
ansible.builtin.include_vars: '{{ item }}'
with_first_found:
- "vars/default.yml"
tasks:
- name: set up reverse proxy
include_tasks: caddyinstall.ansible.yml
when: INSTALL_CADDY
- name: copy existing Caddyfile to make sure there is a backup
ansible.builtin.copy:
src: /Caddyfile
src: "{{ CADDYFILE_LOC }}"
remote_src: true
dest: /Caddyfile.bak
dest: "{{ CADDYFILE_LOC }}.bak"
owner: root
group: root
mode: '0644'
@ -16,19 +25,21 @@
- name: Insert/Update configuration using a local file and validate it
ansible.builtin.blockinfile:
block: "{{ lookup('ansible.builtin.file', './caddyfile/Caddyfile') }}"
path: /Caddyfile
path: "{{ CADDYFILE_LOC }}"
insertafter: "####"
- name: restart Caddy
ansible.builtin.docker_container:
name: caddy-caddy-1
state: started
restart: true
ansible.builtin.systemd_service:
name: caddy
state: restarted
daemon_reload: true
- name: Pause play until a URL is reachable from this host
ansible.builtin.uri:
url: "https://pve.dubois.zip"
url: "https://localhost"
follow_redirects: none
method: GET
validate_certs: false
register: _result
until: _result.status == 200
retries: 6 # 6 * 5 seconds = 30sec
@ -45,11 +56,11 @@
mode: '0644'
when: _result.status != 200
- name: if _result.status != 200 restart caddy
ansible.builtin.docker_container:
name: caddy-caddy-1
state: started
restart: true
- name: restart Caddy
ansible.builtin.systemd_service:
name: caddy
state: restarted
daemon_reload: true
when: _result.status != 200
- name: if _result.status != 200, fail

5
vars/default.yml Normal file
View file

@ -0,0 +1,5 @@
---
#if you want to install/upgrade caddy on the host above please set INSTALL_CADDY to true.
INSTALL_CADDY: true
#Put the location of your caddyfile should you need to manually edit it and want it in an easier place to find.
CADDYFILE_LOC: /etc/caddy/Caddyfile