{"id":60,"date":"2023-08-05T07:12:35","date_gmt":"2023-08-05T07:12:35","guid":{"rendered":"http:\/\/it4home.dk\/?p=60"},"modified":"2023-10-22T15:49:04","modified_gmt":"2023-10-22T13:49:04","slug":"automating-raspberry-pi-configuration-with-ansible","status":"publish","type":"post","link":"https:\/\/it4home.dk\/index.php\/2023\/08\/05\/automating-raspberry-pi-configuration-with-ansible\/","title":{"rendered":"Automating Raspberry Pi Configuration with Ansible"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-post pdfprnt-top-right\"><a href=\"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/posts\/60?print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\" ><\/a><a href=\"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/posts\/60?print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\" ><img decoding=\"async\" src=\"https:\/\/it4home.dk\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">Introduction:<br>Managing multiple Raspberry Pi devices can be time-consuming and error-prone if done manually. Fortunately, Ansible provides a powerful solution for automating configuration tasks across your Raspberry Pi fleet. In this guide, we&#8217;ll walk you through setting up Ansible, creating an inventory, and using playbooks to perform various tasks on your Raspberry Pi devices.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Prerequisites:<br>Before proceeding, make sure you have Python3 and pip installed on your Raspberry Pi. You can install pip using the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install python3-pip<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Step 1: Install Ansible<br>First, let&#8217;s install Ansible using pip:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python3 -m pip install --user ansible-core==2.12.3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Step 2: Create Ansible Configuration<br>Now, create a directory for your Ansible project and add an <code>ansible.cfg<\/code> file inside it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir \/ansible\nvi \/ansible\/ansible.cfg<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Paste the following configuration in the <code>ansible.cfg<\/code> file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;defaults]\ninventory = inventory\nhost_key_checking = False<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Save the file and exit the editor.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Step 3: Create the Inventory<br>The inventory file contains information about the hosts (Raspberry Pi devices) you want to manage. Create an <code>inventory<\/code> file inside the <code>\/ansible<\/code> directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vi \/ansible\/inventory<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following content to the <code>inventory<\/code> file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;tower]\nRPIT1 ansible_host=192.168.0.220\nRPIT2 ansible_host=192.168.0.221\nRPIT3 ansible_host=192.168.0.222\nRPTT4 ansible_host=192.168.0.223\n\n&#91;test]\nRPI2GB ansible_host=192.168.0.227\n\n&#91;docker]\nRPIDOCKER ansible_host=192.168.0.224\n\n&#91;kali]\nRPIKALI ansible_host=192.168.0.226\n\n&#91;zeros]\nRPIZEROW ansible_host=192.168.0.228 \nRPIZEROW2 ansible_host=192.168.0.225\n\n&#91;all:vars]\nansible_ssh_user=pi ansible_ssh_pass=xxxxxxxxx<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This inventory defines groups of hosts (towers, test, docker, kali, and zeros) and sets the SSH username and password to access them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Step 4: Test Host Reachability<br>Before proceeding with any configuration, make sure all defined hosts are reachable using Ansible&#8217;s ping module:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ansible all -m ping<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Step 5: Check OS Information<br>Retrieve OS information on all clients using the shell module:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ansible all -m shell -a \"lsb_release -a\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Step 6: Upgrade Servers<br>Use a playbook to upgrade all servers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ansible-playbook -e \"target_host=all\" update_upgrade.yaml<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Step 7: Install Neofetch and Temperature<br>Create a playbook to install Neofetch and display the system temperature:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ansible-playbook -e \"target_host=all\" install_neofetch.yaml<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Step 8: Shutdown the Tower<br>Create a playbook to shut down the tower (remember Docker always on):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ansible-playbook -e \"target_host=tower\" shutdown_now.yaml<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Conclusion:<br>You&#8217;ve successfully set up Ansible and used playbooks to automate tasks on your Raspberry Pi devices. Ansible simplifies managing multiple devices and allows you to perform configurations efficiently and consistently. With Ansible, you can now spend less time on repetitive tasks and focus more on your Raspberry Pi projects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Happy automating!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction:Managing multiple Raspberry Pi devices can be time-consuming and error-prone if done manually. Fortunately, Ansible provides a powerful solution for automating configuration tasks across your Raspberry Pi fleet. In this guide, we&#8217;ll walk you&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":364,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[32,5,7],"tags":[35],"class_list":["post-60","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ansible","category-linux","category-raspberry","tag-ok"],"_links":{"self":[{"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/posts\/60","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/comments?post=60"}],"version-history":[{"count":4,"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/posts\/60\/revisions"}],"predecessor-version":[{"id":219,"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/posts\/60\/revisions\/219"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/media\/364"}],"wp:attachment":[{"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/media?parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/categories?post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/tags?post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}