Skip to content

Instantly share code, notes, and snippets.

@piglovesyou
Last active October 16, 2016 07:47
Show Gist options
  • Save piglovesyou/f63da461d4c28b05b8993f2fd82e907e to your computer and use it in GitHub Desktop.
Save piglovesyou/f63da461d4c28b05b8993f2fd82e907e to your computer and use it in GitHub Desktop.
download, build, and install aur packages with ansible

(This is a forked version that just uses ansible_ssh_user for makepkg)

About

When using ArchLinux, I typically prefer to use an AUR helper like pacaur or yaourt to automate away the process of installing a community package.

Ansible's pacman module is great, but it doesn't support AUR packages or pacman's -U flag. Installing AUR packages with Ansible seemed to be left as an exercise to the user, and since AUR helpers do not come with a fresh Arch install, I created this set of tasks to be a reusable way of installing AUR packages on my Arch hosts.

I should take the time to PR an AUR module for Ansible sometime soon, but this is a nice, resusable submodule for any Arch-based Ansible role.

Example

---
- name: install yaourt on ArchLinux hosts
  hosts: my_archlinux_host_group
  gather_facts: yes
  tasks:
    - name: install package-query (a yaourt dependency)
      include: aur/pkg.yml pkg_name=package-query
      
    - name: install yaourt
      include: aur/pkg.yml pkg_name=yaourt

Reccommended Usage

  1. Add this gist as a submodule to your Ansible role or playbook
# Example ./roles directory structure for an existing Ansible playbook with a 'foobar' role 
#   roles/
#     foobar/
#       tasks/
#         aur/          # Source repo added as a submodule (cloned into an `aur` directory)
#           pkg.yml
$ cd ./roles/foobar/tasks
$ git submodule add https://gist.github.com/39ecb1eb1ab8ee1d0ce1.git aur
  1. Now aur/pkg.yml may be included from any task or handler within the foobar role. Given the variables pkg_name, the tasks will validate, download, compile (as a logging-in user), and install (as root) the pkg_name package.
---
- name: AUR | Get metadata from AurJson api
become: no
uri: >
url=https://aur.archlinux.org/rpc.php?type=info&arg={{ pkg_name | mandatory }}
return_content=yes
timeout=6
register: api_info
- assert:
that:
- api_info.status == 200
- api_info.json is defined
- api_info.json.type == 'info'
- api_info.json.resultcount == 1
- api_info.json.results is defined
- name: AUR | Get installed package version
become: no
shell: pacman -Q {{ pkg_name }} | cut -d ' ' -f2
register: pacman_query_result
- name: AUR | Check if the AUR Version is already installed
become: no
when: api_info.json.results.Version != pacman_query_result.stdout
shell: echo "Needs Install"
register: version_check_result
- name: AUR | {{ pkg_name }} | Download tarball
become: no
when: version_check_result.changed
get_url: >
url='https://aur.archlinux.org{{ api_info.json.results.URLPath }}'
dest='/tmp/'
register: aur_tarball
- name: AUR | {{ pkg_name }} | Extract tarball
become: no
when: version_check_result.changed
unarchive: >
src={{ aur_tarball.dest }}
dest='/tmp/'
copy=no
register: extracted_pkg
- name: AUR | {{ pkg_name }} | Build package, including missing dependencies
when: extracted_pkg|changed
become: no
command: >
makepkg --noconfirm --noprogressbar --nocolor --force --syncdeps
chdir=/tmp/{{ pkg_name }}
register: aur_makepkg_result
- name: AUR | {{ pkg_name }} | Install newly-built aur package with pacman
when: aur_makepkg_result|changed
become: yes
shell: >
pacman --noconfirm --noprogressbar --needed -U *.pkg.tar.xz
chdir=/tmp/{{ pkg_name }}
register: pacman_install_result
changed_when: pacman_install_result.stdout is defined and pacman_install_result.stdout.find('there is nothing to do') == -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment