1---
2layout: post
3title: "Automating setup with ansible-pull part-1"
4date: 2019-03-07
5lastmod: 2020-07-12
6tags: ['ansible','ansible-pull', 'linux', 'fedora']
7---
8
9Every time that I do a clean install on my machine it takes a few hours till I
10get to point where I was before formatting it, install all packages, select
11themes, icons, fonts, install IDEs, extensions and so on. After doing it a few
12times I came to the conclusion that I would save time by spending time
13automating this chore, and as a result, I could toy a little more with my
14system and not worry about spending a weekend re-installing everything (which
15have happened more time that I\'d like to remember).
16
17\<!--more-->
18
19So after a few attempts using python and bash I ended with many files and keep
20everything organized and concise turned out to be more tedious than the setup
21itself. So there comes [Ansible](https://www.ansible.com/). It is an
22enterprise-graded software used to automate tasks. It has A LOT OF features and
23it can be really helpful if you\'re a sysadmin but for now we\'re going to
24focus on [Ansible
25Pull](https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html#ansible-pull)
26and
27[Playbooks](https://docs.ansible.com/ansible/latest/user_guide/playbooks.html).
28As better described:
29
30> \[Ansible-Pull\] is used to up a remote copy of ansible on each
31> managed node, each set to run via cron and update playbook source via
32> a source repository. This inverts the default push architecture of
33> Ansible into a pull architecture, which has near-limitless scaling
34> potential.
35>
36> Playbooks are Ansible's configuration, deployment, and orchestration
37> language. They can describe a policy you want your remote systems to
38> enforce, or a set of steps in a general IT process.
39>
40> [source](https://docs.ansible.com/ansible/latest/cli/ansible-pull.html)
41
42The goal is to pull and run a playbook remotely using a git repository.
43The playbook will describe the tasks needed to setup our machine from
44scratch.
45
46But first lets toy a bit a with playbooks locally with `ansible-playbook`, to
47do so we need to add `localhost` to ansible's hosts list. Add it to
48 `/etc/ansible/hosts`:
49
50``` service
51[all]
52localhost
53```
54
55As an experiment we\'re going to write a asks to install vim. Currently, I\'m
56using [Fedora](https://getfedora.org/) thus we going to use [dnf
57modeule](https://docs.ansible.com/ansible/latest/modules/dnf_module.html) to
58install packages, but if you\'re using another distribution look for a
59equivalent module like [apt
60module](https://docs.ansible.com/ansible/latest/modules/apt_module.html) for
61[Ubuntu](https://ubuntu.com/).
62
63The playbook to install is quite straightforward:
64
65``` yaml
66# main.yaml
67- hosts: all
68 tasks:
69 - name: install vim
70 dnf:
71 name: vim
72 state: latest
73```
74
75`hosts:` is required and it has to match our hosts otherwise the
76playbook won\'t run.
77
78`tasks:` is the list of tasks that the playbook will perform, in this
79case will be `dnf install vim`.
80
81To run a playbook use the command `ansible-playbook` commando to run
82`main.yml` direct from disk, do to so just run the following command:
83
84``` bash
85sudo ansible-playbook --connection=local main.yml
86```
87
88After a few seconds, vim will be installed on your machine.
89
90```
91PLAY [all] *************************************************************
92
93TASK [Gathering Facts] *************************************************
94ok: [localhost]
95
96TASK [install vim] *****************************************************
97ok: [localhost]
98
99PLAY RECAP *************************************************************
100localhost : ok=2 changed=0 unreachable=0 failed=0
101```
102
103This is the first step, next part we shall create a more complex
104playbook and setup repository to run it remotely using `ansible-pull`.