for is a command-line tool designed to automate the execution of shell commands and scripts across multiple remote servers, similar to Ansible, but with a focus on simplicity and use of Go.
- Execute commands and scripts defined in a configuration file.
- Run ad hoc commands and scripts on specified groups of hosts.
- Secure SSH connections for remote command execution.
- Support for grouping hosts in an inventory file.
- Option to run commands and scripts locally.
- Service-based execution similar to Ansible roles.
-
Clone the repository:
git clone https://github.com/diceone/for.git cd for
-
Initialize the Go module (if not already initialized):
go mod init github.com/diceone/for
-
Download dependencies:
go mod tidy
-
Build the binary:
go build -o for ./cmd/for
- Go 1.20 or later
-config string
: Path to the configuration file (default:./config.yaml
).-playbook string
: Path to the playbook file.-help
: Show help message.-t string
: Ad hoc task to run (e.g., 'command').-g string
: Group to run ad hoc task on.-local
: Run commands and scripts locally without SSH.
Run an ad hoc task locally without specifying a configuration file or group:
./for -t "uptime" -local
Run a local playbook:
./for -playbook playbook.yaml -local
-
Create a configuration file
config.yaml
:inventory_file: "hosts.ini" ssh_user: "yourusername" ssh_key_path: "/path/to/your/private/key" run_locally: false
-
Create a playbook file
playbook.yaml
:- name: Apply webserver service hosts: webservers services: - service: example_service - name: Apply dbserver service hosts: dbservers services: - service: example_service
-
Create an inventory file
hosts.ini
:[webservers] 192.168.1.10 192.168.1.11 [dbservers] 192.168.1.20 192.168.1.21
-
Create a service file
services/example_service/tasks/main.yaml
:- name: Update package index command: sudo apt-get update - name: Upgrade packages command: sudo apt-get upgrade -y
-
Run the tool with the configuration and playbook file:
./for -config config.yaml -playbook playbook.yaml
Run an ad hoc task on the webservers
group:
./for -config config.yaml -g webservers -t "uptime"
Run a script on the webservers
group:
./for -config config.yaml -g webservers -t "/path/to/script.sh"
The configuration file should be in YAML format and include the path to the inventory file, SSH user details, SSH key path, and a flag to indicate if commands should be run locally.
Example config.yaml
:
inventory_file: "hosts.ini"
ssh_user: "yourusername"
ssh_key_path: "/path/to/your/private/key"
run_locally: false
The playbook file should be in YAML format and include the services to be applied to the specified hosts.
Example playbook.yaml
:
- name: Apply webserver service
hosts: webservers
services:
- service: example_service
- name: Apply dbserver service
hosts: dbservers
services:
- service: example_service
Services should be organized in directories under services/
with a tasks/main.yaml
file defining the tasks.
Example services/example_service/tasks/main.yaml
:
- name: Update package index
command: sudo apt-get update
- name: Upgrade packages
command: sudo apt-get upgrade -y
- Go 1.20 or later
-
Clone the repository:
git clone https://github.com/diceone/for.git cd for
-
Build the binary:
go build -o for ./cmd/for
Currently, tests are not implemented. Contributions are welcome!
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
This project is licensed under the MIT License. See the LICENSE file for details.