Ansible playbook which uses a role defined in a collection - ansible

This is an example of an Ansible playbook I am currently playing around with:
---
- hosts: all
collections:
- mynamespace.my_collection
roles:
- mynamespace.my_role1
- mynamespace.my_role2
- geerlingguy.repo-remi
The mynamespace.my_collection collection is a custom collection that contains a couple of roles, namely mynamespace.my_role1 and mynamespace.my_role2.
I have a requirements.yml file as follows:
---
collections:
- name: git#github.com:mynamespace/my_collection.git
roles:
- name: geerlingguy.repo-remi
version: "2.0.1"
And I install the collection and roles as follows:
ansible-galaxy collection install -r /home/ansible/requirements.yml --force
ansible-galaxy role install -r /home/ansible/requirements.yml --force
Each time I attempt to run the playbook it fails with the following error:
ERROR! the role 'mynamespace.my_role1' was not found in mynamespace.my_collection:ansible.legacy:/home/ansible/roles:/home/ansible_roles:/home/ansible
The error appears to be in '/home/ansible/play.yml': line 42, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
roles:
- mynamespace.my_role1
^ here
For the avoidance of doubt, I have tried multiple ways of defining the roles in the playbook including mynamespace.my_collection.my_role1 (the fully qualified name of the role within the collection).
I suspect I've done something wrong or misunderstood how it should work but my understanding is a collection can contain multiple roles and once that collection is installed, I should be able to call upon one or more of the roles within the collection inside my playbook to use it but it doesn't seem to be working for me.
The error seems to suggest it is looking for the role inside the collection but not finding it.
The collection is installed to the path /home/ansible_collections/mynamespace/my_collection and within that directory is roles/my_role1 and roles/my_role2.
Maybe the structure of the roles inside the collection is wrong?
I'm using Ansible 2.10 on CentOS 8.
Thanks for any advice!
EDIT:
I just wanted to expand on something I alluded to earlier. I believe the docs say the fully qualified name should be used to reference the role in the collection within the playbook.
Unfortunately, this errors too:
ERROR! the role 'mynamespace.my_collection.my_role1' was not found in mynamespace.my_collection:ansible.legacy:/home/ansible/roles:/home/ansible_roles:/home/ansible
The error appears to be in '/home/ansible/play.yml': line 42, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
roles:
- mynamespace.my_collection.my_role1
^ here

I posted this as an issue over at the ansible/ansible repo and we did get to the bottom of this.
One small clue missing is the contents of my /etc/ansible/ansible.cfg file:
COLLECTIONS_PATHS(/etc/ansible/ansible.cfg) = ['/home/ansible_collections']
DEFAULT_ROLES_PATH(/etc/ansible/ansible.cfg) = ['/home/ansible_roles']
To quote contributor Sloane Hertel directly:
There's a bug/discrepancy between how ansible-galaxy and ansible handle the collections path. ansible-galaxy tries to be smart and adds ansible_collections to the path if it's not there already, but ansible always joins ansible_collections to the path - the playbook is trying to find the collection in /home/ansible_collections/ansible_collections/.
The solution, therefore, is to change my COLLECTIONS_PATHS value from /home/ansible_collections to /home.
From then on ansible-playbook will be searching for any roles in collections in the path /home/ansible_collections/mynamespace/roles instead of /home/ansible_collections/ansible_collections/mynamespace/roles.
I changed my directory structure slightly:
home/
├─ ansible_collections/
│ ├─ mynamespace/
│ │ ├─ my_collection/
│ │ │ ├─ roles/
│ │ │ │ ├─ mynamespace
│ │ │ │ │ ├─ my_role1/
│ │ │ │ │ │ ├─ meta/
│ │ │ │ │ │ ├─ tasks/
Which now means my playbook file looks like:
---
- hosts: all
collections:
- mynamespace.my_collection
roles:
- mynamespace.my_role1
- mynamespace.my_role2
- geerlingguy.repo-remi
And the roles are now found correctly.

ansible playbook, was formerly known as converge. Try converting your workspace to yml file format for read only data structure.

Related

Ansible can't find roles from collection

I got collection that has structure
namespace/
── collectionA/
├── docs/
├── galaxy.yml
├── README.md
└── roles/
├── roleA/
| └── tasks/
| ├──taskA.yml
| ├──taskB.yml
└── roleB/
└── tasks/
├──taskA.yml
├──taskB.yml
according to using collections if I wan to use that roles all I have to do is include_role with fqdn
- hosts: all
collections:
- my_namespace.my_collection
tasks:
- import_role:
name: role1
but it seems not working. I still get error:
ERROR! the role 'manage_users' was not found in edaas.post_provisioning:ansible.legacy:/home/jenkins/agent/workspace/Create_Infra/playbooks/roles:/home/cirunner/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/home/jenkins/agent/workspace/Create_Infra/playbooks
12:10:53
12:10:53 The error appears to be in '/home/jenkins/agent/workspace/Create_Infra/playbooks/ansible_main_initial.yml': line 24, column 15, but may
12:10:53 be elsewhere in the file depending on the exact syntax problem.
12:10:53
12:10:53 The offending line appears to be:
12:10:53
12:10:53 - ansible.builtin.import_role:
12:10:53 name: manage_users
12:10:53 ^ here
Collection is installed correctly - checked by ansible-galaxy collection list
Any idea what can be still wrong? Role names are aligned to rules (lowercase and only characters with _
Collection is installed in /home/cirunner/.ansible/collections
ansible [core 2.11.12] config file = None configured module
search path = ['/home/cirunner/.ansible/plugins/modules',
'/usr/share/ansible/plugins/modules'] ansible python module location
= /usr/local/lib/python3.8/dist-packages/ansible ansible collection location = /home/cirunner/.ansible/collections executable location =
/usr/local/bin/ansible python version = 3.8.0 (default, Dec 9 2021,
17:53:27) [GCC 8.4.0] jinja version = 3.1.2 libyaml = True
I came across this thread, and my issue was I had hyphens in my role name, which is not supported as per the documentation found here.
Changing to underscores in the role name resolved the issue.
I know this is old but I had a similar issue and it took me hours to find my problem. Maybe the way I figured it out will help someone else.
My problem was an empty collection directory left over from some tests in my project directory <project_dir>/collections/ansible_collection/my_namespace/my_collection directory. ansible-galaxy found the real installed collection containing the role in /usr/share/ansible/collections and reported it as installed and everything fine.
But ansible-playbook found the empty directory in my project directory first and interpreted this as the collection location and didn't look further to the actual installed collection in /usr/share/ansible/collections.
So, how I figured it out and how you might be able to figure out your problem:
Explicitly mention the collection in your playbook:
...
collections:
- my_namespace.my_collection
Add at least four '-v' arguments when running your playbook:
ansible-playbook ... -vvvv
Search for following line and see where your collection was found:
Loading collection my_namespace.my_collection from <project_dir>/collections/ansible_collections/my_namespace/my_collection
I hope it helps.

Export Variable in Linux Terminal with Multiple Strings Terraform

I have this variable in Terraform called "example". The variable is a type of "list(strings)" and I need to export that variable in my terminal, when running Terraform plan (i cannot put the value in code, so I have to export it):
export VAR_example=""xyz", "abc", "123""
but when I run this^, it gives me error:
│ Error: Invalid number literal
│
│ on <value for var.example> line 1:
│ (source code not available)
│
│ Failed to recognize the value of this number literal.
╵
╷
│ Error: Extra characters after expression
│
│ on <value for var.example> line 1:
│ (source code not available)
│
│ An expression was successfully parsed, but extra characters were found after it.
Then, I tried exporting it like this:
export VAR_example='"xyz", "abc", "123"'
Then, I got the error:
╷
│ Error: Extra characters after expression
│
│ on <value for var.example> line 1:
│ (source code not available)
│
│ An expression was successfully parsed, but extra characters were found after it.
Now, I am not sure how to export it at all. Any solution would be helpful.
Since it is a list of strings, you could provide it this way as well [1]:
terraform apply -var='examples=["xyz", "abc", "123"]'
However, since an environment variable needs to be used you could set it like:
export TF_VAR_examples='["xyz", "abc", "123"]'
Note that you need to prefix any environment variables with TF_ in order for Terraform to pick them up [2].
[1] https://www.terraform.io/language/values/variables#variables-on-the-command-line
[2] https://www.terraform.io/cli/config/environment-variables#tf_var_name

"The specified collections path is not part of the configured Ansible collections paths" but I'm installing into a relative directory

I'm installing the ansible.posix collection to use in my playbook like this:
ansible-galaxy collection install -r ansible/requirements.yml -p ansible/collections
However, I get this warning message that I want to get rid of:
[WARNING]: The specified collections path '/home/myuser/path/to/my/repo/ansible/collections' is not part of the
configured Ansible collections paths '/home/myuser/.ansible/collections:/usr/share/ansible/collections'. The installed collection won't be
picked up in an Ansible run.
My repo is laid out like this:
├── ansible
│   ├── playbook.yml
│   ├── files
│   │   ├── ...
│   ├── tasks
│   │   ├── ...
│   ├── requirements.yml
├── ansible.cfg
...
ansible.cfg looks like this:
[defaults]
timeout = 60
callback_whitelist = profile_tasks
Here's the output of ansible --version:
ansible 2.9.17
config file = /home/myuser/path/to/my/repo/ansible.cfg
configured module search path = ['/home/myuser/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.7/dist-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.7.3 (default, Jul 25 2020, 13:03:44) [GCC 8.3.0]
In the docs for installing collections with ansible-galaxy, they say the following:
You can also keep a collection adjacent to the current playbook, under a collections/ansible_collections/ directory structure.
play.yml
├── collections/
│ └── ansible_collections/
│ └── my_namespace/
│ └── my_collection/<collection structure lives here>
And, like the documentation suggests, I can still use the collection just fine in my play. But this warning message is quite annoying. How do I get rid of it?
I have created ansible.cfg within the ansible project I'm working on.
You could simply cp /etc/ansible/ansible.cfg .
but since the file would look like:
[defaults]
collections_paths = ./collections/ansible_collections
It is just easier to create it.
Once there, Ansible will know about your custom configuration file.
In you project folder you will:
mkdir -p ./collections/ansible_collections
And then run the install.
If your requirements.yml contains a collection like:
collections:
- community.general
You'd have to install it as:
ansible-galaxy collection install -r requirements.yml -p ./collections/
And the output would be:
[borat#mypotatopc mycoolproject]$ ansible-galaxy collection install -r requirements.yml -p ./collections/
Process install dependency map
Starting collection install process
Installing 'community.general:3.1.0' to '/home/borat/projects/mycoolproject/collections/ansible_collections/community/general'
In case you won't setup your modified ansible.cfg, the output would be:
[borat#mypotatopc mycoolproject]$ ansible-galaxy collection install -r requirements.yml -p ./
[WARNING]: The specified collections path '/home/borat/projects/mycoolproject' is not part of the configured Ansible collections paths
'/home/borat/.ansible/collections:/usr/share/ansible/collections'. The installed collection won't be picked up in an Ansible run.
Process install dependency map
Starting collection install process
Installing 'community.general:3.1.0' to '/home/borat/projects/mycoolproject/ansible_collections/community/general'
There are other methods too, but I like this one.

Golang cannot write file into directory

Situation:
I'm trying to write a file into a directory, like shown as follows:
func (p *Page) Save() error {
filepath := DerivePath(p.Title)
fmt.Println(filepath)
content, _ := json.MarshalIndent(p, "", " ")
err := ioutil.WriteFile(filepath, content, 0600)
return err
}
Problem:
The following error occurs in line 5:
open data/Testpage.json: The system cannot find the path specified.
I already tried to create the file before writing with os.Create, but it doesn't work either.
Loading from the data directory works just fine. Only writing new files into the directory fails.
Additional information:
My project structure is as follows:
│ .gitignore
│ .project
│
├───bin
│ main.exe
│
├───data
│ Welcome.json
│
├───pkg
│ └───windows_amd64
│ page.a
│
├───src
│ ├───main
│ │ main.go
│ │
│ └───page
│ page.go
│ page_test.go
│
└───templates
view.html
As mentioned above, reading data/Welcome.json works just fine (by using io/ioutils.ReadFile).
The source is available on https://gitlab.com/thyaris/Wiki.
Executing D:\GitWorkspaces\Wiki\wiki>go test -v page writes the following output:
=== RUN TestSave
data/Testpage.json
--- FAIL: TestSave (0.00s)
page_test.go:15: open data/Testpage.json: The system cannot find the path specified.
page_test.go:19: 'Testpage.json' was not created
=== RUN TestLoadPage
--- FAIL: TestLoadPage (0.00s)
page_test.go:26: Error while loading
page_test.go:32: File content did not match
=== RUN TestDelete
--- PASS: TestDelete (0.00s)
FAIL
exit status 1
FAIL page 0.094s
Your problem here is that the test engine is not running your executable with the working directory you expect. Instead of using the working directory defined by your shell or IDE, it is setting it to the source directory of the code being tested. (I had this bite me too once, long ago :) I had almost forgotten about that...)
The simple solution is to change DerivePath so that you can set the prefix externally, then change it to a the path you need at the beginning of your tests. There are other (possibly better?) solutions of course.

Puppet unable to find my .erb template but finds other files OK?

No one has been able to explain this inside my company so if you are able to solve this KUDOS to you!
Inside my puppet repo I have setup as follows:
environment/ops/modules/papertrail
├── files
│ ├── elasticsearch_log_files.yml
│ ├── log_files.yml
│ └── remote_syslog.conf
|
└── manifests
├── elasticsearch.pp
└──init.pp
└── templates
└── elasticsearch_log_files.yml.erb
MY elasticsearch.pp file contains the following:
class papertrail::elasticsearch inherits papertrail {
$source = "puppet:///modules/papertrail"
file { "/etc/log_files.yml" :
mode => 0644,
owner => root,
group => root,
ensure => present,
source => "$source/elasticsearch_log_files.yml",
}
}
Now when I try to change the last line to:
"$source/elasticsearch_log_files.yml.erb",
or
"$source/templates/elasticsearch_log_files.yml",
Puppet errors out and says that it can't locate the file:
Error: /Stage[main]/Papertrail::Elasticsearch/File[/etc/log_files.yml]: Could not evaluate: Could not retrieve information from environment ops source(s) puppet:///modules/papertrail/elasticsearch_log_files.yml.erb
What is strange is that when I use the following stanza to just include the yml file instead of erb it works fine and the file gets populated on the target:
"$source/elasticsearch_log_files.yml",
How can I include my erb? I have dynamic variables that I need to assign to the configuration file log_files.yml and I am so far unable to do so =(
This is solved. I didn't add the template directory to my git commit so once added with git add . it worked.

Resources