#!/usr/bin/env ansible-playbook
---
#==============================================================#
# File      :   node.yml
# Desc      :   init node for pigsty
# Ctime     :   2020-05-12
# Mtime     :   2026-01-20
# Path      :   node.yml
# Docs      :   https://pigsty.io/docs/node/playbook/#nodeyml
# License   :   Apache-2.0 @ https://pigsty.io/docs/about/license/
# Copyright :   2018-2026  Ruohang Feng / Vonng (rh@vonng.com)
#==============================================================#
- name: NODE
  hosts: all
  become: true
  gather_facts: no
  roles:
    - { role: node_id       , tags: id       }  # get node identity (always)
    - { role: node          , tags: node     }  # prepare node for pigsty
    - { role: haproxy       , tags: haproxy  }  # init haproxy optional
    - { role: node_monitor  , tags: monitor  }  # init node monitor


#---------------------------------------------------------------
# Usage
#---------------------------------------------------------------
# 1. make sure you have nopass ssh & sudo access to target node
# 2. make sure you have defined these nodes in your inventory
# 3. run ./node.yml -l <selector> to finish node provisioning
#
#  Add node to pigsty:
#     node.yml -l <cls>      # add groups
#     node.yml -l <ip>       # add single node
#
#  Observability push-agent mode:
#     ./node.yml -l openclaw.svc.plus,jp-xhttp.svc.plus \
#       -e node_monitor_mode=push \
#       -e observability_endpoint=https://observability.svc.plus/ \
#
#  Bootstrap with another admin user: (Create admin with another admin)
#     node.yml -t node_admin       # create admin user for nodes
#     node.yml -t node_admin -k -K -e ansible_user=<another admin>
#
#--------------------------------------------------------------#
# Utils
#--------------------------------------------------------------#
#
#  bin/node-add pg-meta               # init nodes of group `pg-meta`
#  bin/node-add pg-meta pg-test       # init multiple node groups
#  bin/node-add 10.10.10.10           # init node with ip `10.10.10.10`
#  bin/node-add 10.10.10.1*           # init node with pattern `10.10.10.1*`
#  bin/node-add 1.2.3.4 5.6.7.8       # init multiple nodes
#
#---------------------------------------------------------------
# Identity
#---------------------------------------------------------------
# nodename : optional, used as node identity, default to existing name
# node_cluster : optional, used as node identity, default to `nodes`
#
#  cls: node_cluster (explicit) > pg_cluster (when node_id_from_pg) > 'nodes'
#  ins: nodename (explicit) > pg_instance (when node_id_from_pg) > node.hostname
#
# you can overwrite node identity with pgsql identity on 1:1 deployment
# with option `node_id_from_pg: true`
#
# 1. node identity can be specified by nodename or node_cluster explicitly (override all)
# 2. if node identity is not specified and node_id_from_pg is set, it will use pg's instance identity if applicable
# 3. otherwise, node_cluster will fall back to `nodes` and nodename will fall back to node's current hostname
#---------------------------------------------------------------
# Tasks
#---------------------------------------------------------------
# node-id       : generate node identity
# node_name     : setup hostname
# node_hosts    : setup /etc/hosts records
# node_resolv   : setup dns resolver
# node_firewall : setup firewall & selinux
# node_ca       : add & trust ca certificate
# node_repo     : add upstream repo
# node_pkg      : install yum packages
# node_uv       : setup uv python venv
# node_feature  : setup numa, grub, static network
# node_kernel   : enable kernel modules
# node_tune     : setup tuned profile
# node_sysctl   : setup additional sysctl parameters
# node_profile  : write /etc/profile.d/node.sh
# node_alias    : write /etc/profile.d/node.alias.sh
# node_ulimit   : setup resource limits
# node_data     : setup main data dir
# node_admin    : setup admin user and ssh key
# node_timezone : setup timezone
# node_ntp      : setup ntp server/clients
# node_crontab  : add/overwrite crontab tasks
# node_vip      : setup optional l2 vrrp vip for node cluster
#   - vip_install
#   - vip_config
#   - vip_launch
#   - vip_reload
# haproxy       : setup haproxy on node to expose services
#   - haproxy_install
#   - haproxy_config
#   - haproxy_launch
#   - haproxy_reload
# monitor       : setup node_exporter & vector for metrics & logs
#   - haproxy_register
#   - vip_dns
#   - node_exporter
#     - node_exporter_config
#     - node_exporter_launch
#   - vip_exporter
#     - vip_exporter_config
#     - vip_exporter_launch
#   - node_register
#     - add_metrics
#     - add_logs
#   - vector
#     - vector_clean
#     - vector_config
#     - vector_launch
#---------------------------------------------------------------
...
