How to use variables in a Jinja2 filter?

35 views
Skip to first unread message

David Li

unread,
Oct 10, 2016, 11:22:55 AM10/10/16
to Ansible Project
Hi,

I am trying to create an user with password hash filter like this:

- name: Create a user
  user: 
     name: abc
     group: xyz
     password: {{ 'mypass'|password_hash('sha256', 'mysalt') }}


This works!
Now I would like to use variables in place of 'mypass' and 'mysault' that are defined in vars/main.yml (encrypted by vault)

---
password: 'mypass'
password_sault: 'mysault'

so the playbook would look like this:

- name: Create a user
  user: 
     name: abc
     group: xyz
     password: {{ '{{password}}'|password_hash('sha256', '{{password_sault}}') }}

However this doesn't really work. What's the right way to do this?
Thanks.

David


Kai Stian Olstad

unread,
Oct 10, 2016, 12:20:15 PM10/10/16
to ansible...@googlegroups.com
On 09. okt. 2016 21:18, David Li wrote:
> ---
> password: 'mypass'
> password_sault: 'mysault'
>
> so the playbook would look like this:
>
> - name: Create a user
> user:
> name: abc
> group: xyz
> password: {{ '{{password}}'|password_hash('sha256',
> '{{password_sault}}') }}
>
> However this doesn't really work. What's the right way to do this?
> Thanks.

Curly brackets doesn't stack, just use the variable directly without quotes.

password: "{{ password | password_hash('sha256', password_sault) }}"

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages