{"id":5513,"date":"2026-11-15T17:51:24","date_gmt":"2026-11-15T17:51:24","guid":{"rendered":"https:\/\/geekmungus.co.uk\/?p=5513"},"modified":"2026-11-15T17:51:24","modified_gmt":"2026-11-15T17:51:24","slug":"ansible-user-module-password-generate-password-hash","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=5513","title":{"rendered":"Ansible User Module Password &#8211; Generate Password Hash"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A common issue when using the Ansible &#8220;user&#8221; module is setting a password. Although you shouldn&#8217;t be writing the password direct into your Ansible YML files, unless this is for testing, or you intend to replace them with something to store them securely and retrieve them at runtime, for example Ansible Vault; you need to be aware that Ansible &#8220;user&#8221; module expects the password in a certain format.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There&#8217;s a number of different ways to get to something suitable, but in this example I&#8217;m going to show you <strong>mkpasswd<\/strong>. We first need to install it with (assuming you are using Ubuntu\/Debian):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install whois<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then to generate a SHA-512 hash you can use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkpasswd --method=sha-512 --salt='thesaltvalue' 'supersecretpassword'<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"788\" height=\"40\" src=\"https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2026\/11\/image-2.png\" alt=\"\" class=\"wp-image-5514\" srcset=\"https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2026\/11\/image-2.png 788w, https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2026\/11\/image-2-749x38.png 749w, https:\/\/geekmungus.co.uk\/wp-content\/uploads\/2026\/11\/image-2-300x15.png 300w\" sizes=\"auto, (max-width: 788px) 100vw, 788px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll notice I stuck in a salt too. The salt (assuming its different for each user) ensures that the generated password is also different. Without a salt, if you put in the same plain text password string, you&#8217;d end up with the same hash; and that risks easier cracking.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now we have this string, we can put it into the ansible create user stanza within our Ansible markup, for example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>- name: Create Users\n  user:\n    name: \"{{ item.username }}\"\n    groups: \"{{ item.groups }}\"\n    generate_ssh_key: yes\n    ssh_key_comment: \"{{ item.username }}@{{ ansible_fqdn }}\"\n    password: $6$thesaltvalue$SMif0lqr.9gs\/BlCQZWgSO0SAhIDJXmsn6WogfcBMYSyREOImiW\/7JH8x62ANjbwYVakxGVL6f7KryPXToOCZ0\n  loop: \"{{ users }}\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Quick note on the structure of the string. The <strong>$6$<\/strong> at the start specifies the hashing algorithm used, $6$ is SHA-512 (recommended), $5$ is SHA-256 (ok &#8211; but use 512 if possible) or $1$ is MD5 (don&#8217;t use!).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A common issue when using the Ansible &#8220;user&#8221; module is setting a password. Although you shouldn&#8217;t be writing the password direct into your Ansible YML files, unless this is for testing, or you intend to replace them with something to store them securely and retrieve them at runtime, for example Ansible Vault; you need to &#8230; <a title=\"Ansible User Module Password &#8211; Generate Password Hash\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=5513\" aria-label=\"Read more about Ansible User Module Password &#8211; Generate Password Hash\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":4522,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-5513","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ansible"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/5513","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5513"}],"version-history":[{"count":1,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/5513\/revisions"}],"predecessor-version":[{"id":5515,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/5513\/revisions\/5515"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/media\/4522"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}