├── LICENSE ├── README.md ├── aur └── aur.py /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 4 | software, either in source code form or as a compiled binary, for any purpose, 5 | commercial or non-commercial, and by any means. 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | *ansible-aur* is an ansible module to use some aur helpers. 2 | 3 | Currently, we support the following AUR helpers: 4 | 5 | - pacaur (recommended, default) 6 | - yaourt 7 | 8 | ## Usage 9 | 10 | 1. Add as a submodule in your playbook: 11 | 12 | ``` 13 | mkdir -p library/external_modules 14 | git submodule add git://github.com/cdown/ansible-aur.git library/external_modules/ansible-aur 15 | ``` 16 | 17 | 18 | 2. Link the binary to the base of `library/`: 19 | 20 | ``` 21 | ln -s external_modules/ansible-aur/aur library/aur 22 | ``` 23 | 24 | 3. Use it in a task, as in the following examples: 25 | 26 | ``` 27 | # Install (using pacaur) 28 | - aur: name=yturl 29 | become:yes 30 | become_user: some_user_that_has_nopasswd_in_sudoers_for_pacman_u 31 | 32 | # Install (using yaourt) 33 | - aur: name=yturl tool=yaourt 34 | [...] 35 | 36 | # Update (using pacaur) 37 | - aur: update=yes auronly=yes 38 | [...] 39 | 40 | # Remove (can also be done with the pacman resource) 41 | - aur: name=yturl state=absent 42 | [...] 43 | 44 | # Remove recursively (can also be done with the pacman resource) 45 | - aur: name=yturl state=absent recurse=true 46 | [...] 47 | ``` 48 | -------------------------------------------------------------------------------- /aur: -------------------------------------------------------------------------------- 1 | aur.py -------------------------------------------------------------------------------- /aur.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | 3 | from ansible.module_utils.basic import * 4 | 5 | 6 | TOOL_CMD_MAP = { 7 | 'pacaur': ['env', 'EDITOR=cat', 'pacaur', '--noconfirm', '--noedit'], 8 | 'yaourt': ['yaourt', '--noconfirm'], 9 | } 10 | 11 | 12 | def package_installed(module, package_name): 13 | cmd = ['pacman', '-Q', package_name] 14 | exit_code, _, _ = module.run_command(cmd, check_rc=False) 15 | return exit_code == 0 16 | 17 | 18 | def update_packages(module, tool, auronly): 19 | assert tool in TOOL_CMD_MAP 20 | 21 | cmd = ['env', 'LC_ALL=C'] + TOOL_CMD_MAP[tool] + ['-Su'] 22 | if auronly: 23 | cmd += ['--aur'] 24 | rc, stdout, stderr = module.run_command(cmd, check_rc=True) 25 | 26 | module.exit_json( 27 | changed='there is nothing to do' not in stdout, 28 | msg='updated packages', 29 | ) 30 | 31 | 32 | def install_packages(module, package_name, tool, update, auronly): 33 | if package_installed(module, package_name): 34 | module.exit_json( 35 | changed=False, 36 | msg='package already installed', 37 | ) 38 | 39 | assert tool in TOOL_CMD_MAP 40 | 41 | options = '-S' 42 | 43 | if update: 44 | options += 'u' 45 | 46 | cmd = TOOL_CMD_MAP[tool] + [options, package_name] 47 | if auronly: 48 | cmd += ['--aur'] 49 | module.run_command(cmd, check_rc=True) 50 | 51 | module.exit_json( 52 | changed=True, 53 | msg='installed package', 54 | ) 55 | 56 | 57 | def remove_packages(module, package_name, recurse, nosave): 58 | if not package_installed(module, package_name): 59 | module.exit_json( 60 | changed=False, 61 | msg='package not installed', 62 | ) 63 | 64 | options = '-R' 65 | 66 | if nosave: 67 | options += 'n' 68 | 69 | if recurse: 70 | options += 's' 71 | 72 | cmd = ['pacman', '--noconfirm', options, package_name] 73 | module.run_command(cmd, check_rc=True) 74 | 75 | module.exit_json( 76 | changed=True, 77 | msg='removed package', 78 | ) 79 | 80 | 81 | def main(): 82 | module = AnsibleModule( 83 | argument_spec={ 84 | 'name': { 85 | 'required': False, 86 | }, 87 | 'state': { 88 | 'default': 'present', 89 | 'choices': ['present', 'absent'], 90 | }, 91 | 'tool': { 92 | 'default': 'pacaur', 93 | 'choices': ['pacaur', 'yaourt'], 94 | }, 95 | 'recurse': { 96 | 'default': True, 97 | 'type': 'bool', 98 | }, 99 | 'nosave': { 100 | 'default': True, 101 | 'type': 'bool', 102 | }, 103 | 'update': { 104 | 'default': False, 105 | 'type': 'bool', 106 | }, 107 | 'auronly': { 108 | 'default': True, 109 | 'type': 'bool', 110 | }, 111 | }, 112 | required_one_of=[['name', 'update']], 113 | ) 114 | 115 | params = module.params 116 | 117 | if params['update'] and not params['name']: 118 | update_packages(module, params['tool'], params['auronly']) 119 | elif params['state'] == 'present': 120 | install_packages( 121 | module, params['name'], params['tool'], params['update'], 122 | params['auronly'], 123 | ) 124 | elif params['state'] == 'absent': 125 | remove_packages( 126 | module, params['name'], params['recurse'], params['nosave'], 127 | ) 128 | 129 | 130 | if __name__ == '__main__': 131 | main() 132 | --------------------------------------------------------------------------------