<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://book.ghanim.no/index.php?action=history&amp;feed=atom&amp;title=HomeLab%2FAnsible_-_Create_Playbook</id>
	<title>HomeLab/Ansible - Create Playbook - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://book.ghanim.no/index.php?action=history&amp;feed=atom&amp;title=HomeLab%2FAnsible_-_Create_Playbook"/>
	<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=HomeLab/Ansible_-_Create_Playbook&amp;action=history"/>
	<updated>2026-04-21T15:10:52Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://book.ghanim.no/index.php?title=HomeLab/Ansible_-_Create_Playbook&amp;diff=1123&amp;oldid=prev</id>
		<title>imported&gt;Aghanim at 18:15, 8 November 2020</title>
		<link rel="alternate" type="text/html" href="https://book.ghanim.no/index.php?title=HomeLab/Ansible_-_Create_Playbook&amp;diff=1123&amp;oldid=prev"/>
		<updated>2020-11-08T18:15:53Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Prerequisite ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Ubuntu 20.04* Ansible installed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ansible is a great tool to automate IT infrastructure. In my case I use it to manage my Linux hosts. Instead of typing one command on each host, I can send one command to multiple hosts, using something called playbook. Ansible playbook is a set of instructions that you send to run on a single or a group of hosts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Generate and send SSH keys to hosts ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In order for ansible to execute your commands on the host, ansible need to have SSH access without password to the host&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[https://blog.aghanim.net/index.php/2020/09/05/how-to-add-a-new-host-to-ansible/ Follow this guide to achieve this.]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Add hosts to inventory ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In order to setup a playbook you first need to setup a hosts file. You can find the hosts file in /etc/ansible. The file is called &amp;quot;hosts&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can add ungrouped hosts or a collection of hosts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
# Ungroupd hosts&lt;br /&gt;
Host01 ansible_host=192.168.1.100&lt;br /&gt;
Host10 ansible_host=192.168.1.110&lt;br /&gt;
&lt;br /&gt;
# Grouped hosts&lt;br /&gt;
[Servers01]&lt;br /&gt;
Host20 ansible_host=192.168.1.150&lt;br /&gt;
Host30 ansible_host=192.168.1.151&lt;br /&gt;
Host40 ansible_host=192.168.1.152&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Create a playbook ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ansible playbooks uses YAML language. YAML is ident sensitive, so make sure your indents are correct.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a YAML config.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
sudo vim playbook1.yml&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can send any type of command to your hosts. I send update and upgrade commands to my hosts everyday using crontab. My Playbook looks like this.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
- hosts: Servers01&lt;br /&gt;
  become: true&lt;br /&gt;
  become_user: root&lt;br /&gt;
  tasks:&lt;br /&gt;
    - name: Update apt repo and cache on all Debian/Ubuntu boxes&lt;br /&gt;
      apt: update_cache=yes force_apt_get=yes cache_valid_time=3600&lt;br /&gt;
&lt;br /&gt;
    - name: Upgrade all packages on servers&lt;br /&gt;
      apt: upgrade=dist force_apt_get=yes&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We will break down the commands.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;hosts: Servers01&amp;#039;&amp;#039;&amp;#039; - Specify a list of hosts that the instructions will be sent to.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;become: true &amp;#039;&amp;#039;&amp;#039;- We&amp;#039;re telling ansible that the instructions we are sending, requires root access to exectue.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;become_user:root&amp;#039;&amp;#039;&amp;#039; - We are running the commands as the user root.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;tasks: -&amp;#039;&amp;#039;&amp;#039; The tasks initiates the lists of task that is going to be executed. Each task have a unique name using &amp;quot;name&amp;quot;. We use &amp;quot;apt&amp;quot; module to update our hosts, or in other instances, install packages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Run the playbook ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To run your playbook, simply type:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
sudo ansible-playbook /etc/ansible/roles/playbook1.yml&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And that is all you have to do. You can create a schedule using crontab to run this playbook everyday&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
0 00 * * * ansible-playbook /etc/ansible/roles/playbook1.yml&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
There is much more you can do in ansible. I recommend you read [https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html the docs.]&lt;br /&gt;
&lt;br /&gt;
[[Category:HomeLab]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Aghanim</name></author>
	</entry>
</feed>