jinja2 - How to include encrypted file in Ansible jinja template? - Stack Overflow

I have 2 jinja templates containing private key:private.key (encrypted)private-copy.key (need to inclu

I have 2 jinja templates containing private key:

  • private.key (encrypted)
  • private-copy.key (need to include content of private.key)

private.key file is encrypted using ansible vault, e.g.

ansible-vault encrypt --vault-password-file ~/.pass.txt private.key

so its content looks like this:

$ANSIBLE_VAULT;1.1;AES256
123456789[...]

I'm trying to get the decrypted content of private.key

-----BEGIN RSA PRIVATE KEY-----
ABCDEfgh[...]
-----END RSA PRIVATE KEY-----

inside private-copy.key (and not its encrypted value).

I've tried the include jinja statement inside private-copy.key:

{% include "private.key" %}

but the result gives me the encrypted value in private-copy.key instead of the decrypted content of private.key.

I also know that I can use an encrypted variable and put it in private-copy.key instead of including encrypted file. For simplification purpose, I want to use directly the encrypted file (on certificates renewal, we get a file, I just wanted to use it directly).

I have 2 jinja templates containing private key:

  • private.key (encrypted)
  • private-copy.key (need to include content of private.key)

private.key file is encrypted using ansible vault, e.g.

ansible-vault encrypt --vault-password-file ~/.pass.txt private.key

so its content looks like this:

$ANSIBLE_VAULT;1.1;AES256
123456789[...]

I'm trying to get the decrypted content of private.key

-----BEGIN RSA PRIVATE KEY-----
ABCDEfgh[...]
-----END RSA PRIVATE KEY-----

inside private-copy.key (and not its encrypted value).

I've tried the include jinja statement inside private-copy.key:

{% include "private.key" %}

but the result gives me the encrypted value in private-copy.key instead of the decrypted content of private.key.

I also know that I can use an encrypted variable and put it in private-copy.key instead of including encrypted file. For simplification purpose, I want to use directly the encrypted file (on certificates renewal, we get a file, I just wanted to use it directly).

Share Improve this question asked Nov 20, 2024 at 15:50 TitouTitou 4143 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Use the file lookup plugin: Ansible allows you to read the content of a file using the lookup('file', ...) plugin. When combined with ansible-vault for encrypted files, it automatically decrypts the content if the file is encrypted.

# This is private-copy.key
{{ lookup('file', 'private.key') }}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742347241a4426793.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信