gabrielgio.me @ 12e8802cd9829580275027533ce898e484d003d1

 1---
 2title:   "Automating desktop setup with ansible-pull part-2"
 3date:    2019-04-22
 4lastmod: 2020-07-12
 5tags:    ['ansible', 'ansible-pull', 'linux', 'fedora']
 6---
 7
 8[[{{< ref "2019-03-07-ansible-part-1.org " >}}][See part 1]]
 9
10Now we're going to setup ansible to work with a git repository. The process is
11quite similar to ~ansible-playbook~, the only difference is that the source for
12the playbook will be a remote repository and not a local file. Following the
13previous example we'll get vim setup automated.
14
15Create a git repository wherever you see fit, [[https://about.gitlab.com/][gitlab]] and [[https://github.com/][github]] offer free
16repositories. For this task we need to add only two file: one for the
17~yml~ file describing the tasks and the ~.vimrc~ file.
18
19
20In the ~.vimrc~ add your own configuration, you can see mine [[https://gitlab.com/gabrielgio/homestation/-/blob/debcf3458df511aef9f7dca0cb73f6cf6baddd5d/.vimrc][over here]], it is
21pretty simple as I don't use it but for simple text editing (like this post) so
22you can start with that if you don't have one.
23
24The ~yml~ file will have two tasks, one is to install vim, just like we did in
25the part 1.
26
27#+BEGIN_SRC yaml
28# main.yml
29---
30- name: install vim
31  dnf:
32    name: vim
33    state: latest
34#+END_SRC
35
36To copy ~.vimrc~ file to your ~$HOME~ we going to use [[https://docs.ansible.com/ansible/latest/modules/copy_module.html][copy module]]:
37
38#+BEGIN_SRC yaml
39# main.yml
40---
41- name: copy vimrc file
42  copy:
43    src: config/.vimrc
44    dest: ~/
45    mode: 0644
46#+END_SRC
47
48After we've added those two files to repository you will have be something [[https://gitlab.com/gabrielgio/homestation/-/tree/debcf3458df511aef9f7dca0cb73f6cf6baddd5d][like
49this]].
50
51And now we just need to run ~ansible-pull~ command
52
53#+BEGIN_SRC shell
54# you may need run it as a sudo
55ansible-pull -U $YOUR_REPO -i all main.yml
56#+END_SRC
57
58Params:
59- *~-i~* is a list of hosts.
60- *~-U~* is the git repository URL.
61
62Remember ~man~ is your best friend, take a look at ~man ansible-pull~ to know
63more about its parameters.
64
65The best part you can quickly test and see the result by running my sample:
66#+BEGIN_SRC shell
67ansible-pull -U https://gitlab.com/gabrielgio/homestation.git -C debcf3458df511aef9f7dca0cb73f6cf6baddd5d -i all main.yml
68#+END_SRC
69
70The idea here is to keep your repository as a source of truth when comes to
71configuration, you can add ~ansible-pull~ to a CRON tab, so you just need to push
72something to your repository and after a few minutes not only your machine but
73all the machines that have it setup will run the playbooks. You can use this
74method as a simple way to install software, update machines or even distribute
75tooling company-wise.