洞察提升小程序标签管理,实现高效的金融行业数字化转型
883
2022-11-04
用bash进行最小的分布式配置管理。ansible / chef / puppet / 等的微小替代品
Minimal distributed configuration management in bash. Tiny alternative to ansible / chef / puppet / etc. Made with❤️ by the friendly folks at https://team.video
$ sudo av applyFetching inventory...Applying module nginxApplying module memcachedDone
How it works
Aviary.sh follows some guiding principles:
bash is just fine (yes, it is)minimize levels of abstractioneach host takes care of itself
Each host periodically fetches the latest version of the inventory to see what roles should it be performing. Given whatever roles, the inventory also describes modules (services, programs, etc) that need to be installed and running in order to fulfill the role, and the host configures itself accordingly. The inventory is a git repo with a specific directory structure and idempotent scripts to apply modules.
Getting started
Installation
Install from the command line, on a box to be managed by aviary.sh:
curl https://aviary.sh/install | sudo bash
Inventory setup
Configure your inventory if you don't have one yet:
mkdir inventorycd inventorymkdir {hosts,modules,roles,directives}touch {hosts,modules,roles,directives}/.gitkeepgit initgit add .git commit -m "initial commit"
Configure and push your repo to an origin:
git remote add origin $my_origin_urlgit push -u origin master
Set your inventory url in config:
echo inventory_git_url=$my_origin_url >> /var/lib/aviary/config
Of course the dealings with git will be non-interactive, so you need to either set up ssh keys or access tokens in order to make that work. In GitHub for example, find "Personal Access Tokens" under your account settings. Once you have an access token, you can include it in the git url, e.g., https://
Modules
In the inventory directory we created above, add our first module:
mkdir modules/motd
Create an idempotent script to configure the message-of-the-day that users will see when they log in to this box. In the inventory, create modules/motd/apply with these contents:
# inventory/modules/motd/applycat <
Now create this host in the inventory, and add the motd module to be applied:
mkdir hosts/$(hostname)echo motd > hosts/$(hostname)/modules
It's usually better practice to apply roles (sets of modules) to hosts, but you can also apply ad-hoc modules directly if you like, as we're doing here.
Now check in these contents and push them up to the inventory repo.
Running av
To apply our module, run av apply.
# av applyFetching inventory...Applying motd...Done.
Inspect /etc/motd to see that our motd module has in fact been applied.
Running av status (or just av) tells us how the host is configured and what is its status:
# av statusSTATUS OK
Templates and variables
In order to make configuration files dynamic, we can use template files and variable interpolation. Templates are {{ moustache }} style, and variables can be configured at various levels of the inventory directory hierarchy in variables bash files containing variable assignments.
Let's spruce up our motd module. In the inventory, let's add a template in modules/motd:
# inventory/modules/motd/motd.templateWelcome to {{ hostname }}"Ever make mistakes in life? Let’s make them birds. Yeah, they’re birds now."--Bob Ross
Set the hostname variable in a variables file:
# inventory/modules/motd/variableshostname=$(hostname)
Set the apply script to interpolate the template:
# inventory/modules/motd/applysource templatesource variablestemplate $(dirname $0)/motd.template > /etc/motd
Looking at our inventory directory structure, we should see something like this:
inventory├── hosts│ └── my-host-01│ └── modules├── modules│ └── motd│ ├── apply│ ├── motd.template│ └── variables├── roles└── directives
From here, add more modules, group modules into roles, and apply roles to your hosts, similarly to how we did with this first module.
Concepts
Inventory - Git repository where you keep configuration about your servers and what-all they should be doing. Consists of hosts, roles, and modules.
Host - Server virtual or not with a hostname.
Role - High-level function that you define (e.g., "application server", or "database server") to be assumed by the host. Multiple roles may be applied to a host.
Module - Service or program (e.g., "node", or "postgres") required to fulfill a role. A role will usually be comprised of many modules.
Directive - One-time set of commands to be executed immediately.
Inventory
The inventory is the git repository where you keep configuration about your servers -- what roles they play, what services they run, etc.
There are four top-level directories: hosts, roles, modules, and directives. Files in each directory are newline-delimited text files, unless specified otherwise
Hosts
The hosts directory contains a directory for each host. In each host directory live the files:
roles - a list of roles to be fulfilled by the hostmodules - a list of ad-hoc modules to be applied on the hostvariables - list of bash variable assignments local to the host
Roles
The roles directory contains a directory for each role. In each role directory live the files:
modules - a list of modules required to fulfill the given role
Modules
The modules directory contains a directory for each module. In each module directory live the files:
apply - idempotent bash script that will ensure the given service or program is installed and runningvariables - list of bash variable assignments local to the moduletest - bash script to assert that all looks well after we've run applyany other files (templates, configuration files, etc) necessary to support the apply script
Directives
The directives directory contains bash scripts to be executed once on each host, immediately when they are discovered. Only directive scripts with modification times within the most recent 24 hours will be considered for execution.
av
The command line tool is called av.
av - manage configuration for your hostsUsage: av [options] [command] [command-args]Options: --help Show this help message --version Show version --log-level
Why Bash?
For just about three decades, bash has been standard issue on the vast majority of computers serving traffic on the Internet. Other languages have come and gone, each with their own story and arc, while bash has just been there.
In addition to it consistently being there, it is good / good-enough at most everything we want to do while configuring a machine. If we were using some other language half the time we'd end up shelling out anyway, so let's just stick in the shell to begin with and revel in the ease and consistency...
But it's too funky you say. Well, yes, bash can have its quirks. But we need some funk every now and then. Let's just embrace it!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~