saltstack基于pillar实现文件管理的案例

在 /srv/salt 目录下

 top.sls 文件

base:
  "10.10.10.10":
    - tx-hw-prd
    - test

tx-hw-prd.sls 文件

docker-compose:
  file.managed:
    - name: /tmp/docker-compose.yaml  # 分发到目标机器的路径和文件名称
    - source: salt://hw/prd/docker-compose.yaml  # 模板文件路径
    - makedirs: True
    - template: jinja
    - backup: minion
    - context:
      APP: {{ pillar["server"]["app"] }}
      ConfigName: {{ pillar["ConfigName"] }}
      memsd_host: {{ pillar["memsd_host"] }}
      login_num: {{ pillar["server"]["login_num"] }}
      social_num: {{ pillar["server"]["social_num"] }}
      pvp_num: {{ pillar["server"]["pvp_num"] }}
      game_num: {{ pillar["server"]["game_num"] }}
      host_wanip: {{ pillar["server"]["host_wanip"] }}
      server_id: {{ pillar["server"]["server_id"] }}
      version:  {{ salt['pillar.get']('version') }}  # 在命令行传入

模板文件hw/prd/docker-compose.yaml

version: "3.9"

networks:
  default:
    driver: bridge
    ipam:
      config:
        - subnet: 172.19.0.0/16

{% if "design-config" in APP %}
volumes:
  localdesigncfg: { }
{% endif %}

services:
  {% if "design-config" in APP %}
  design-config:
    container_name: design-config
    image: hw-jcr.jjworlds.com/diaoyu/design-config:new-v{{ version }}
    restart: "no"
    volumes:
      - localdesigncfg:/data/app
  {% endif %}
  {% if "memsd" in APP %}
  memsd:
    container_name: memsd
    command: ./memsd -addr="0.0.0.0:8905" -datafile=/app/log/memsd_persist.json
    image: hw-jcr.jjworlds.com/diaoyu/memsd:new-v{{ version }}
    restart: unless-stopped
    ports:
      - "8905:8905"
    volumes:
      - ./data/memsd:/app/log
  {% endif %}
  {% if "hub" in APP %}
  hub:
    container_name: hub
    command: ./hub -svcgroup="{{ConfigName}}" -sdaddr="{{memsd_host}}:8905" -configname="{{ConfigName}}" -logfile=/app/log/hub.log -logfilesize=100M
    image: hw-jcr.jjworlds.com/diaoyu/hub:new-v{{ version }}
    restart: unless-stopped
    depends_on:
      - memsd
    volumes:
      - ./log:/app/log
      - ./config/GeoLite2-Country.mmdb:/data/geo/GeoLite2-Country.mmdb:ro
  {% endif %}
  {% if "capt" in APP %}
  capt:
    container_name: capt
    command: ./capt -svcgroup="{{ConfigName}}" -sdaddr="{{memsd_host}}:8905" -configname="{{ConfigName}}" -logfile=/app/log/capt.log -logfilesize=100M -uselocaldesigncfg=1
    image: hw-jcr.jjworlds.com/diaoyu/capt:new-v{{ version }}
    restart: unless-stopped
    depends_on:
      - memsd
      - hub
    volumes:
      - ./log:/app/log
      - ./hw:/data/aurora/hw
      - ./config/GeoLite2-Country.mmdb:/data/geo/GeoLite2-Country.mmdb:ro
      - localdesigncfg:/app/cfg:ro
  {% endif %}
  {% if "match" in APP %}
  match:
    container_name: match
    command: ./match -svcgroup="{{ConfigName}}" -sdaddr="{{memsd_host}}:8905" -configname="{{ConfigName}}" -logfile=/app/log/match.log -logfilesize=100M -uselocaldesigncfg=1
    image: hw-jcr.jjworlds.com/diaoyu/match:new-v{{ version }}
    restart: unless-stopped
    depends_on:
      - memsd
      - capt
    volumes:
      - ./log:/app/log
      - ./config/GeoLite2-Country.mmdb:/data/geo/GeoLite2-Country.mmdb:ro
      - localdesigncfg:/app/cfg:ro
  {% endif %}
  {% if "pay" in APP %}
  pay:
    container_name: pay
    command: ./pay -svcgroup="{{ConfigName}}" -sdaddr="{{memsd_host}}:8905" -configname="{{ConfigName}}" -logfile=/app/log/pay.log -logfilesize=100M -uselocaldesigncfg=1
    image: hw-jcr.jjworlds.com/diaoyu/pay:new-v{{ version }}
    restart: unless-stopped
    depends_on:
      - memsd
      - match
    ports:
      - "19450:19450"
    volumes:
      - ./log:/app/log
      - ./hw:/data/aurora/hw
      - ./config/GeoLite2-Country.mmdb:/data/geo/GeoLite2-Country.mmdb:ro
      - localdesigncfg:/app/cfg:ro
  {% endif %}
  {% if "oprfx" in APP %}
  oprfx:
    container_name: oprfx
    command: ./oprfx -svcgroup="{{ConfigName}}" -sdaddr="{{memsd_host}}:8905" -configname="{{ConfigName}}" -logfile=/app/log/oprfx.log -logfilesize=100M -uselocaldesigncfg=1
    image: hw-jcr.jjworlds.com/diaoyu/oprfx:new-v{{ version }}
    restart: unless-stopped
    depends_on:
      - memsd
      - pay
    volumes:
      - ./log:/app/log
      - ./config/GeoLite2-Country.mmdb:/data/geo/GeoLite2-Country.mmdb:ro
      - localdesigncfg:/app/cfg:ro
  {% endif %}
  {% if "oprmaster" in APP %}
  oprmaster:
    container_name: oprmaster
    command: ./oprmaster -svcgroup="{{ConfigName}}" -sdaddr="{{memsd_host}}:8905" -configname="{{ConfigName}}" -logfile=/app/log/oprmaster.log -logfilesize=100M
    image: hw-jcr.jjworlds.com/diaoyu/oprmaster:new-v{{ version }}
    restart: unless-stopped
    depends_on:
      - memsd
      - oprfx
    ports:
      - "19196:19196"
    volumes:
      - ./log:/app/log
      - ./config/GeoLite2-Country.mmdb:/data/geo/GeoLite2-Country.mmdb:ro
  {% endif %}
  {% if "login" in APP %}
  {% for i in range(1,login_num+1) %}
  login{{i}}:
    container_name: login{{i}}
    command: ./login -svcindex="{{server_id}}{{i}}" -svcgroup="{{ConfigName}}" -sdaddr="{{memsd_host}}:8905" -configname="{{ConfigName}}" -logfile=/app/log/login.log -logfilesize=100M -uselocaldesigncfg=1
    image: hw-jcr.jjworlds.com/diaoyu/login:new-v{{ version }}
    restart: unless-stopped
    depends_on:
      - memsd
      - oprmaster
    ports:
      - "10116:10116"
    volumes:
      - ./log:/app/log
      - ./config/GeoLite2-Country.mmdb:/data/geo/GeoLite2-Country.mmdb:ro
      - localdesigncfg:/app/cfg:ro
  {% endfor %}
  {% if "room" in APP %}
  room:
    container_name: room
    command: ./room -svcgroup="{{ConfigName}}" -sdaddr="{{memsd_host}}:8905" -configname="{{ConfigName}}" -logfile=/app/log/room.log -logfilesize=100M -uselocaldesigncfg=1
    image: hw-jcr.jjworlds.com/diaoyu/room:new-v{{ version }}
    restart: unless-stopped
    depends_on:
      - memsd
      - login
    volumes:
      - ./log:/app/log
      - ./config/GeoLite2-Country.mmdb:/data/geo/GeoLite2-Country.mmdb:ro
      - localdesigncfg:/app/cfg:ro
  {% endif %}
  {% if "social" in APP %}
  {% for i in range(1,social_num+1) %}
  social{{i}}:
    container_name: social{{i}}
    command: ./social -svcindex="{{server_id}}{{i}}" -svcgroup="{{ConfigName}}" -sdaddr="{{memsd_host}}:8905" -configname="{{ConfigName}}" -logfile=/app/log/social{{i}}.log -logfilesize=100M -uselocaldesigncfg=1
    image: hw-jcr.jjworlds.com/diaoyu/social:new-v{{ version }}
    restart: unless-stopped
    depends_on:
      - memsd
      - room
    volumes:
      - ./log:/app/log
      - ./config/GeoLite2-Country.mmdb:/data/geo/GeoLite2-Country.mmdb:ro
      - localdesigncfg:/app/cfg:ro
  {% endfor %}
  {% endif %}
  {% if "pvp" in APP %}
  {% for i in range(1,pvp_num+1) %}
  pvp{{i}}:
    container_name: pvp{{i}}
    command: ./pvp -svcindex="{{server_id}}{{i}}" -svcgroup="{{ConfigName}}" -sdaddr="{{memsd_host}}:8905" -configname="{{ConfigName}}" -logfile=/app/log/pvp{{i}}.log -logfilesize=100M -uselocaldesigncfg=1
    image: hw-jcr.jjworlds.com/diaoyu/pvp:new=v{{ version }}
    restart: unless-stopped
    depends_on:
      - memsd
      - social0
    volumes:
      - ./log:/app/log
      - ./hw:/data/aurora/hw
      - ./config/GeoLite2-Country.mmdb:/data/geo/GeoLite2-Country.mmdb:ro
      - localdesigncfg:/app/cfg:ro
  {% endfor %}
  {% endif %}
  {% if "game" in APP %}
  {% for i in range(1,game_num+1) %}
  game{{i}}:
    container_name: game{{i}}
    command: ./game -svcindex="{{server_id}}{{i}}" -tcp-port="1020{{i-1}}" -svcgroup="{{ConfigName}}" -sdaddr="{{memsd_host}}:8905" -wanip="{{host_wanip}}" -configname="{{ConfigName}}" -logfile=/app/log/game{{i}}.log -logfilesize=100M -uselocaldesigncfg=1
    image: hw-jcr.jjworlds.com/diaoyu/game:new-v{{ version }}
    restart: unless-stopped
    depends_on:
      - memsd
      - pvp0
    ports:
      - "1020{{i-1}}:1020{{i-1}}"
    volumes:
      - ./log:/app/log
      - ./hw:/data/aurora/hw
      - ./config/GeoLite2-Country.mmdb:/data/geo/GeoLite2-Country.mmdb:ro
      - localdesigncfg:/app/cfg:ro
  {% endfor %}
  {% endif %}

{% endif %}

可以看出来,模板文件中引用了很多pillar属性。pillar属性相关的配置放在/srv/pillar 文件夹下。现在看下/srv/pillar的配置文件

在/srv/pillar目录下

pillar的配置和state一样,也有个入口文件top.sls

查看top.sls文件

base:
  '10.10.10.10':
    - hw.prd.10-10-10-10  # 表示当前文件夹下,hw目录下,prd目录下的10-10-10-10.sls文件

查看 hw/prd/10-10-10-10.sls 文件

server:
    app:
      - design-config
      - memsd
      - hub
      - capt
      - match
      - pay
      - oprfx
      - oprmaster
      - login
      - room
      - social
      - pvp
      - game
    server_id: 1
    host_wanip: 0.0.0.0
    login_num: 2
    social_num: 2
    pvp_num: 5
    game_num: 10
 
ConfigName: hw_f2
memsd_host: 10.10.10.10

 执行命令:

salt '10.10.10.10' state.apply tx-hw-prd  pillar='{version: 9900}'
posted @ 2024-01-19 16:45  羊脂玉净瓶  阅读(17)  评论(0)    收藏  举报