Ansible - Encode a string using the b64encode filter

The b64encode filter is used to encode a string. The b64decode filter is used to decode a string. In this example, string foo is encoded.

Notice in this example that foo is wrapped in quotes. When wrapped in quotes, foo is interpreted as a string. If foo were not wrapped in quotes, foo would be interpreted as a variable.

- name: "encoded the 'foo' string"
  debug:
    msg: "{{ 'foo' | b64encode }}"

 

The following should be returned.

TASK [encode the 'foo' string]
ok: [server1.example.com] => {
    "msg": [
        "Zm9v"
    ]
}

 

Let's say you are using the vars plugin to create a variable. In this example, the foo variable contains a value of Hello.

vars:
  foo: "Hello"

 

Here is how you would encode the foo variable, which will encode value Hello. Notice that foo is not wrapped in quotes, so that foo is interpreted as a variable, not a string.

- name: "encoded the 'foo' variable"
  debug:
    msg: "{{ foo | b64encode }}"

 

The following should be returned.

TASK [encode the 'foo' variable]
ok: [server1.example.com] => {
    "msg": [
        ""SGVsbG8="
    ]
}


Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter 63b63 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |