๐ง Welcome to my Ansible Notes
Here are some notes which I find handy ...
๐ฅ Includes
๐ include vars
- name: Include environment vars files
ansible.builtin.include_vars:
file: vars/{{ env }}/secrets.yml
- name: Include Satellite main vars
ansible.builtin.include_vars: vars/capsule_satgitops_vars.yml
๐งฉ include tasks
- name: Test include
ansible.builtin.include_tasks: test_loop_item.yml
vars:
my_list_item: "{{ _my_list_item }}"
my_index: "{{ _index }}"
loop: "{{ my_list }}"
loop_control:
loop_var: _my_list_item
label: Some text
index_var: _index
tags:
- always
- name: Include role {{ role_2run }}
ansible.builtin.include_tasks: "tasks/baseline/{{ role_2run|regex_replace('^.*-(.*)$', '\\1') }}.yml"
๐งฑ include roles
- name: Include role specific tasks
ansible.builtin.include_tasks: tasks/role/roles_stub.yml
vars:
role_2run: "{{ role_item }}"
loop: "{{ some_roles }}"
loop_control:
loop_var: role_item
- name: Run tasks/other.yaml instead of 'main'
ansible.builtin.include_role:
name: myrole
tasks_from: other
- name: Pass variables to role
ansible.builtin.include_role:
name: myrole
vars:
rolevar1: value from task
when: not idontwanttorun
- name: Use role in loop
ansible.builtin.include_role:
name: '{{ roleinputvar }}'
loop:
- '{{ roleinput1 }}'
- '{{ roleinput2 }}'
loop_control:
loop_var: roleinputvar
- name: Apply tags to tasks within included file
ansible.builtin.include_role:
name: install
apply:
tags:
- install
tags:
- always
๐ Lists
โ Add item to list in a loop
- name: Example add list
ansible.builtin.set_fact:
new_list: "{{ new_list | default([]) + [item] }}"
loop:
- aap
- noot
- mies
๐งฎ List of hashes
๐ ๏ธ create dynamic
- name: Create list of hashes
vars:
systems: [ system1 , system8 , system3 ]
count: "{{ [1,2,3,4,5,6,7,8,9] | random }}"
ansible.builtin.set_fact:
new_list: "{{ new_list | default([]) + [ { 'system': item, 'count': count|int } ] }}"
loop: "{{ systems }}"
โ remove item
- set_fact:
new_node: "{{ new_node | default({}, true) | combine( { item.key: item.value }) }}"
loop: "{{ node | dict2items }}"
when: item.key is not match(key2delete)
โ๏ธ When clause
๐ use cases
โ True/False
Avoid comparison to empty string and โtrue/falseโ
Use:
rather than:Why? Ansible follows Pythonic truth testing.
- name: When Test defined and true/false
hosts: localhost
gather_facts: false
vars:
str1: ""
str2:
str3: []
str4: {}
num1: 0
num2: 1
num3: 9
num4: -9
tasks:
- name: Test empty
ansible.builtin.debug:
msg: Niet Defined is not defined
when: NietDefined is not defined
- name: Test NoneType
ansible.builtin.debug:
msg: All when tests belows give true
when:
- not str1
- not str2
- not str3
- not str4
- not num1
- num2
- num3
- num4
- name: Test length of var
ansible.builtin.debug:
msg: Have content ( length <= 0 )
when:
- str1 | default('',true) | length <= 0
- str2 | default('',true) | length <= 0
- str3 | default('',true) | length <= 0
- str4 | default('',true) | length <= 0
- name: Stop play via meta
ansible.builtin.meta: end_play
tags:
- always
๐งน ansible-lint
โ๏ธ .ansible-lint
warn_list:
- fqcn[action-core]
- yaml[trailing-spaces]
- yaml[line-length]
skip_list:
- run-once[task]
- no-changed-when
exclude_paths:
- .cache/
- test/
- playbooks/azure*
var_naming_pattern: "^[a-z_][a-z0-9_]*$"
loop_var_prefix: "^(__|{role}_)"
offline: true
๐ท๏ธ noqa
๐ท๏ธ skip_ansible_lint
Use tag:
๐ฆ Collection
๐งฐ common role
Or via meta/main.yml:
And use tasks from common_role:
๐๏ธ Project structure
/home/harry/ansible/project_Foo
โโโ ansible.cfg
โโโ collections
โ โโโ ansible_collections
โ โโโ zilux
โ โโโ tools
โ โโโ docs
โ โโโ galaxy.yml
โ โโโ meta
โ โโโ plugins
โ โโโ README.md
โ โโโ roles
โ โโโ zilux-tools-1.0.0.tar.gz
โโโ inventory
โโโ playbooks
โโโ collections
โโโ group_vars
โโโ test_col_zilux_tools.yml
.ansible-lint:
warn_list:
- fqcn[action-core]
- yaml[trailing-spaces]
- yaml[line-length]
skip_list:
- run-once[task]
- no-changed-when
- galaxy[no-changelog]
var_naming_pattern: "^[a-z_][a-z0-9_]*$"
loop_var_prefix: "^(__|{role}_)"
offline: true
.ansible-lint-ignore is empty.
galaxy.yml:
namespace: zilux
name: tools
version: 1.0.0
readme: README.md
authors:
- Harry Zijlmans <zijlmansh@gmail.com>
description: My personal tools collection
license:
- GPL-2.0-or-later
repository: http://docs.zilux.nl
meta/runtime.yml:
meta/main.yml of role:
galaxy_info:
author: Harry Zijlmans
description: acme role to test some stuff
company: zilux
license: BSD-3-Clause
min_ansible_version: '2.1'
platforms:
- name: Fedora
versions:
- all
galaxy_tags:
- tools
dependencies: []