roles_path=../roles # Convert string to valid Ansible variable name convert_to_ansible_var() { local input="$1" # Replace hyphens, and spaces with underscores local result=$(echo "$input" | tr '-' '_' | tr ' ' '_') # Remove any characters that aren't alphanumeric or underscore result=$(echo "$result" | tr -cd '[:alnum:]_') # Ensure it doesn't start with a number (prepend underscore if it does) if [[ "$result" =~ ^[0-9] ]]; then result="_${result}" fi # Convert to lowercase (Ansible convention) result=$(echo "$result" | tr '[:upper:]' '[:lower:]') echo "$result" } # Check if any arguments were provided if [ $# -eq 0 ]; then echo "No arguments provided." echo "Usage: $0 role1 role2 ..." exit 1 fi pushd $roles_path for arg in "$@"; do echo "Creating directory $arg" mkdir $arg cd $arg echo "Creating subdirectories" mkdir {defaults,templates,meta} echo "Creating main.yml files" touch {defaults,meta}/main.yml touch templates/compose.yml.j2 app_name=$(convert_to_ansible_var "$arg") # create files cat > defaults/main.yml << EOF --- app_name: $arg # Container configuration ${app_name}_container_name: "{{ app_name | default('$arg') }}" ${app_name}_container_version: latest ${app_name}_restart_policy: "{{ app_restart_policy }}" # Network configuration # ${app_name}_http_port: 8080 # Volume paths # ${app_name}_config_path: "{{ app_dir }}/config" # ${app_name}_data_path: "{{ app_dir }}/data" # App-specific configuration # Add custom variables here # Directory structure # app_subdirectories: # - config # - data # Backup configuration # app_backup_subdirectories: # - config EOF cat > meta/main.yml << EOF --- galaxy_info: author: description: Deploy $arg with Docker Compose license: MIT dependencies: - role: docker_compose_app vars: app_role_name: $arg EOF cat > templates/compose.yml.j2 << EOF --- services: ${arg}: image: "your-image:{{ ${app_name}_container_version }}" container_name: "{{ ${app_name}_container_name }}" restart: "{{ ${app_name}_restart_policy }}" # ports: # - "{{ ${app_name}_http_port }}:8080" # volumes: # - "{{ ${app_name}_config_path }}:/config" # - "{{ ${app_name}_data_path }}:/data" EOF cd .. done popd