Use curly braces instead of brackets for Terraform list

1/21/2020

I want to install the official jenkins helm chart using Terraform which then invokes Ansible.

Ideally, I would use the Terraform helm provider but it has a severe limitation when being invoked over tls.

Therefore I am performing the actual helm installation via the following local-exec installation.

  provisioner "local-exec" {
    command = "ansible-playbook -vvv ${path.module}/playbooks/playbook.yaml -i ./playbooks/hosts --extra-vars \"{ \"k8s_cluster_name\": \"${local.k8s_name}\", \"zonename\": \"${var.zone}\", \"env_name\": \"${var.env_name}\", \"google_project_name\": \"${var.project_id}\", \"jenkins_master_image_tag\": \"${var.jenkins_master_image_tag}\", \"jenkins_agent_image_tag\": \"${var.jenkins_agent_image_tag}\", \"jenkins_plugins_list\": \"${var.jenkins_plugins_list}\" }\""
  }
}

My purpose is to be able to pass the plugins list used in the values file as argument (i.e. extra var) to ansible cmd.

Therefore I declared the following var:

variable "jenkins_plugins_list" {
  description = "The list of jenkins' plugins to be installed"
  type = "list"
  default = ["kubernetes:1.21.2", "workflow-job:2.36", "workflow-aggregator:2.6", "credentials-binding:1.20", "git:4.0.0"]
}

The particular ansible task that will use the jenkins_plugins_list variable is the following:

    - name: Install Jenkins
      register: jenkins_init
      shell: "helm upgrade --force --tls --install --set master.installPlugins={{ jenkins_plugins_list }}  --set master.tag={{ jenkins_master_image_tag }} --set agent.tag={{ jenkins_agent_image_tag }} -f {{ tempdir }}/{{ env_name }}-jenkins/jenkins-values.yaml jenkins-{{ env_name }} --namespace jenkins stable/jenkins"
      retries: 5
      delay: 30
      until: jenkins_init.rc == 0

I am trying to explicitly set the master.installPlugins variable via the tf variable passed during the ansible invocation.

The problem is the following:

The helm command accepts the list i.e. the value corresponding to the variable jenkins_plugins_list as follows:

helm upgrade --force --tls --install --set master.installPlugins="{kubernetes:1.21.2,git:4.0.0}"

i.e. with quotes and curly braces.

However I have only managed to produce the following output:

helm upgrade --force --tls --install --set master.installPlugins=[u'kubernetes:1.21.2', u'workflow-job:2.36', u'workflow-aggregator:2.6', u'credentials-binding:1.20', u'git:4.0.0'] -f /org_files/tmp/myenv-jenkins/jenkins-values.yaml jenkins-myenv --namespace jenkins stable/jenkins"
-- pkaramol
ansible
kubernetes
kubernetes-helm
terraform

1 Answer

1/21/2020

You get the list as a... list, which is a good thing ;)

You need to transform that to "whatever that format is" you need, which is the list of element separated by comas and surrounded by curly braces.

See the below example. Syntax is a little tricky to bypass the triple curly braces in the expression that cause an interpretation error:

---
- hosts: localhost
  gather_facts: false

  vars:
    jenkins_plugins_list: ['kubernetes:1.21.2', 'workflow-job:2.36', 'workflow-aggregator:2.6', 'credentials-binding:1.20', 'git:4.0.0']

  tasks:
    - debug:
        msg: "{{ '{' + jenkins_plugins_list | join(',') + '}' }}"

Which gives

PLAY [localhost] ********************************************************************************************************************************************************************************************************************************************************

TASK [debug] ************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "{kubernetes:1.21.2,workflow-job:2.36,workflow-aggregator:2.6,credentials-binding:1.20,git:4.0.0}"
}

PLAY RECAP **************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
-- Zeitounator
Source: StackOverflow