Howto use the WebUI Variables in hcl script?

I have a variable (ConnectionString) setup in the basic Nomad WebUi, but I can’t figure out how to access it from the task part of my Job. I’d like to set and env variable from it for my docker image.

Have tried var.ConnectionString, using templates but nothing worked. The docs are not at all helpful on this, there is nothing that references the WebUI variables.

Any suggestions? Thanks.

nb. This is just a test cluster, I have no ACL’s setup.

image

It’s easy but you have to be careful with the scope. For example, for a path var nomad/jobs (like yours) this vars might be accesible for all jobs in the cluster if you configure it.

If you change it to nomad/jobs/<some-group/ it might be accesible just for this group. Same happens with tasks nomad/jobs/<some-group>/<some-task>.

If you want to create a var that might be accesible for other purposes just don’t start the path with nomad.

Answering your question you have to add a template stanza inside a task and then inside the template stanza call the vars as follows.

job "phpmyadmin" {
    datacenters = ["dc1"]
    group "phpmyadmin" {
        count = 1
        network {
            port "phpmyadmin-port" {
                static = 8081
                to = 80
            }
        }

        task "phpmyadmin" {
            driver = "docker"
            config {
                image = "phpmyadmin:apache"
                ports = [ "phpmyadmin-port" ]
            }

            template {
                destination = "secrets/prod-env"
                env = true
                data = <<EOH
                {{ with nomadVar "nomad/jobs/phpmyadmin" }}
                PMA_HOST={{ .PMA_HOST }}
                PMA_CONTROLUSER={{ .PMA_CONTROLUSER }}
                PMA_CONTROLPASS={{ .PMA_CONTROLPASS }}
                {{ end }}
                EOH
            }
        }
    }
}

In this case I have a nomad/jobs/phpmyadmin var and I have created env variable from them (env = true). My var path has 3 variables PMA_HOST, PMA_CONTROLUSER and PMA_CONTROLPASS

1 Like

Thank you! Got it working perfectly from that, have a better understanding now.