Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions examples/filters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,41 @@
hosts: rhel9vm
gather_facts: false
vars:
test: 123
test: 123
tasks:

- name: Create var which is mandatory
debug:
msg: "This var should be mandatory {{ test | mandatory }}"
ansible.builtin.debug:
msg: This var should be mandatory {{ test | mandatory }}

- name: Create users with optionan homedir
become: true
user:
ansible.builtin.user:
name: "{{ item.name }}"
shell: /bin/bash
groups: wheel
append: yes
append: true
home: "{{ item.homedir | default(omit) }}"
loop:
- name: james
homedir: /opt/james
- name: toni
- name: pepe

- name: Playing with selectattr
hosts: localhost
become: false
vars:
hosts:
- name: bastion
ip:
- 172.25.250.254
- 172.25.252.1
- name: classroom
ip:
- 172.25.252.254
hosts:
- name: bastion
ip:
- 172.25.250.254
- 172.25.252.1
- name: classroom
ip:
- 172.25.252.254

tasks:
- name: Debug using selectattr the 'name' elements from the list of dictionaries in 'hosts'
ansible.builtin.debug:
msg:
- "{{ hosts | selectattr('name', '==', 'bastion')| map(attribute='ip') }}"
- "{{ hosts | selectattr('name', '==', 'bastion') | map(attribute='ip') }}"
35 changes: 18 additions & 17 deletions examples/selectattr_map.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
- name: Playing with selectattr
hosts: localhost
become: false
vars:
hosts:
- name: bastion
ip:
- 172.25.250.254
- 172.25.252.1
- name: classroom
ip:
- 172.25.252.254
tasks:
- name: Debug using selectattr the 'name' elements from the list of dictionaries in 'hosts'
ansible.builtin.debug:
msg:
- "{{ hosts | selectattr('name', '==', 'bastion')| map(attribute='ip') }}"
---
- name: Playing with selectattr
hosts: localhost
become: false
vars:
hosts:
- name: bastion
ip:
- 172.25.250.254
- 172.25.252.1
- name: classroom
ip:
- 172.25.252.254
tasks:
- name: Debug using selectattr the 'name' elements from the list of dictionaries in 'hosts'
ansible.builtin.debug:
msg:
- "{{ hosts | selectattr('name', '==', 'bastion') | map(attribute='ip') }}"
38 changes: 20 additions & 18 deletions examples/workshop_jinja2_filters.yml
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
---
- name: Workshop using Jinja2 fitlers
hosts: serverb
become: true
become: true
vars:
expected_user: consultant1
tasks:
- name: Slurp files
ansible.builtin.slurp:
src: "{{ item }}"
loop:
- /etc/passwd
- /etc/shadow
- /etc/group
- /etc/passwd
- /etc/shadow
- /etc/group
register: slurped_files

- name: Set content fact
vars:
line_separator: "\n"
ansible.builtin.set_fact:
etc_passwd: >-
etc_passwd: >-
{{ slurped_files['results'] |
selectattr('source', '==', '/etc/passwd') | map(attribute='content') |
first | b64decode | split(line_separator) }}
etc_shadow: >-
{{ slurped_files['results'] |
selectattr('source', '==', '/etc/shadow') | map(attribute='content') |
first | b64decode | split(line_separator) }}
etc_group: >-
{{ slurped_files['results'] |
selectattr('source', '==', '/etc/group') | map(attribute='content') |
first | b64decode | split(line_separator) }}

etc_group: >-
{{ slurped_files['results'] | selectattr('source', '==', '/etc/group') | map(attribute='content') | first | b64decode | split(line_separator) }}

- name: Debug slurped files
ansible.builtin.debug:
msg: "{{ etc_passwd }}"
verbosity: 2

- name: Set passwd_line_match fact
vars:
line_search: "^{{ expected_user }}:"
passwd_line_match: >-
{% for LINE in etc_passwd -%}
{% if LINE | regex_search(line_search) -%}
{{ LINE }}
{%- endif %}
{%- endif %}

{%- endfor %}
ansible.builtin.debug:
var: passwd_line_match
Expand All @@ -55,31 +54,34 @@
{% for LINE in etc_shadow -%}
{% if LINE | regex_search(line_search) -%}
{{ LINE }}
{%- endif %}
{%- endif %}

{%- endfor %}
ansible.builtin.debug:
var: shadow_line_match

- name: Set group_line_match fact
vars:
line_search: "^{{ expected_user }}:"
group_line_match: >-
{% for LINE in etc_group -%}
{% if LINE | regex_search(line_search) -%}
{{ LINE }}
{%- endif %}
{%- endif %}

{%- endfor %}
ansible.builtin.debug:
var: group_line_match

- name: Set aux_group_line_match fact
vars:
line_search: ":{{ expected_user }}"
line_search: :{{ expected_user }}
aux_group_line_match: >-
{% for LINE in etc_group -%}
{% if LINE | regex_search(line_search) -%}
{{ LINE }}
{%- endif %}
{%- endif %}

{%- endfor %}
ansible.builtin.debug:
var: aux_group_line_match
13 changes: 7 additions & 6 deletions hello.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
hosts: all
tasks:
- name: Hello Message
debug:
msg: "Hello World"
- name: Hostname of machine
shell: hostname -f
ansible.builtin.debug:
msg: Hello World

- name: Hostname of machine
register: output
- debug:
msg: "This host is {{ output.stdout }}"
ansible.builtin.command: hostname -f
- ansible.builtin.debug:
msg: This host is {{ output.stdout }}