How to change the CI/CD pipeline structure to parallel or sequential in different conditions - cicd

I have two job like this:
stages:
- stage-name
variables:
VAR1: "abc"
job1:
stage: stage-name
rules:
- if: '$VAR1 == "abc"'
job2:
stage: stage-name
rules:
- if: '$VAR1 == "abc"'
Normally, two jobs will run parallel. However, I want to change condition in 'rules' keyword to make two jobs run sequentially. How I can do that?

Related

Referencing psuedo parameters in IF condition

I want to be able to create a condition that would be True IFF the current ${AWS::Partition} pseudo variable is aws-cn.
I found no reference for this in AWS CloudFormation docs.
You can make that condition in the following way:
Conditions:
AWSCN: !Equals
- !Sub '${AWS::Partition}'
- 'aws-cn'
And then use it in an IF condition as such:
!If
- AWSCN
- #TRUE
- #FALSE

Gitlab CI variable as a regex

I feel silly asking this question, but is it possible to do something like:
variables:
TAG_EXP: /some regex/
Some job:
only:
- $TAG_EXP
The above won't work, but is there a way to achieve a similar goal?
My issue is the tag is used on a lot of jobs and I'd like to have the expression in one easy to change place.
Use extends.
For example:
stages:
- build
.build:template:
stage: build
only:
- my_tag
build:one:
extends:
- .build:template
script:
- echo "my first job"
build:two:
extends:
- .build:template
script:
- echo "my second job"
And both build jobs will inherit the stage and only tags.

Datastore: composite index not recognised

I've written a piece of code that adds and retrieves entities from the Datastore based on one filter (and order on the same property) - that worked fine. But when I tried adding filters for more properties, I got:
PreconditionFailed: 412 no matching index found. recommended index is:- kind: Temperature properties: - name: DeviceID - name: created
Eventually I figured out that I need to create index.yaml. Mine looks like this:
indexes:
- kind: Temperature
ancestor: no
properties:
- name: ID
- name: created
- name: Value
And it seems to be recognised, as the console shows:
that it has been updated
Yet when I run my code (specifically this part with two properties), it doesn't work (still getting the above-mentioned error) (the code is running on the Compute Engine).
query.add_filter('created', '>=', newStart)
query.add_filter('created', '<', newEnd)
query.add_filter('DeviceID', '=', devID)
query.order = ['created']
Trying to run the same query on the console produces
Your Datastore does not have the composite index (developer-supplied) required for this query.
error. Search showed one other person who had the same issue and he managed to fix it by changing the order of the properties in the index.yaml, but that is not helping in my case. Has anybody encountered a similar problem or could help me with the solution?
You'll need to create the exact index suggested in the error message:
- kind: Temperature
ancestor: no
properties:
- name: DeviceID
- name: created
Specifically, the first property in the index needs to be DeviceID instead of ID and the last property in the index needs to be the one you're using in the inequality filter (so you can't have Value as the last property in the index).

How to use Ansible inventory variables in templates

Is that even possible to refer to a group variable while running a play for other group?
I have a specific case like that:
/etc/ansible/hosts
[group1]
server1.test.org
[group2]
server2.test.com
[group2:vars]
foo=bar
Running a play for group1
- name: test variables...
hosts: group1
gather_facts: no
tasks:
- debug: msg="foo={{ groups[group2].foo }}"
It is not working, I have tried other syntax variants without success.
The groups don't actually have variables defined for them when the inventory gets initialized. The hosts get a copy of what's defined for group variables. So to do what you want, you need to read from a host. Try this:
- name: test variables...
hosts: group1
gather_facts: no
tasks:
- debug: msg="foo={{ hostvars[groups['group2'][0]].foo }}"

How specify a list value as variable in ansible inventory file?

I need something like (ansible inventory file):
[example]
127.0.0.1 timezone="Europe/Amsterdam" locales="en_US","nl_NL"
However, ansible does not recognize 'locales' as a list.
You can pass a list or object like this:
[example]
127.0.0.1 timezone="Europe/Amsterdam" locales='["en_US", "nl_NL"]'
With complex variables, it's best to define them in a host_vars file rather than in the inventory file, since host_vars files support YAML syntax.
Try creating a host_vars/127.0.0.1 file with the following content:
---
timezone: Europe/Amsterdam
locales:
- en_US
- nl_NL
Ryler's answer is good in this specific case but I ran into problems using other variations with the template module.
[example]
127.0.0.1 timezone="Europe/Amsterdam" locales='["en_US", "nl_NL"]'
Is his original example and works fine.
The following variations work with template. Basically if it's a string you must remember to use the internal double quotes or the entire structure is parsed as a single string. If it's only numbers or "True" or "False" (not "yes") then you're fine. In this variation I couldn't make it work with template if it had external quotes.
I haven't done an exhaustive check of which internal use cases they do and do not break other than the template module.
I am using Ansible 2.2.1.
[example:vars]
# these work
myvar1=["foo", "bar"]
myvar2=[1,2]
myvar3=[True,False]
# These fail, they get interpreted as a single string.
myvar4=[yes, no]
myvar5=[foo,bar]
myvar6='["foo", "bar"]'
you can try split
#inventory file
[example]
127.0.0.1 timezone="Europe/Amsterdam" locales="en_US","nl_NL"
#role file
---
- debug: msg="{{ item }}"
with_items: locales.split(',')
I believe the case is where you define your variable.
if it is under a
[host:vars]
var=["a", "b"]
otherwise:
[hosts]
host1 var='["a", "b"]'
INI file with variables looks like this
$ cat ./vars/vars.yml
lvol_names=['2g-1','2g-2','2g-3']
the variable represents the list type
lvol_names:
- 2g-1
- 2g-2
- 2g-3
the variable can be read from a playbook via lookup:
$ cat ./play.yml
- name: play1
hosts: kub2_data_nodes
become: yes
vars:
- lvol_names: "{{ lookup('ini', 'lvol_names type=properties file=./vars/vars.yml') }}"
You can custom a filter, to split string to list
Github ansible example show how to create custom filter.