Ansible




list available inventory variables of a host

chris@knechtruprecht [~]: ansible-inventory --host=hostname


Split/Join example

split.yml

---
- name: Split Example
  hosts: localhost
  gather_facts: false
  tags: ['splitexample']
  tasks:

  - set_fact:
      Orginal: "subsub.sub.domain.de"

  - set_fact:
      Subdomain: "{{ Orginal.split('.')[1:] | join('.') }}"

  - set_fact:
      Domain: "{{ Orginal.split('.')[2:] | join('.') }}"

  - ansible.builtin.debug:
      msg:
      - "Orginal: '{{ Orginal }}'"
      - "Subdomain: '{{ Subdomain }}'"
      - "Domain: '{{ Domain }}'"
output:

chris@knechtruprecht [~]: ansible-playbook split.yml

PLAY [Split Example] ***************************************************

TASK [set_fact] ********************************************************
ok: [localhost]

TASK [set_fact] ********************************************************
ok: [localhost]

TASK [set_fact] ********************************************************
ok: [localhost]

TASK [ansible.builtin.debug] *******************************************
ok: [localhost] => {
  "msg": [
      "Orginal: 'subsub.sub.domain.de'",
      "Subdomain: 'sub.domain.de'",
      "Domain: 'domain.de'"
  ] }

PLAY RECAP *************************************************************
localhost: ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0


Read data from csv-file example

community.general.read_csv module

csv.yml

---
- name: read data from csv-file
  hosts: localhost
  gather_facts: false
  tags: ['csv']

  tasks:
  - name: Read users from CSV file and return a dictionary
    community.general.read_csv:
      path: users.csv
      key: UID
      delimiter: ;
    register: users

  - ansible.builtin.debug:
      msg:
      - "UID: {{ item.key }}"
      - "GID: {{ item.value.GID }}"
      - "User: {{ item.value.User }}"
    loop: "{{ users.dict|dict2items }}"


users.csv:
UID;GID;User 1005;1007;chris 1006;1007;stefan 1007;1008;heinz

output:
chris@knechtruprecht [~]: ansible-playbook csv.yml

PLAY [read data from csv-file] ***********************************************************************************

TASK [Read users from CSV file and return a dictionary] **********************************************************
ok: [localhost]

TASK [ansible.builtin.debug] *************************************************************************************
ok: [localhost] => (item={u'key': u'1007', u'value': {u'GID': u'1008', u'UID': u'1007', u'User': u'heinz'}}) => {
    "msg": [
        "UID: 1007",
        "GID: 1008",
        "User: heinz"
    ]
}
ok: [localhost] => (item={u'key': u'1006', u'value': {u'GID': u'1007', u'UID': u'1006', u'User': u'stefan'}}) => {
    "msg": [
        "UID: 1006",
        "GID: 1007",
        "User: stefan"
    ]
}
ok: [localhost] => (item={u'key': u'1005', u'value': {u'GID': u'1007', u'UID': u'1005', u'User': u'chris'}}) => {
    "msg": [
        "UID: 1005",
        "GID: 1007",
        "User: chris"
    ]
}

PLAY RECAP *******************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 Top


sprintf like output example

Output of an integer variable with two digits (with leading zero if less than 10)

- set_fact:
  variable: "{{ '%02d'|format(Counter|int) }}"


filter examples

check if a string, stored in the variable "SearchVariable" is part of the variable "Variable"
I found a lot examples with Variable|search(SearchVariable) - this is not working since ansible 2.9
Also found a lot examples with a static searchstring

- set_fact:
    Variable: "This ist just a example"
    SearchVariable: "just"

- set_fact:
    var1: "{{ SearchVariable }} was found in {{ Variable }}"
  when: SearchVariable | string in Variable

- set_fact:
    Variable: "This ist just a example"
    SearchVariable: "nonsense"

- set_fact:
    var1: "{{ SearchVariable }} was not found in {{ Variable }}"
  when: SearchVariable | string not in Variable

check if a variable is ""
- set_fact:
    Variable: "new content"
  when: Variable|length == 0