Using Ansible Pull
Overview
Have you ever wanted to create a wide-open file share on your LAN to enable your users to quickly and easily share files? In this video, the viewer is shown the process of setting up a custom Samba implementation on Debian/Ubuntu that makes sharing files on your LAN easy.
Relevant Links |
---|
Original Video |
Setting up
You will need a Git repository for this tutorial. The easiest method is to create an account on Github if you don't already have one, and create a repository there. Witch each example that references the Git repository, replace what you see with the URL for yours. Create the repository
Make sure you have git installed, on Debian/Ubuntu (etc) you can do that with:
sudo apt update sudo apt install git
You will also need Ansible installed. On Debian/Ubuntu (etc) you can do that with:
sudo apt update sudo apt install git sudo apt install ansible
It's a good idea to create an ssh-key as well:
ssh-keygen
First iteration
The first version of our playbook installs a single package, to show how ansible-pull works.
Here's the playbook file:
local.yml
- hosts: localhost connection: local become: true tasks: - name: Install htop apt: name: htop
Push the changes to the repository
Before you can use this playbook via ansible-pull, you'll need to push it up to your repository, you can do that with:
git add local.yml git commit -m "initial commit" git push origin master
Run the playbook
After the playbook has been pushed, you can run it with the following command (change the repository URL to match yours):
sudo ansible-pull -U https://github.com/<your_user_name>/ansible.git
Second Iteration
With the second version of our playbook, we add a few more packages.
local.yml (second version)
- hosts: localhost connection: local become: true tasks: - name: Install packages apt: name: - htop - mc - tmux
Push the new version to the repository
git add local.yml git commit -m "added additional packages to local.yml" git push origin master
Run it
sudo ansible-pull -U https://github.com/<github_user>/ansible.git
Third Iteration
The third version changes the local.yml file to be more of an index, that pulls in other files.
Reorganizing the file layout
Create a directory in the repository to hold taskbooks:
mkdir tasks
Move the current local.yml to packages.yml in the new directory:
mv local.yml tasks/packages.yml
local.yml (third version)
- hosts: localhost connection: local become: true pre_tasks: - name: update repositories apt: update_cache: yes changed_when: False
tasks: - include: tasks/packages.yml
We move the play that installs packages out of local.yml and into its own file:
tasks/packages.yml
- name: Install packages apt: name: - htop - mc - tmux
Commit and push the changes so far:
git add . git commit -m "reorganized file layout" git push origin master
Run it
sudo ansible-pull -U https://github.com/<github_user>/ansible.git
Fourth Iteration
The fourth version of our config sets up full automation, so that our hosts will continue to check in after running the playbook once. We do this by having Ansible set up a cron job for itself, as well as create a background user to run Ansible with.
Adding users and a cron job
Create a taskbook for users:
tasks/users.yml
- name: create ansible user user: name: ansible system: yes - name: copy sudoers_ansible copy: src: files/sudoers_ansible dest: /etc/sudoers.d/ansible owner: root group: root mode: 0440
Create a directory to hold files
mkdir files
files/sudoers_ansible
ansible ALL=(ALL) NOPASSWD: ALL
Create a taskbook to add cron jobs
tasks/cron.yml
- name: install cron job (ansible-pull) cron: user: ansible name: "ansible provision" minute: "*/10" job: "/usr/bin/ansible-pull -o -U https://github.com/jlacroix82/ansible_pull_tutorial.git > /dev/null"
Update local.yml so that it imports the new taskbooks we've added:
local.yml
- hosts: localhost become: true pre_tasks: - name: update repositories apt: update_cache: yes changed_when: False
tasks: - include: tasks/users.yml - include: tasks/cron.yml - include: tasks/packages.yml
Commit and push the new changes
git add . git commit - m "added cront and user taskbooks" git push origin master
Run it
sudo ansible-pull -U https://github.com/<github_user>/ansible.git