gabrielgio.me @ 12e8802cd9829580275027533ce898e484d003d1

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