Unable to use select match in ansible 2.8 - select

For this piece of code
"{{ recap_check.split('-')[1:] | select('!=','linux') | join('_') }}"
I am getting this error:
TemplateRuntimeError: no test named '!='
Same piece of code runs fine in 2.7.
It is possible that the variable I am sending in to the task is not being loaded.

select is using the builtin tests from Jinja.
In the above list of tests, you can see that not equal is actually ne.
So you Jinja line should be:
"{{ recap_check.split('-')[1:] | select('ne', 'linux') | join('_') }}"

So i dont know why the select doesn't work but this is how have it solved.
After printing out a whole lot of debugs
recap_check = 0-ueg-eeprom-linux
recap_role: "{{ recap_check.split('-')[1:2] | join('_') }}"
the select filter for '!~' or 'ne' doesnt work in ansible2.8 environment.

Related

Not able to set a github environment variable in github actions

My workflow is using a windows runner.
The workflow runs an aws cli command to get instance IDs. I then try to set the returned IDs as a github environment variable to use in future steps
Here is the snippet from my workflow file:
- name: Get IDs
id: get_instances
run: |
$getinfo = aws ec2 describe-instances --filters 'Name=tag:Name,Values=project-bell' --output text --query 'Reservations[*].Instances[*].InstanceId' --region "eu-west-1"
$instances = ($getinfo -Join " ")
echo $instances
echo "INSTANCE_ID=$instances" >> $GITHUB_ENV
- name: Print IDs
run: |
echo {{ env.INSTANCE_ID }}
echo "{{ env.INSTANCE_ID }}"
So the 1st line in my run command of the "Get IDs" step returns the IDs as an array. So $getinfo looks like this:
instanceID 1
instanceID 2
2nd line I use -Join command to get the IDs to be returned in a single line: instanceID 1 instanceID 2
I then try and set this single line as a github environment variable but it isn's working and not sure why.
When i echo $instances i can see that the -Join command has worked and i see the IDs printed as a single line instanceID 1 instanceID 2.
But on the "Print IDs" step {{ env.INSTANCE_ID }} is returned rather than the instanceID 1 instanceID 2 which i tried to set as a github environment variable in the previous step.
Notes:
I have to 2 echos on the "Print IDs" job as i was just testing if i needed quotations for it to work"
I have the -join command because when i tried to set $getinfo (1st run command) as a github env var i got an error saying it was an array
Question
Am I writing the command wrong to set github env?
Or is it something to do with the values themselves - like does it need to be a string?

ModuleNotFoundError: No module named 'azure.mgmt.network.version' [duplicate]

After upgrading ansible to version 2.10.5 and python3.8.10 my playbook.yml fails with this error.
ModuleNotFoundError: No module named 'azure.mgmt.monitor.version'
fatal: [localhost]: FAILED! => {"attempts": 1, "changed": false, "msg": "Failed to import the required Python library (ansible[azure] (azure >= 2.0.0)) on certrenewplay's Python /usr/bin/python3`
The module is there if I run python3 -c "import azure.mgmt.monitor" and if I run pip3 list I see it installed as azure-mgmt-monitor==2.0.0
The exact part of the playbook code that is erroring is this:
- name: Create _acme-challenge record for zone "{{ env_name_dot }}"
azure_rm_dnsrecordset:
subscription_id: "{{ mgmt_subscription }}"
client_id: "{{ mgmt_vault_azure_client_id }}"
tenant: "{{ mgmt_vault_azure_tenant_id }}"
secret: "{{ mgmt_vault_azure_client_secret }}"
resource_group: "{{ mgmt_rg }}"
relative_name: "_acme-challenge.{{ env_name }}"
zone_name: "{{ dns_zone_name }}.{{ dns_zone_domain }}"
record_type: TXT
state: present
records:
- entry: "{{ cn_challenge_data }}"
time_to_live: 60
when: dns_zone_name != 'activedrop'
register: add_record
retries: 1
delay: 10
until: add_record is succeeded
I'm not sure what I'm doing wrong-can anyone advise please or help me on this please?
Thanks
This same issue happened to me because Ansible now ships with its own version of the Azure collection and it was conflicting with the version I had manually installed in my own playbook using the "ansible-galaxy collection" command.
What I suggest you do is only use the version that ships with Ansible and then install its requirements like so:
pip install -r /usr/lib/python3/dist-packages/ansible_collections/azure/azcollection/requirements-azure.txt
It is easier to setup correctly on a freshly installed system (e.g in Docker) than it is to fix a broken system.
I think that you did not follow the instruction about installing azure collection from https://github.com/ansible-collections/azure
Installing collection itself does not install python dependencies, these are installed using python pip and you need to be sure you are installing them inside the same python (v)env where ansible is installed, or ansible will give you the error that you seen when trying to load the module.
Unfortunately the azure-mgmt-monitor package is bugged, even on 3.0.0 to not properly create a version submodule. Haven't been able to track down exactly where in the code it's busted, but it is and there is a direct import of that submodule in the Ansible Galaxy module causing it to fail. Unfortunately you should use the Azure CLI at this time and forget about using azure_rm

Using an include statement as the default value

I am creating a helm chart in which I want to specify a default for a value using a template function. Specifically I want to either use the override value image.name, or default to the template function chart.name:
{{ .Values.image.name | default include chart.name . }}
But when linting the chart I have the following error:
[ERROR] templates/: render error in "chart/templates/deployment.yaml": template: chart/templates/deployment.yaml:22:81: executing "chart/templates/deployment.yaml" at <include>: wrong number of args for include: want 2 got 0
Is it possible to use an included template function as the default value? Or can I only use literals?
You can. Just enclose your include statement in parentheses:
{{ .Values.image.name | default (include "chart.name" .)}}
Please see using the default function

Append release timestamp to helm template name

I'm struggling with finding a way to include the Release.Time builtin as part of a helm name.
If I just include it as:
name: {{ template "myapp.name" . }}-{{ .Release.Time }}
Dry run shows this:
name: myapp-seconds:1534946206 nanos:143228281
It seems like this is a *timestamp.Timestamp object or something because {{ .Release.Time | trimPrefix "seconds:" | trunc 10 }} outputs wrong type for value; expected string; got *timestamp.Timestamp
I can hack the string parsing by doing: {{ .Release.Time | toString | trimPrefix "seconds:" | trunc 10 }}, but it seems like I should be able to call something on the Timestamp object to get the seconds. Is anyone aware of where the docs are for this? I can't find any reference to it at https://godoc.org/github.com/Masterminds/sprig.
To format timestamp, you can use date FORMAT TIME from Sprig doc. And because .Release.Time got removed in Helm 3, you have to use now instead:
{{ now | date "20060102150405" }}
Refer the format options: https://golang.org/pkg/time/#Time.Format
But having timestamp in the name is not good idea because of upgrades.
In my case, by adding the following annotation I was able to achieve this, I am also using helmfile as the wrapper of my helm templates.
annotations:
deploymentTime: {{ now | date "2006-01-02T15:04:05" }}

Ansible passing variables between host contexts

As the title says when I'd like to be able to pass a variable that is registered under one host group to another, but I'm not sure how to do that and I couldn't find anything relevant under the variable documentation http://docs.ansible.com/ansible/playbooks_variables.html
This is a simplified example of what I am trying to see. I have a playbook that calls many different groups and checks where a symlink points. I'd like to be able to report all of the symlink targets to console at the end of the play.
The problem is the registered value is only valid under the host group that it was defined in. Is there a proper way of exporting these variables?
---
- hosts: max_logger
tasks:
- shell: ls -la /home/ubuntu/apps/max-logger/active | awk -F':' '{print $NF}'
register: max_logger_old_active
- hosts: max_data
tasks:
- shell: ls -la /home/ubuntu/apps/max-data/active | awk -F':' '{print $NF}'
register: max_data_old_active
- hosts: "localhost"
tasks:
- debug: >
msg="The old max_logger build is {{ max_logger_old_active.stdout }}
The old max_data build is {{ max_data_old_active.stdout }}"
You don't need to pass anything here (you just need to access). Registered variables are stored as host facts and they are stored in memory for the time the whole playbook is run, so you can access them from all subsequent plays.
This can be achieved using magic variable hostvars.
You need however to refer to a host name, which doesn't necessarily match the host group name (e.g. max_logger) which you posted in the question:
- hosts: "localhost"
tasks:
- debug: >
msg="The old max_logger build is {{ hostvars['max_logger_host'].max_logger_old_active.stdout }}
The old max_data build is {{ hostvars['max_data_host'].max_data_old_active.stdout }}"
You can also write hostvars['max_data_host']['max_data_old_active']['stdout'].