gabrielgio.me @ bf64e61e1da92a829ae172cde608371a3335b602

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