{"id":7934,"date":"2015-11-10T12:47:23","date_gmt":"2015-11-10T12:47:23","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/10\/specify-sudo-password-for-ansible-open-source-projects-ansible-ansible\/"},"modified":"2015-11-10T12:47:23","modified_gmt":"2015-11-10T12:47:23","slug":"specify-sudo-password-for-ansible-open-source-projects-ansible-ansible","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/10\/specify-sudo-password-for-ansible-open-source-projects-ansible-ansible\/","title":{"rendered":"Specify sudo password for Ansible-open source projects ansible\/ansible"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/458848f3a085f562559af4ef8f1e5a6b?s=128&amp;d=identicon&amp;r=PG\" \/> <strong>toast38coza<\/strong><\/p>\n<p>Probably the best way to do this &#8211; assuming that you can&#8217;t use the NOPASSWD solution provided by scottod is to use Mircea Vutcovici&#8217;s solution in combination with Ansible vault.<\/p>\n<p>For example, you might have a playbook something like this:<\/p>\n<pre><code>- hosts: all\n\n  vars_files:\n    - secret\n\n  tasks:\n    - name: Do something as sudo\n      service: name=nginx state=restarted\n      sudo: yes\n<\/code><\/pre>\n<p>Here we are including a file called <code>secret<\/code> which will contain our sudo password.<\/p>\n<p>We will use ansible-vault to create an encrypted version of this file:<\/p>\n<pre><code>ansible-vault create secret\n<\/code><\/pre>\n<p>This will ask you for a password, then open your default editor to edit the file. You can put your <code>ansible_sudo_pass<\/code> in here.<\/p>\n<p>e.g.: <code>secret<\/code>:<\/p>\n<pre><code>ansible_sudo_pass: mysudopassword\n<\/code><\/pre>\n<p>Save and exit, now you have an encrypted <code>secret<\/code> file which Ansible is able to decrypt when you run your playbook. Note: you can edit the file with <code>ansible-vault edit secret<\/code> (and enter the password that you used when creating the file)<\/p>\n<p>The final piece of the puzzle is to provide Ansible with a <code>--vault-password-file<\/code> which it will use to decrypt your <code>secret<\/code> file.<\/p>\n<p>Create a file called <code>vault.txt<\/code> and in that put the password that you used when creating your <code>secret<\/code> file. The password should be a string stored as a single line in the file.<\/p>\n<p>From the Ansible Docs:<\/p>\n<blockquote>\n<p>.. ensure permissions on the file are such that no one else can access your key and do not add your key to source control<\/p>\n<\/blockquote>\n<p>Finally: you can now run your playbook with something like<\/p>\n<pre><code>ansible-playbook playbook.yml -u someuser -i hosts --sudo --vault-password-file=vault.txt \n<\/code><\/pre>\n<p>The above is assuming the following directory layout:<\/p>\n<pre><code>.\n|_ playbook.yml\n|_ secret\n|_ hosts\n|_ vault.txt\n<\/code><\/pre>\n<p>You can read more about Ansible Vault here: https:\/\/docs.ansible.com\/playbooks_vault.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>toast38coza Probably the best way to do this &#8211; assuming that you can&#8217;t use the NOPASSWD solution provided by scottod is to use Mircea Vutcovici&#8217;s solution in combination with Ansible vault. For example, you might have a playbook something like this: &#8211; hosts: all vars_files: &#8211; secret tasks: &#8211; name: Do something as sudo service: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7934","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7934","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7934"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7934\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}