<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ansible &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/ansible/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Wed, 04 Sep 2024 15:45:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>Ansible &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>Ansible: Handling Sudo Password with Homebrew</title>
		<link>https://myshittycode.com/2024/09/04/ansible-handling-sudo-password-with-homebrew/</link>
					<comments>https://myshittycode.com/2024/09/04/ansible-handling-sudo-password-with-homebrew/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Wed, 04 Sep 2024 15:45:24 +0000</pubDate>
				<category><![CDATA[Development Tools]]></category>
		<category><![CDATA[Ansible]]></category>
		<category><![CDATA[Homebrew]]></category>
		<guid isPermaLink="false">https://myshittycode.com/?p=2665</guid>

					<description><![CDATA[<p>Problem When using the Ansible playbook to run Homebrew-related modules, it will prompt for a sudo password where necessary on specific tasks. For example, using the community.general.homebrew_cask module to (un)install the apps under /Applications directory will prompt for a sudo password on each app. It is not possible to preemptively prompt for a sudo password [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2024/09/04/ansible-handling-sudo-password-with-homebrew/">Ansible: Handling Sudo Password with Homebrew</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<div class="wp-block-rank-math-toc-block" id="rank-math-toc"><h2>Table of Contents</h2><nav><ul><li><a href="#problem">Problem</a></li><li><a href="#does-not-work-too">Root Cause</a></li><li><a href="#solution">Solution</a></li></ul></nav></div>



<h2 class="wp-block-heading" id="problem">Problem</h2>



<p>When using the Ansible playbook to run Homebrew-related modules, it will prompt for a sudo password where necessary on specific tasks. For example, using the <a href="https://docs.ansible.com/ansible/latest/collections/community/general/homebrew_cask_module.html" target="_blank" rel="noopener">community.general.homebrew_cask</a> module to (un)install the apps under /Applications directory will prompt for a sudo password on each app.</p>



<p>It is not possible to preemptively prompt for a sudo password before running the Ansible playbook:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; highlight: [2]; title: ; notranslate">
# Does not work!
sudo -v

ansible-playbook main.yml
</pre></div>


<p>It is also not possible to perform <a href="https://gist.github.com/cowboy/3118588" target="_blank" rel="noopener">clever tricks like this</a> to extend the sudo timeout:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; highlight: [2]; title: ; notranslate">
# Does not work, too!
while true; do sudo -n true; sleep 60; kill -0 &quot;$$&quot; || exit; done 2&gt;/dev/null &amp;

ansible-playbook main.yml
</pre></div>


<h2 class="wp-block-heading" id="does-not-work-too">Root Cause</h2>



<p>This is not an Ansible problem. This behavior exists because Homebrew always clears the sudo password cache to prevent privilege escalation attacks [<a href="https://github.com/Homebrew/brew/issues/17905" target="_blank" rel="noopener">link 1</a>] [<a href="https://github.com/Homebrew/brew/pull/17694/commits/2adf25dcaf8d8c66124c5b76b8a41ae228a7bb02" target="_blank" rel="noopener">link 2</a>].</p>



<p>The upside is the <strong>community.general.homebrew_cask</strong> module provides a variable for passing in a sudo password, for example:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: yaml; highlight: [7]; title: ; notranslate">
- name: Install/Upgrade cask packages
  community.general.homebrew_cask:
    name: &#039;{{ item }}&#039;
    state: upgraded
    greedy: true
    install_options: force,no-quarantine
    sudo_password: &quot;{{ ansible_become_pass }}&quot;
  loop: &#039;{{ homebrew_cask_packages_present }}&#039;
</pre></div>


<h2 class="wp-block-heading" id="solution">Solution</h2>



<p>While storing passwords in a file is not ideal, it prevents these Ansible tasks from prompting for a sudo password each time. The <strong>ansible-vault</strong> command can be used to store the password (and other secrets) securely.</p>



<p>First, create an encrypted file.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
ansible-vault create vault.yml
</pre></div>


<p>You will be prompted for a new vault password. Once provided, enter the following into the file.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
ansible_become_pass: &#x5B;YOUR_SUDO_PASSWORD]
</pre></div>


<p>Now, your encrypted file should be created in the location you specified with the proper permission set:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; highlight: [18]; title: ; notranslate">
$ ls -la
total 88
drwxr-xr-x@ 18 shitty.author  staff   576 Sep  4 09:06 .
drwxr-xr-x  12 shitty.author  staff   384 Jun 27 12:47 ..
-rw-r--r--   1 shitty.author  staff    99 Mar  1  2024 .ansible-lint
-rw-r--r--   1 shitty.author  staff   193 Mar 19 08:57 .editorconfig
drwxr-xr-x  16 shitty.author  staff   512 Sep  4 09:06 .git
-rw-r--r--   1 shitty.author  staff   243 Mar  1  2024 .gitignore
drwxr-xr-x   8 shitty.author  staff   256 Sep  4 09:35 .idea
drwxr-xr-x   6 shitty.author  staff   192 Mar  4  2024 .venv
-rw-r--r--   1 shitty.author  staff    72 Mar  1  2024 .yamllint
-rw-r--r--   1 shitty.author  staff  1079 Mar  1  2024 LICENSE.md
-rw-r--r--@  1 shitty.author  staff  1796 Sep  4 08:39 README.md
-rw-r--r--   1 shitty.author  staff   381 Mar  1  2024 ansible.cfg
-rw-r--r--   1 shitty.author  staff    65 Mar  1  2024 inventory.yml
-rw-r--r--@  1 shitty.author  staff  1450 Sep  4 09:06 main.yml
drwxr-xr-x  16 shitty.author  staff   512 Aug 12 09:32 roles
-rw-------@  1 shitty.author  staff   484 Sep  4 09:01 vault.yml
</pre></div>


<p>If you open vault.yml, the content should look gibberish.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
$ cat vault.yml                                                                                  ✔
$ANSIBLE_VAULT;1.1;AES256
62343635616463643935613965336336323366653565646137616238663266633936363463611364
6139353639666163323066653733663763323236663361380a646264623465616461646637315661
34396438316137383130366330313431653336396435656562356430333762373866663234383230
6265656164346531660a626431383230326664393839316131626330353562363164313439661863
31343363323236333531303139396662386531626165663732386233626538646338333133375936
6535626465393233386634393934623438393535626365313132
</pre></div>


<p>In the Ansible playbook, specify vault.yml under <strong>vars_files</strong>.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: yaml; highlight: [4,5]; title: ; notranslate">
---
- name: My Playbook
  hosts: all
  vars_files:
    - vault.yml
  roles:
    - ...
</pre></div>


<p>Finally, run the Ansible playbook with the <strong>&#8211;ask-vault-pass</strong> option, where you will be prompted for the vault password once before Ansible executes the playbook.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
ansible-playbook main.yml --ask-vault-pass
</pre></div>


<p>Now, you will not be prompted for a sudo password every time Homebrew-related tasks run.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2024/09/04/ansible-handling-sudo-password-with-homebrew/">Ansible: Handling Sudo Password with Homebrew</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2024/09/04/ansible-handling-sudo-password-with-homebrew/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2665</post-id>	</item>
		<item>
		<title>Ansible: Handling Multiple Hosts via SSH</title>
		<link>https://myshittycode.com/2021/05/19/ansible-handling-multiple-hosts-via-ssh/</link>
					<comments>https://myshittycode.com/2021/05/19/ansible-handling-multiple-hosts-via-ssh/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Wed, 19 May 2021 13:28:47 +0000</pubDate>
				<category><![CDATA[Development Tools]]></category>
		<category><![CDATA[Ansible]]></category>
		<category><![CDATA[MacOS]]></category>
		<category><![CDATA[SSH]]></category>
		<guid isPermaLink="false">https://myshittycode.com/?p=1217</guid>

					<description><![CDATA[<p>PROBLEM To run Ansible playbook in multiple hosts via SSH. SOLUTION Configuring SSH environment Ensure SSH keypair exists on the current machine (ex: ~/.ssh/id_rsa for private key and ~/.ssh/id_rsa.pub for public key). If you do not have one, create one: Copy the public key (ex: ~/.ssh/id_rsa.pub) to each remote host&#8217;s ~/.ssh/authorized_keys. If this file doesn&#8217;t [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2021/05/19/ansible-handling-multiple-hosts-via-ssh/">Ansible: Handling Multiple Hosts via SSH</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p></p>



<h2 class="wp-block-heading">PROBLEM</h2>



<p>To run Ansible playbook in multiple hosts via SSH.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<h3 class="wp-block-heading">Configuring SSH environment</h3>



<p>Ensure SSH keypair exists on the current machine (ex: <strong>~/.ssh/id_rsa</strong> for private key and <strong>~/.ssh/id_rsa.pub</strong> for public key). If you do not have one, create one:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
ssh-keygen
</pre></div>


<p>Copy the public key (ex: <strong>~/.ssh/id_rsa.pub</strong>) to each remote host&#8217;s <strong>~/.ssh/authorized_keys</strong>. If this file doesn&#8217;t exist, create it.</p>



<p>Ensure the current machine&#8217;s <strong>.ssh/</strong> directory and file have correct permission.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
</pre></div>


<p>In the current machine&#8217;s <strong>/etc/hosts</strong>, add all remote hosts.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
127.0.0.1      localhost  # current machine
192.168.1.100  donkeykong # remote host 1
192.168.1.200  supermario # remote host 2
</pre></div>


<p>In each remote host, enable the remote login and grant yourself the access to this service.</p>



<figure class="wp-block-image size-large"><a href="https://myshittycode.com/wp-content/uploads/2021/05/remote-login-1.png?x45560"><img fetchpriority="high" decoding="async" width="1200" height="988" src="https://myshittycode.com/wp-content/uploads/2021/05/remote-login-1.png?x45560" alt="" class="wp-image-1226" srcset="https://myshittycode.com/wp-content/uploads/2021/05/remote-login-1.png 1200w, https://myshittycode.com/wp-content/uploads/2021/05/remote-login-1-300x247.png 300w, https://myshittycode.com/wp-content/uploads/2021/05/remote-login-1-1024x843.png 1024w, https://myshittycode.com/wp-content/uploads/2021/05/remote-login-1-768x632.png 768w" sizes="(max-width: 1200px) 100vw, 1200px" /></a><figcaption class="wp-element-caption">Enabling Remote Login on Mac</figcaption></figure>



<p>Test SSH connection to remote host to ensure they work first before working on Ansible playbook.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
ssh user@donkeykong
ssh user@supermario
</pre></div>


<h3 class="wp-block-heading">Creating Ansible Playbook</h3>



<p>Create <strong>ansible.cfg</strong> and define the location of inventory file.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
&#x5B;defaults]
inventory = inventory.yml
</pre></div>


<p>Create <strong>inventory.yml</strong> and define both localhost and remote hosts.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: yaml; title: ; notranslate">
all:
  hosts:
    localhost:
      ansible_connection: local
    donkeykong:
      ansible_user: user
      ansible_ssh_private_key_file: ~/.ssh/id_rsa
    supermario:
      ansible_user: user
      ansible_ssh_private_key_file: ~/.ssh/id_rsa
</pre></div>


<p>Run a test to ensure the connection to remote hosts are successful.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
ansible all -i inventory.yml -m ping
</pre></div>


<p>If successful, the output looks something like this:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
localhost | SUCCESS =&gt; {
    &quot;changed&quot;: false,
    &quot;ping&quot;: &quot;pong&quot;
}
donkeykong | SUCCESS =&gt; {
    &quot;changed&quot;: false,
    &quot;ping&quot;: &quot;pong&quot;
}
supermario | SUCCESS =&gt; {
    &quot;changed&quot;: false,
    &quot;ping&quot;: &quot;pong&quot;
}
</pre></div>


<p>Create <strong>main.yml</strong> with a very simple task.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: yaml; title: ; notranslate">
- name: all-hosts
  hosts: all
  tasks:
    - name: Capture current dir
      shell: pwd
      register: output

    - name: Display output
      debug: msg=&#039;{{ output.stdout }}&#039;
</pre></div>


<p>Run the playbook.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
ansible-playbook main.yml
</pre></div>


<p>If successful, the output looks something like this:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
PLAY &#x5B;all-hosts] *******************************************************************************************************

TASK &#x5B;Gathering Facts] *************************************************************************************************
ok: &#x5B;localhost]
ok: &#x5B;donkeykong]
ok: &#x5B;supermario]

TASK &#x5B;Capture current dir] *********************************************************************************************
changed: &#x5B;localhost]
changed: &#x5B;donkeykong]
changed: &#x5B;supermario]

TASK &#x5B;Display output] **************************************************************************************************
ok: &#x5B;localhost] =&gt; {
    &quot;msg&quot;: &quot;/Users/user/myshittycode&quot;
}
ok: &#x5B;donkeykong] =&gt; {
    &quot;msg&quot;: &quot;/Users/user&quot;
}
ok: &#x5B;supermario] =&gt; {
    &quot;msg&quot;: &quot;/Users/user&quot;
}

PLAY RECAP *************************************************************************************************************
donkeykong                 : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
localhost                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
supermario                 : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
</pre></div>


<h3 class="wp-block-heading">Controlling the Hosts</h3>



<p>Sometimes, you want finer controls on what tasks to be ran in certain hosts.</p>



<p>To run in just one host (ex: <strong>donkeykong</strong>):</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: yaml; highlight: [2]; title: ; notranslate">
- name: one-host
  hosts: donkeykong
  tasks:
    - ...
</pre></div>


<p>To run in all remote hosts except localhost:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: yaml; highlight: [2]; title: ; notranslate">
- name: all-hosts-except-localhost
  hosts: all:!localhost
  tasks:
    - ...
</pre></div>


<p></p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2021/05/19/ansible-handling-multiple-hosts-via-ssh/">Ansible: Handling Multiple Hosts via SSH</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2021/05/19/ansible-handling-multiple-hosts-via-ssh/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1217</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk (Request-wide modification query)

Served from: myshittycode.com @ 2026-02-21 11:35:30 by W3 Total Cache
-->