Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible SHA1 encrypt variable

how can I encode a given string using ansible?

I'm trying to do this:

echo -n "mypassword" | sha1sum
like image 842
Green Avatar asked Feb 15 '26 20:02

Green


1 Answers

Q: "Encode a given string."

A: Use hash filter. For example, the command and the filter

    - shell: echo -n "mypassword" | sha1sum
      register: result
    - debug:
        var: result.stdout
    - set_fact:
        pswd: "{{ 'mypassword' | hash('sha1') }}"
    - debug:
        var: pswd

give the same results

    "result.stdout": "91dfd9ddb4198affc5c194cd8ce6d338fde470e2  -"

    "pswd": "91dfd9ddb4198affc5c194cd8ce6d338fde470e2"

Q: "Insert the output string into a file."

A: For example, use the template

shell> cat templates/pswd.j2.txt 
pswd: {{ pswd }}
    - template:
        src: pswd.j2.txt
        dest: pswd.txt

give

shell> cat pswd.txt 
pswd: 91dfd9ddb4198affc5c194cd8ce6d338fde470e2
like image 98
Vladimir Botka Avatar answered Feb 18 '26 17:02

Vladimir Botka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!