yamlpuppetbolt

How to properly connect steps with a puppet bolt plan (yaml format)


I am creating a puppet bolt plan to install docker on 4 hosts, enable swarm on the first host, and then add the other 3 hosts to that swarm.

My issue is I do not know how to save this section of the yaml to a variable.

steps:
    - command: docker swarm init
      targets: $firstmanagementnode

Which I need because the output contains a key to allow other hosts to join the swarm later on in the plan

There is an example in Puppet's documentation below, but honestly I can make heads or tails of it

steps:
  - name: hostnames
    command: hostname -f
    targets: $targets
  - task: echo
    parameters:
      message: $hostnames.map |$hostname_result| { $hostname_result['stdout'] }.join(',')

I was able to solve my problem with the yaml and task below:

YAML:

  - name: initswarm
    command: docker swarm init
    targets: $firstmanagementnode
  - name: managertoken
    command: docker swarm join-token manager
    targets: $firstmanagementnode
  - name: managersjoin
    task: docker_swarm::joinswarm
    targets: $managernodes
    parameters:
      masternode: $managertoken.map |$token_result| { $token_result['stdout'] }.join(',')
    description: configure additional manager nodes

task:

#!/bin/bash

$(echo $PT_masternode | grep -o 'docker.*2377')

task json:

{
    "description": "joins a docker swarm",
    "input_method": "both",
    "parameters": {
        "masternode": {
            "description": "The first master node",
            "type": "String"
        }
    }
}

Solution

  • Have you tried looking at the Docker module on the forge https://forge.puppet.com/puppetlabs/docker.

    A lot of the work you're trying to do has already been done as stand alone tasks and you probably just need to wrap this into a plan.

    Take a look at the tasks here https://github.com/puppetlabs/puppetlabs-docker/tree/main/tasks the .rb files is the code and the .json is effectively the man page, if you pull this module onto you're Bolt server you'll be able to run a bolt task show docker::swarm_join to get more info.

    The tasks here initialise the swarm, there is another task called swarm_token that'll return your token to be used in a plan and pass into swarm_join.

    There isn't a task to install docker but all you'd need to do in your plan would be to add include docker at the start of you're plan.