Skip to content

JohnnyWorks-TW/docker-ansible-rulebook

Repository files navigation

Ansible Rulebook image

This is a ansible-rulebook image for Ansible. It is based on the official Ansible image and adds the following:

  • ansible
  • ansible-rulebook

Usage

You can use following command to debug:

docker run --rm -it -v .:/data johnnyworks/ansible-rulebook:jdk17-ubi8 bash

Example

For example, you have a nginx web server and you want to check if the web server is running. You have a rulebook file rulebook_check_url.yml and an inventory file inventory.yml in the current directory.

First, bring up a nginx web server:

docker run --rm -d -p 8880:80 nginx:stable-alpine

or use docker-compose:

'docker-compose.yml':

version: '3.8'
services:
  nginx:
    image: nginx:stable-alpine
    restart: always
    ports:
      - 8880:80

bring up the nginx web server:

docker compose up -d

Check the web server:

curl http://192.168.1.2:8880/

You should see the default nginx page. (Assume that the nginx web server is running on 192.168.1.2:8880 change it to your own settings.)

Then, create a rulebook file rulebook_check_url.yml:

---
- name: Check Web server
  hosts: all
  sources:
    - ansible.eda.url_check:
        urls:
          - http://192.168.1.2:8880/
        delay: 10
  rules:
    - name: Restart Nginx
      condition: event.url_check.status == "down"
      action:
        run_playbook:
          name: playbook_restart_nginx.yml

Create a playbook file playbook_restart_nginx.yml:

---
- name: Restart nginx
  hosts: myservers
  gather_facts: no
  vars:
    nginx_path: /path/to/test-nginx
  tasks:
    - name: docker compose down
      ansible.builtin.shell: cd {{ nginx_path }} && docker compose down
    - name: docker compose up
      ansible.builtin.shell: cd {{ nginx_path }} && docker compose up -d

Create a inventory file inventory.yml:

myservers:
  hosts:
    192.168.1.2:
      ansible_user: myuser
      ansible_ssh_private_key_file: id_ed25519
      ansible_connection: ssh
      ansible_port: 22

Assume that the nginx web server is running on 192.168.1.2 and the ssh private key file is id_ed25519. Change it to your own settings.

You can use following command to test ansible-playbook:

docker run --rm -it -v .:/data johnnyworks/ansible-rulebook:jdk17-ubi8 ansible-playbook -vvv -i /data/inventory.yml /data/playbook_restart_nginx.yml

Finally, you can use following command to run ansible-rulebook:

docker run --rm -it -v .:/data johnnyworks/ansible-rulebook:jdk17-ubi8 ansible-rulebook --rulebook /data/rulebook_check_url.yml -i /data/inventory.yml --verbose

If the web server is down, the ansible-rulebook will restart the nginx web server.

About

Ansible Rulebook image

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published