Skip to content

Instantly share code, notes, and snippets.

@eikaas
Created March 18, 2019 14:49
Show Gist options
  • Save eikaas/f6297d2a37bcf4802c8a3d61c99ca7f5 to your computer and use it in GitHub Desktop.
Save eikaas/f6297d2a37bcf4802c8a3d61c99ca7f5 to your computer and use it in GitHub Desktop.
tricks.yml
- name: Data-Wrestling Techniques
hosts: localhost
gather_facts: no
vars:
arr1:
- 1
- 2
- 3
arr3: [5, 6, 7, 8, 9, 10]
arr3: [91, 2, 3, 85, 2]
all: "{{ arr1 + arr2 + arr3 }}"
tasks:
- name: All
debug:
msg: "{{ items }}"
with_items: "{{ arr1 + arr2 + arr3 }}"
- name: Merged lists of unique values (parens=required)
debug:
msg: "{{ (arr1 + arr2 + arr3) | unique }}"
- name: Get home directory of the connected user
shell: "getent passwd {{ ansible_user | quote}} | awk -F: '{print $6}'"
register: homedir
- name: h
debug:
msg: "Homedir of {{ ansible_user | quote }} is {{ homedir.stdout }}"
- name: Defininig variables, multi-dimensional arrays
vars:
myvar: "Hello "
mylist:
- a
- b
myaltlist: [[four, five, six], [seven, eight, nine]]
debug:
msg: "{{ myvar }} {{ mylist }} {{ myaltlist }}"
- name: Produce a random number
debug:
msg: "{{ 100 | random }}"
- name: Validate m
vars:
might_be_ipaddrs: [192.15.192.1, 0.0.0.0/0, 10.0.0.0/24, 123.51.23.34/72, 123.51.23.34, "totaly not an ip":]
debug:
msg: "{{ might_be_ipaddrs | ipaddr }}"
- name: Convert (valid)IPv4-addr into the PTR-record of its equivalent IPv6-addr
vars:
maybeips: [192.15.192.1, 0.0.0.0/0, 10.0.0.0/24, 123.51.23.34/72, 123.51.23.34, 1.2.3.4]
debug:
msg: "{{ maybeips | ipaddr('ipv6') | ipaddr('revdns') }}"
- name: Create a SHA256 password-hash from plaintext password+salt
vars:
mypass: qwe123
mysalt: suchsalt
myhash: "{{ mypass | password_hash('sha256', mysalt) }}"
debug:
msg: "Pass: {{ mypass }}, Hash: {{ myhash }}, (Salt: {{ mysalt }})"
- name: Replace port with regex_replace
debug:
msg: "{{ '127.0.0.1:80' | regex_replace(':80', ':443') }}"
- name: The type_debug filter returns the underlying python datatype
vars:
things:
- 123
- "123"
- '123'
- kek
- ''
debug:
msg: "{{ things[0] | type_debug }}"
- name: Iterating python dictionaries (hash/struct/map types)
vars:
users:
alice:
name: Alice Appleworth
telephone: 123-456-7890
bob:
name: Bob Bananarama
telephone: 987-654-3210
debug:
msg: "User {{ item.key }} is {{ item.value.name }} with phone number {{ item.value.telephone }}"
with_dict: "{{ users }}"
- name: Iterating lists in parallell
vars:
alpha: ['a', 'b', 'c']
numbers: [1, 2, 3]
debug:
msg: "{{ item.0 }} and {{ item.1 }}"
with_together:
- "{{ alpha }}"
- "{{ numbers }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment