Skip to content

Instantly share code, notes, and snippets.

@Gonzih
Forked from cahna/ansible-aur-pkg-installer.md
Last active September 20, 2016 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gonzih/65c4f1dbf62a4adb323640cd0b356ebc to your computer and use it in GitHub Desktop.
Save Gonzih/65c4f1dbf62a4adb323640cd0b356ebc to your computer and use it in GitHub Desktop.
download, build, and install aur packages with ansible

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
  vars:
    makepkg_nonroot_user: "{{ ansible_ssh_user }}"
  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     # Include this file with vars 'pkg_name' and 'makepkg_nonroot_user' to install an AUR package
$ cd ./roles/foobar/tasks
$ git submodule add https://gist.github.com/45bb9eee92c5f1fce66f.git aur
  1. Now aur/pkg.yml may be included from any task or handler within the foobar role. Given the variables pkg_name and makepkg_nonroot_user, the tasks will validate, download, compile (as the makepkg_nonroot_user user), and install (as root) the pkg_name package.

Tip: Set makepkg_nonroot_user in your group_vars/all file to avoid repeating yourself.

---
- name: AUR | {{ pkg_name }} | check if package is installed
command: pacman -Q {{ pkg_name }}
ignore_errors: yes
changed_when: False
register:
aur_pacman_check
- name: AUR | {{ pkg_name }} | clone pkg source
when: aur_pacman_check.rc != 0
become: no
git: >
repo='https://aur.archlinux.org/{{ pkg_name }}.git'
dest='/tmp/ansible-aur-build/{{ pkg_name }}'
force=yes
register: aur_git
- name: AUR | {{ pkg_name }} | build package, including missing dependencies
when: aur_git | changed
become: yes
become_user: "{{ makepkg_user }}"
command: >
makepkg --noconfirm --noprogressbar -mfs
chdir=/tmp/ansible-aur-build/{{ pkg_name }}
register: aur_makepkg_result
- shell: ls -1 /tmp/ansible-aur-build/{{ pkg_name | quote }} | grep pkg.tar
when: aur_makepkg_result | changed
register: compiled_package_name
- name: AUR | {{ pkg_name }} | install newly-built aur package with pacman
when: aur_makepkg_result | changed
become: yes
shell: >
pacman --noconfirm --noprogressbar --needed -U {{ compiled_package_name.stdout | quote }}
chdir=/tmp/ansible-aur-build/{{ 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