Ansible: Saving Time by Locally Caching material

The BuilderHotspot can be used to save time building Single Board Computing firmware images where firmware components are installed using apt-get.

Downloading & compiling drivers can be painfully slow. I recently built a recipe for running a phony wifi network that spoofs common wifi SSIDs like “NETGEAR”. The solution relies on the AWUS036AH wifi adapter. These drivers aren’t available via apt- so I I need to download, configure, compile & install drivers. These activities add about 30 minutes to a firmware build- it would be advantageous to find some ways to speed this up.

We can reduce some time by:

  • Saving the driver files locally on the builder hotspot
  • pre-compiling them for target machines
  • syncing them to the target
  • then running make install on the target system.

I don’t want to package the drivers in the main recipe I publish- the drivers may be inappropriate for the target system- so I need a solution that will work for people generally.

I solved this problem by using some ansible conditionals.

I precompiled the rtl8812au drivers and stuck them in the Pi’ account’s home directory on the BuilderHotspot.

The logic of this playbook is:

If I have a rtl8812au in the home directory (discovered by stat’ing the location), register a variable called rtl8812au_config.

If the variable is defined, then I can use the synchronize step.

If the variable is not defined (because the directory was not identified), then we’ll go ahead and do a git clone- and in later steps, compile and install the drivers.

   - name: Check for /home/pi/rtl8812au
     delegate_to: localhost
     stat:
       path: /home/pi/rtl8812au
     register: rtl8812au_config 
     tags:          
     - dual_nic
     - catcher
     - awus03ach_wifi

   - name: Synchronization of files for rtl8812 wifi 
     synchronize: 
       src: "/home/pi/rtl8812au"
       dest: /tmp/
     when: rtl8812au_config is defined
     tags:          
     - dual_nic
     - catcher
     - awus03ach_wifi

   - name: Clone of rtl8812au drivers
     ansible.builtin.git:
       repo: https://github.com/aircrack-ng/rtl812au.git/
       dest: /tmp
       single_branch: yes
       version: master
     when: rtl8812au_config is undefined
     tags:          
     - dual_nic
     - catcher
     - awus03ach_wifi