sprig funcs

Demo usecases of sprig golang template functions

Showcase deal with data types

Please refer to usage relating to dict/map/slice/b64

env variables

- name: sprig_os_env
  value: '{{ env "HOME"}}'
  flags: [vvvv,]

- name: sprig_os_env_expand
  value: '{{ expandenv "Your path is set to $PATH" }} '
  flags: [vvvv,]
  • env func get the value of HOME
  • expandenv func is to expand and subsitude the value in runtime

showcase encrpt and decrypt using dynamic var

Please notice the usage of encrypt and decrypt examples:

- name: sprig_encrypt_AES
  value: '{{ encryptAES "secretkey" "plaintext" }}'
  flags: [vvvv,]

- name: sprig_decrypt_AES
  value: '{{ print .sprig_encrypt_AES |decryptAES "secretkey" }}'
  flags: [vvvv,]

- name: sprig_AES_key
  value: '{{ printf "%s-%s" .pod_name .ns }}'
  flags: [vvvv,]

- name: sprig_encrypt_AES_using_key_var
  value: '{{ encryptAES .sprig_AES_key "plaintext" }}'
  flags: [vvvv,]

- name: sprig_decrypt_AES_using_key_var
  value: '{{ print .sprig_encrypt_AES_using_key_var |decryptAES .sprig_AES_key }}'
  flags: [vvvv,]

Demo

source

Main task yaml file
    vars:
      ns: prod
      pod_name: web_app
      ha: true
      age: 34
      old: 54
      admins: [tom, jason, alice]
      managers:
      - tom
      - jason
      - alice
      student:
        name: Tom
        gender: Male
        teachers:
        - tom
        - jason
        - alice
        address:
          suburb:
            name: sydney
            postcode: 2000
            CBD: yes
          school: Sydney Grammar
    dvars:
    - name: sprig_trim
      value: '{{ trim "  hello       "}}'
      desc: The trim function removes space from either side of a string
      flags: [vvvv]
    - name: sprig_trim_all
      value: '{{ trimAll "$" "$5.00"}}'
      desc: Remove given characters from the front or back of a string
      flags: [vvvv]
    - name: sprig_trim_suffix
      value: '{{ trimSuffix "-" "hello-"}}'
      desc: Trim just the suffix from a string
      flags: [vvvv]
    - name: sprig_trim_prefix
      value: '{{ trimPrefix "-" "-hello"}}'
      desc: Trim just the preffix from a string
      flags: [vvvv]
    - name: sprig_upper
      value: '{{ upper .pod_name}}'
      flags: [vvvv]
    - name: sprig_repeat
      value: '{{ repeat 3 .pod_name }}'
      flags: [vvvv]
    - name: sprig_repeat_with_space
      value: '{{ printf "%s%s%s" " [" .pod_name "] "}}'
      flags: [vvvv]
    - name: sprig_repeat_with_space_repeat
      value: '{{ printf "%s%s%s" " [" .pod_name "] " |repeat 3}}'
      flags: [vvvv]
    - name: sprig_substring
      value: '{{ substr 0 5 "hello world" }}'
      flags: [vvvv]
    - name: sprig_nospace
      value: '{{ nospace "hello w o r l d" }}'
      flags: [vvvv]
    - name: sprig_encrypt_AES
      value: '{{ encryptAES "secretkey" "plaintext" }}'
      flags: [vvvv]
    - name: sprig_decrypt_AES
      value: '{{ print .sprig_encrypt_AES |decryptAES "secretkey" }}'
      flags: [vvvv]
    - name: sprig_AES_key
      value: '{{ printf "%s-%s" .pod_name .ns }}'
      flags: [vvvv]
    - name: sprig_encrypt_AES_using_key_var
      value: '{{ encryptAES .sprig_AES_key "plaintext" }}'
      flags: [vvvv]
    - name: sprig_decrypt_AES_using_key_var
      value: '{{ print .sprig_encrypt_AES_using_key_var |decryptAES .sprig_AES_key }}'
      flags: [vvvv]
    - name: sprig_slice
      value: '{{ slice .admins 1 3 }}'
      flags: [vvvv]
    - name: sprig_slice_new
      value: '{{  list 1 2 3 4 5 |print }}'
      flags: [vvvv]
    - name: sprig_slice_assign
      value: '{{ $myList := list 1 2 3 4 5 }} {{  print $myList }} '
      flags: [vvvv]
    - name: sprig_slice_append
      value: '{{ $myList := list 1 2 3 4 5 }} {{  $newlist := append $myList 6 }} {{  print
        $newlist }} '
      flags: [vvvv]
    - name: sprig_slice_concat
      value: '{{ $myList := list 1 2 3 4 5 }} {{  $newlist:=concat $myList ( list 6 7
        ) ( list 8 ) }} {{  print $newlist }} '
      flags: [vvvv]
    - name: sprig_slice_reverse
      value: '{{  list 1 2 3 4 5 |reverse }}'
      flags: [vvvv]
    - name: sprig_slice_uniq
      value: '{{  list 1 4 2 3 3 4 5 |uniq }}'
      flags: [vvvv]
    - name: var_slice_index
      desc: check if the builtin will still work
      value: "{{ index .admins 1 }}"
      flags: [vvvv]
      #  - name: sprig_slice_filter_out
      #    value: '{{  list 1 2 3 4 5 |without 1 3 }}'
      #    desc: how to use pipe out instead???
      #    flags: [vvvv,]
    - name: sprig_slice_filter_out
      value: '{{ $myList := list 1 2 3 4 5 }} {{  without $myList 2 3 5 |print}} '
      flags: [vvvv]
    - name: sprig_slice_contains
      value: '{{  list 1 2 3 4 5 |has 4 }}'
      desc: Test to see if a list has a particular element
      flags: [vvvv]
    - name: sprig_string_contains
      value: '{{ "hello, world" |contains "world"}}'
      desc: Test if a string contains substr
      flags: [v]
    - name: sprig_slice_compact
      value: '{{  $list := list 1 "a" "foo" ""  }} {{    $copy := compact $list  }} {{   print
        $copy }}'
      desc: compact will return a new list with the empty (i.e., “”) item removed
      flags: [vvvv]
    - name: sprig_b64enc
      value: '{{  .pod_name|b64enc }}'
      flags: [vvvv]
    - name: sprig_b64dec
      value: '{{ .sprig_b64enc |b64dec }} '
      flags: [vvvv]
    - name: random_hello
      value: '{{ hello }}'
      flags: [vvvv]
    - name: random_hello_plain
      value: '{{ "hi, tom"}}'
      flags: [vvvv]
    - name: sprig_os_env
      value: '{{ env "HOME"}}'
      flags: [vvvv]
    - name: sprig_os_env_expand
      value: '{{ expandenv "Your path is set to $PATH" }} '
      flags: [vvvv]
    - name: sprig_dict
      value: ' {{ $myDict := dict "name1" "value1" "name2" "value2" "name3" "value 3"
        }} {{ println $myDict}} {{ print $myDict.name2}} '
      flags: [vvvv]
    - name: sprig_dict_access
      value: ' {{ println .student.address}} '
      flags: [vvvv]
    - name: sprig_dict_access_using_func
      value: ' {{ get .student "address"}} '
      desc: checkout other dic/map funcs here http://masterminds.github.io/sprig/dicts.html
      flags: [vvvv]
    - name: add1
      value: '{{ add1 "100" }}'
      flags: [vvvv]
    - name: add
      value: '{{ add "100" "23" }}'
      flags: [vvvv]
    - name: convert_and_add1
      value: '{{ atoi "010" | add1 }}'
      flags: [vvvv]
    - name: convert_and_add1_2
      value: '{{ atoi "0010" | add1 }}'
      flags: [vvvv]
    - name: sprig_substring2
      value: '{{ substr 1 5 "c0011" }}'
      flags: [vvvv]
    - name: sprig_word_count
      value: '{{ wordcount "c0011 asdf hello" }}'
      flags: [vvv]
    - name: sprig_muliply
      value: '{{ mul 4 5 3 }}'
      flags: [vvv]
    - name: sprig_date
      desc: 20060102 is the format
      value: '{{now|date "20060102"}}'
      flags: [v]
    tasks:
    - name: task
      task:
      - func: shell
        do:
        - echo "check the value of other dvar using vvvv flag print out"
    
Main log file
    loading [Config]:  ./tests/functests/upconfig.yml
    Main config:
                 Version -> 1.0.0
                  RefDir -> ./tests/functests
                 WorkDir -> cwd
              AbsWorkDir -> /up_project/up
                TaskFile -> c0037
                 Verbose -> vvv
              ModuleName -> self
               ShellType -> /bin/sh
           MaxCallLayers -> 8
                 Timeout -> 3600000
     MaxModuelCallLayers -> 256
               EntryTask -> task
      ModRepoUsernameRef -> 
      ModRepoPasswordRef -> 
    work dir: /up_project/up
    -exec task: task
    loading [Task]:  ./tests/functests/c0037
    module: [self], instance id: [dev], exec profile: []
    profile -  envVars:
    
    (*core.Cache)({
    })
    
    dvar> sprig_trim:
    "hello"
    
    -
    hello
    dvar> sprig_trim_all:
    "5.00"
    
    -
    5.00
    dvar> sprig_trim_suffix:
    "hello"
    
    -
    hello
    dvar> sprig_trim_prefix:
    "hello"
    
    -
    hello
    dvar> sprig_upper:
    "WEB_APP"
    
    -
    WEB_APP
    dvar> sprig_repeat:
    "web_appweb_appweb_app"
    
    -
    web_appweb_appweb_app
    dvar> sprig_repeat_with_space:
    " [web_app] "
    
    -
     [web_app] 
    dvar> sprig_repeat_with_space_repeat:
    " [web_app]  [web_app]  [web_app] "
    
    -
     [web_app]  [web_app]  [web_app] 
    dvar> sprig_substring:
    "hello"
    
    -
    hello
    dvar> sprig_nospace:
    "helloworld"
    
    -
    helloworld
    dvar> sprig_encrypt_AES:
    "Mja5/hd/u9J9IKz8WXoC/YXA4+JDeaA9da40Pdcxdc0="
    
    -
    Mja5/hd/u9J9IKz8WXoC/YXA4+JDeaA9da40Pdcxdc0=
    dvar> sprig_decrypt_AES:
    "plaintext"
    
    -
    plaintext
    dvar> sprig_AES_key:
    "web_app-prod"
    
    -
    web_app-prod
    dvar> sprig_encrypt_AES_using_key_var:
    "5XCUZnx28G4Sh0Kno7fx41ZE5DK1pTtbbqV0DvTthjM="
    
    -
    5XCUZnx28G4Sh0Kno7fx41ZE5DK1pTtbbqV0DvTthjM=
    dvar> sprig_decrypt_AES_using_key_var:
    "plaintext"
    
    -
    plaintext
    dvar> sprig_slice:
    "[jason alice]"
    
    -
    [jason alice]
    dvar> sprig_slice_new:
    "[1 2 3 4 5]"
    
    -
    [1 2 3 4 5]
    dvar> sprig_slice_assign:
    " [1 2 3 4 5] "
    
    -
     [1 2 3 4 5] 
    dvar> sprig_slice_append:
    "  [1 2 3 4 5 6] "
    
    -
      [1 2 3 4 5 6] 
    dvar> sprig_slice_concat:
    "  [1 2 3 4 5 6 7 8] "
    
    -
      [1 2 3 4 5 6 7 8] 
    dvar> sprig_slice_reverse:
    "[5 4 3 2 1]"
    
    -
    [5 4 3 2 1]
    dvar> sprig_slice_uniq:
    "[1 4 2 3 5]"
    
    -
    [1 4 2 3 5]
    dvar> var_slice_index:
    "jason"
    
    -
    jason
    dvar> sprig_slice_filter_out:
    " [1 4] "
    
    -
     [1 4] 
    dvar> sprig_slice_contains:
    "true"
    
    -
    true
    dvar> sprig_string_contains:
    "true"
    
    -
    true
    dvar> sprig_slice_compact:
    "  [1 a foo]"
    
    -
      [1 a foo]
    dvar> sprig_b64enc:
    "d2ViX2FwcA=="
    
    -
    d2ViX2FwcA==
    dvar> sprig_b64dec:
    "web_app "
    
    -
    web_app 
    dvar> random_hello:
    "Hello!"
    
    -
    Hello!
    dvar> random_hello_plain:
    "hi, tom"
    
    -
    hi, tom
    dvar> sprig_os_env:
    "/root"
    
    -
    /root
    dvar> sprig_os_env_expand:
    "Your path is set to /bin:/nix/var/nix/profiles/default/bin:/nix/var/nix/profiles/default/sbin:/bin:/sbin:/usr/bin:/usr/sbin "
    
    -
    Your path is set to /bin:/nix/var/nix/profiles/default/bin:/nix/var/nix/profiles/default/sbin:/bin:/sbin:/usr/bin:/usr/sbin 
    dvar> sprig_dict:
    "  map[name1:value1 name2:value2 name3:value 3]\n value2 "
    
    -
      map[name1:value1 name2:value2 name3:value 3]
     value2 
    dvar> sprig_dict_access:
    " map[school:Sydney Grammar suburb:map[cbd:true name:sydney postcode:2000]]\n "
    
    -
     map[school:Sydney Grammar suburb:map[cbd:true name:sydney postcode:2000]]
     
    dvar> sprig_dict_access_using_func:
    " map[school:Sydney Grammar suburb:map[cbd:true name:sydney postcode:2000]] "
    
    -
     map[school:Sydney Grammar suburb:map[cbd:true name:sydney postcode:2000]] 
    dvar> add1:
    "101"
    
    -
    101
    dvar> add:
    "123"
    
    -
    123
    dvar> convert_and_add1:
    "11"
    
    -
    11
    dvar> convert_and_add1_2:
    "11"
    
    -
    11
    dvar> sprig_substring2:
    "0011"
    
    -
    0011
    dvar> sprig_word_count:
    "3"
    
    -
    3
    dvar> sprig_muliply:
    "60"
    
    -
    60
    dvar> sprig_date:
    "20201006"
    
    -
    20201006
    Task1: [task ==> task:  ]
    -Step1:
    self: final context exec vars:
    
    (*core.Cache)({
      "add1": "101",
      "sprig_dict_access": " map[school:Sydney Grammar suburb:map[cbd:true name:sydney postcode:2000]]\n ",
      "sprig_encrypt_AES_using_key_var": "5XCUZnx28G4Sh0Kno7fx41ZE5DK1pTtbbqV0DvTthjM=",
      "convert_and_add1": "11",
      "sprig_nospace": "helloworld",
      "sprig_b64dec": "web_app ",
      "sprig_encrypt_AES": "Mja5/hd/u9J9IKz8WXoC/YXA4+JDeaA9da40Pdcxdc0=",
      "sprig_slice_reverse": "[5 4 3 2 1]",
      "sprig_slice_uniq": "[1 4 2 3 5]",
      "admins": {
        "tom",
        "jason",
        "alice"
      },
      "add": "123",
      "sprig_upper": "WEB_APP",
      "ha": true,
      "random_hello_plain": "hi, tom",
      "sprig_AES_key": "web_app-prod",
      "sprig_trim_suffix": "hello",
      "sprig_word_count": "3",
      "sprig_trim_all": "5.00",
      "sprig_slice_append": "  [1 2 3 4 5 6] ",
      "sprig_date": "20201006",
      "old": 54,
      "sprig_repeat": "web_appweb_appweb_app",
      "sprig_substring2": "0011",
      "sprig_slice_new": "[1 2 3 4 5]",
      "sprig_trim": "hello",
      "pod_name": "web_app",
      "var_slice_index": "jason",
      "sprig_slice_concat": "  [1 2 3 4 5 6 7 8] ",
      "convert_and_add1_2": "11",
      "random_hello": "Hello!",
      "age": 34,
      "ns": "prod",
      "sprig_repeat_with_space_repeat": " [web_app]  [web_app]  [web_app] ",
      "sprig_os_env": "/root",
      "sprig_slice_filter_out": " [1 4] ",
      "managers": {
        "tom",
        "jason",
        "alice"
      },
      "sprig_dict": "  map[name1:value1 name2:value2 name3:value 3]\n value2 ",
      "sprig_substring": "hello",
      "sprig_repeat_with_space": " [web_app] ",
      "sprig_dict_access_using_func": " map[school:Sydney Grammar suburb:map[cbd:true name:sydney postcode:2000]] ",
      "sprig_b64enc": "d2ViX2FwcA==",
      "sprig_trim_prefix": "hello",
      "sprig_slice": "[jason alice]",
      "sprig_os_env_expand": "Your path is set to /bin:/nix/var/nix/profiles/default/bin:/nix/var/nix/profiles/default/sbin:/bin:/sbin:/usr/bin:/usr/sbin ",
      "sprig_slice_assign": " [1 2 3 4 5] ",
      "up_runtime_task_layer_number": 0,
      "sprig_decrypt_AES_using_key_var": "plaintext",
      "sprig_muliply": "60",
      "student": {
        "gender": "Male",
        "teachers": {
          "tom",
          "jason",
          "alice"
        },
        "address": {
          "suburb": {
            "name": "sydney",
            "postcode": 2000,
            "cbd": true
          },
          "school": "Sydney Grammar"
        },
        "name": "Tom"
      },
      "sprig_slice_compact": "  [1 a foo]",
      "sprig_string_contains": "true",
      "sprig_decrypt_AES": "plaintext",
      "sprig_slice_contains": "true"
    })
    
    cmd( 1):
    echo "check the value of other dvar using vvvv flag print out"
    
    -
    check the value of other dvar using vvvv flag print out
    
    -
     .. ok
    . ok
    
Logs with different verbose level
Raw logs with different verbose level

Other references: