Using Ansible to configure your Desktops and Laptops

From LearnLinux.tv Wiki
Revision as of 00:29, 11 December 2020 by Jay (talk | contribs) (→‎Code Examples)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Overview

You guys asked for it, now I finally deliver - in this video I show you how to use Ansible to configure your laptops and desktops! This works for servers too. This is a long video, but should get you on your way. You'll see a few working examples, and by the end of the video, you will have your very own Ansible desktop/laptop config!

Relevant Links
Original Video
Github repository that includes the samples from the video
Github repository that contains a copy of my personal desktop/laptop code

Setting up the Repository

Create a Git repository

The method used in this video relies on a Git repository, so you will need to create a repository on your Git server to continue. In the video, Github is used as an example.

Create an OpenSSH key (used for cloning the Git Repo)

 ssh-keygen -t ed25519 -C "gleb@reys.net"

Install git on your local machine

 sudo apt install git

Pull down the repository to work locally

 git clone https://url/to/your/repository

Add a file to version control

 git add <filename>

Create a git commit

 git commit -m "message about the commit"

Push the changes up to Github

 git push origin main

Code Examples

Example 1

 ---
 - hosts: localhost
   connection: local
   become: true
 
   tasks:
   - name: install htop
     package:
       name: htop

Example 2

 ---
 - hosts: localhost
   connection: local
   become: true
 
   tasks:
   - name: install packages
     package:
       name:
         - htop
         - tmux
         - vim-nox

Example 3

 ---
 - hosts: localhost
   connection: local
   become: true
 
   tasks:
   - name: install packages
     package:
       name:
         - htop
         - tmux
         - vim-nox
 
   - name: copy wallpaper file
     copy:
       src: files/wallpaper.png
       dest: /usr/share/backgrounds/ansible-wallpaper.png
       owner: root
       group: root
 
   - name: set wallpaper
     become_user: jay
     dconf:
       key: "/org/gnome/desktop/background/picture-uri"
       value: "'file:///usr/share/backgrounds/ansible-wallpaper.png'"
 
   - name: set wallpaper position
     become_user: jay
     dconf:
       key: "/org/gnome/desktop/background/picture-options"
       value: "'zoom'"

Example 4

 ---
 - hosts: localhost
   connection: local
   become: true
 
 tasks:
 
   - name: install packages
     package:
       name:
         - htop
         - tmux
         - vim-nox
 
   - name: copy wallpaper file
     copy:
       src: files/wallpaper.png
       dest: /usr/share/backgrounds/ansible-wallpaper.png
       owner: root
       group: root
 
   - name: set wallpaper
     become_user: jay
     dconf:
       key: "/org/gnome/desktop/background/picture-uri"
       value: "'file:///usr/share/backgrounds/ansible-wallpaper.png'"
 
   - name: set wallpaper position
     become_user: jay
     dconf:
       key: "/org/gnome/desktop/background/picture-options"
       value: "'zoom'"
 
   - name: copy custom .bashrc file
     copy:
       src: files/bashrc
       dest: /home/jay/.bashrc
       owner: jay
       group: jay

Example 5

 ---
 - hosts: localhost
   connection: local
   become: true
 
   tasks:
 
   - name: install packages
     package:
       name:
         - htop
         - tmux
         - vim-nox
 
   - name: copy wallpaper file
     copy:
       src: files/wallpaper.png
       dest: /usr/share/backgrounds/ansible-wallpaper.png
       owner: root
       group: root
 
   - name: set wallpaper
     become_user: jay
     dconf:
       key: "/org/gnome/desktop/background/picture-uri"
       value: "'file:///usr/share/backgrounds/ansible-wallpaper.png'"
 
   - name: set wallpaper position
     become_user: jay
     dconf:
       key: "/org/gnome/desktop/background/picture-options"
       value: "'zoom'"
 
   - name: copy custom .bashrc file
     copy:
       src: files/bashrc
       dest: /home/jay/.bashrc
       owner: jay
       group: jay
 
   - name: add ansible user
     user:
       name: velociraptor
       system: yes
 
   - name: set up sudo for ansible user
     copy:
       src: files/sudoer_velociraptor
       dest: /etc/sudoers.d/velociraptor
       owner: root
       group: root
       mode: 0440
 
   - name: add ansible-pull cron job
     cron:
       name: ansible auto-provision
       user: velociraptor
       minute: "*/10"
       job: ansible-pull -o -U https://github.com/jlacroix82/ansible_pull_tutorial.git

Example 6

 ---
 - hosts: localhost
   connection: local
   become: true
 
   tasks:
 
   - name: install packages
     package:
       name:
         - htop
         - tmux
         - vim-nox
 
   - name: copy wallpaper file
     copy:
       src: files/wallpaper.png
       dest: /usr/share/backgrounds/ansible-wallpaper.png
       owner: root
       group: root
 
   - name: set wallpaper
     become_user: jay
     dconf:
       key: "/org/gnome/desktop/background/picture-uri"
       value: "'file:///usr/share/backgrounds/ansible-wallpaper.png'"
 
   - name: set wallpaper position
     become_user: jay
     dconf:
       key: "/org/gnome/desktop/background/picture-options"
       value: "'zoom'"
 
   - name: disable lock screen notifications
     become_user: jay
     dconf:
       key: "/org/gnome/desktop/notifications/show-in-lock-screen"
       value: "false"
 
   - name: copy custom .bashrc file
     copy:
       src: files/bashrc
       dest: /home/jay/.bashrc
       owner: jay
       group: jay
 
   - name: add ansible user
     user:
       name: velociraptor
       system: yes
 
   - name: set up sudo for ansible user
     copy:
       src: files/sudoer_velociraptor
       dest: /etc/sudoers.d/velociraptor
       owner: root
       group: root
       mode: 0440
 
   - name: add ansible-pull cron job
     cron:
       name: ansible auto-provision
       user: velociraptor
       minute: "*/10"
       job: ansible-pull -o -U https://github.com/jlacroix82/ansible_pull_tutorial.git