I have a playbook and a bunch of modules I wrote.
Now I want to reuse the same modules in my next playbook for a different project.
I really want to push those modules to a public git repository and then somehow tell ansible to use the modules from the git repository.
(kinda like npm package.json referencing github)
I can't seem to find any documentation on how to do that.
For now, I am using a workaround where I tell people to npm install the repository, and then define ANSIBLE_LIBRARY variable.
How can I tell the playbook to load modules from a github repository or some other remote location?
Actually modules can be nested inside roles since quite a long time. Since Ansible 2 this even is possible with most of the plugins.
The folders where the modules & plugins need to be stored inside the role is the same as on playbook level. Modules go into library, plugins go into *_plugins (action_plugins, callback_plugins, filter_plugins etc)
To make the module/plugin then available the role has to be applied to the playbook (or added as a dependency of another role)
Only exception known to me are variable plugins and that perfectly makes sense. Since variable plugins are executed when the inventory is read, which happens before roles are interpreted.
vars_plugins still can be distributed in roles, but the path needs to be added in the ansible.cfg. Fortunately you can also use wildcards in paths:
vars_plugins = roles/*/vars_plugins
And no, all of this is not documented in any way. :)
Finally, to distribute roles you can use Ansible Galaxy:
ansible-galaxy install foo
Nothing wrong with directly using git. Ansible Galaxy actually only is a tool to install git repositories. But since Galaxy is the Ansible standard I suggest to at least provide a Galaxy compatible format. A good (best?) practice how to install Galaxy roles separate from the projects roles can be found here.
Here's an example for an action plugin: https://galaxy.ansible.com/udondan/ssh-reconnect/
There is no solution for that at the moment. Maybe you can add a playbook to download the modules to your project to avoid npm, but thats even not that nice.
I have my custom modules in a directory next to my playbooks. This directory is defined in my global ansible.cfg file:
library = /usr/share/ansible
The only drawbag here is that i allways have the same version for modules on all playbboks.
Related
Currently I am working on an API which uses Serverless Framework with Go.
I'm using the Serverless-offline plugin for local testing.
This API depends on a few other repositories (which I also maintain), which I import using the go.mod file.
However I am having a hard time refining my developer workflow.
Currently, if I want to make changes in a repository which this API depends upon, I have to alter the projects go.mod to include replace directives for the purpose of testing, but then I'm having to manually change it back for deployment to production.
Basically I'm looking for a way to include replace directives, which only get applied during local development. How has everyone else dealt with this problem?
Bonus question: Is there any way to run Serverless offline in docker? I'm finding that serverless-offline running on the bare metal is causing inconsistencies between different developers environments.
You can run go commands with an alternate go.mod file with the -modfile option:
From Build commands:
The -modfile=file.mod flag instructs the go command to read (and
possibly write) an alternate file instead of go.mod in the module root
directory. The file’s name must end with .mod. A file named go.mod
must still be present in order to determine the module root directory,
but it is not accessed. When -modfile is specified, an alternate
go.sum file is also used: its path is derived from the -modfile flag
by trimming the .mod extension and appending .sum.
Create a local.go.mod file with the necessary replace directive for development and build, for example, with:
go build -modfile=local.go.mod ./...
I work in a closed environment in which machines do not have access to internet. I'm wondering if there is a way for me to load the dependency modules for development with Go. One way I am thinking is to get the package files, if there is any. However, I couldn't find any such file for the dependencies I am using.
There are several things you could do:
You can use replace directives in your go.mod file to redirect packages to local directories where you have copies of the dependency packages.
You can use go mod vendor (see docs) to "vendor" dependency packages locally.
You can use a local proxy and set the GOPROXY env var.
My current playbook is structured this way
projectroot
|
|--ubuntu2004
|
|--00_setup
|
|--vars
|--playbook.yml
|--readme.md
Because my playbook uses ansible.posix and I also commit my playbook into a github repo. I was hoping if there's a way to include the required collection in this case ansible.posix as a requirement and how do I install it?
I saw that there are multiple ways https://docs.ansible.com/ansible/latest/user_guide/collections_using.html#installing-collections
I was wondering what's the best practice way that makes sense when using a github repository as version control for the playbook?
There's a few ways that you could do this. I'd suggest a requirements file since it's the easiest to set up and to manage.
Create a requirements file that you use to install the required modules.
The best way would be to create a requirements file that references the collection(s) your playbook needs. Which you can then use to install the required collection(s) and / or role(s).
---
collections:
- name: ansible.posix
Store the module in a repository and install it through ansible-galaxy.
You could upload the module to a git repository and then install it. I wouldn't recommend this as storing dependencies in your source-code isn't considered a good practise when there's a tool to manage dependencies available.
ansible-galaxy collection install
git+https://github.com/organization/repo_name.git,devel
Install the module through a playbook
I've set up a master node to run playbooks on with Ansible before and installed modules through the command task in a playbook. As long as you don't reference / include any tasks or plays that use the module, this will work fine.
- name: Install ansible posix module
command: ansible-galaxy collection install ansible.posix
The ideal way would be to have to playbook install the required module(s) prior to executing the tasks. However it's not possible to ignore the error that is thrown when a module is missing, so you would have to have to create a play that doesn't include / reference any of the tasks using the module.
I am developing an a Ansible module for compile sources, like
- source_compile:
archive: /var/cache/sources/nginx.tar.gz
configure:
prefix: /usr
I will probably
Check source package. (If is url download it)
Make a unique build directory.
Unarchive source to build directory.
configure && make && make install
So I want use ansible core module get_url and unarchive and shell, But no found how.
You can execute other modules only from action plugin, not from module itself.
It's done with _execute_module helper function. See template action for example.
Also you may be interested in using other helper functions such as fetch_url to retrieve remote data and _low_level_execute_command to run shell commands instead of calling other modules.
I'd recommend inspecting Ansible core modules/actions sources code to get the idea of how things work.
I have my directory structure setup such that the playbooks directory is under /var/lib/awx/projects/ and roles is a symlink under playbooks.
I need symlinks for roles as I'm using git push from my local system to push playbooks/roles to Tower and both playbooks and roles are under a different git repo.
When I try to run a job in Tower, it complains that it can't find my roles. When I look at every path that ansible states that it has looked in, they are there.
Any ideas why my roles can't be found?
If the symlinks indeed are the problem (I can't tell) then git submodules might be a solution for you. You could add your "roles" repository as a submodule to the repository where the playbooks are located. You need to clone the parent repository then recursive:
git clone --recursive playbooks
Be aware submodules can be a huge PITA, but if you are required to have playbooks and roles in separate repositories and symlinks are indeed not followed by tower, this might be a solution.
Another solution would be to create a separate git repository for every role. Then you could install each role separately via ansible-galaxy. For that you need a requirements.yml with all your roles defined like this:
- src: https://gitsource.host/user/some_role
version: master
name: some_role
Ansible Tower would install all listed roles automatically, as described here.