www.knechtruprecht.de


Ansible Examples






list available inventory variables of a host



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

Top


Looping in Vars (2-dim-loop)


vars.yml:

data:
- name: Dataset1
  information: Information about Dataset1
  author: chris
  commands:
  - command_name: firstcommand
    type: binary
    parameter: '-f -r'
  - command_name: secondcommand
    type: binary
    parameter: '-h 127.0.0.1'
  - command_name: thirdcommand.pl
    type: perl_script
    parameter: ''
- name: Dataset2
  information: something about Dataset2
  author: stephan
  commands:
  - command_name: command123
    type: shell_script
    parameter: 'value=1'
  - command_name: grep
    type: binary
    parameter: 'huhu /tmp/file'
  - command_name: hello_world.sh
    type: bash_script
    parameter: 'out="hello world"'



main.yml:

---
- name: Just a demo
  hosts: localhost
  tags: ['demo']
  vars_files:
    - vars.yml
  tasks:
    - include_tasks: loop.yml
      vars:
        Name: "{{ item['name'] }}"
        Information: "{{ item['information'] }}"
        Author: "{{ item['author'] }}"
        Commands: "{{ item['commands'] }}"
      loop: "{{ data }}"



loop.yml:

---
- name: Just a Debugoutput
  debug:
    msg: "{{ Name }};{{ Information }};{{ Author }};{{ item_command['command_name'] }};{{ item_command['type'] }};{{ item_command['parameter'] }}"
  loop: "{{ Commands }}"
  loop_control:
    loop_var: item_command



output:

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

PLAY [Just a demo] **************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************
ok: [localhost]

TASK [include_tasks] ************************************************************************************************
included: /home/chris/loop.yml for localhost
included: /home/chris/loop.yml for localhost

TASK [Just a Debugoutput] *******************************************************************************************
ok: [localhost] => (item={u'type': u'binary', u'parameter': u'-f -r', u'command_name': u'firstcommand'}) => {
  "msg": "Dataset1;Information about Dataset1;chris;firstcommand;binary;-f -r"
}
ok: [localhost] => (item={u'type': u'binary', u'parameter': u'-h 127.0.0.1', u'command_name': u'secondcommand'}) => {
  "msg": "Dataset1;Information about Dataset1;chris;secondcommand;binary;-h 127.0.0.1"
}
ok: [localhost] => (item={u'type': u'perl_script', u'parameter': u'', u'command_name': u'thirdcommand.pl'}) => {
  "msg": "Dataset1;Information about Dataset1;chris;thirdcommand.pl;perl_script;"
}

TASK [Just a Debugoutput] *******************************************************************************************
ok: [localhost] => (item={u'type': u'shell_script', u'parameter': u'value=1', u'command_name': u'command123'}) => {
  "msg": "Dataset2;something about Dataset2;stephan;command123;shell_script;value=1"
}
ok: [localhost] => (item={u'type': u'binary', u'parameter': u'huhu /tmp/file', u'command_name': u'grep'}) => {
  "msg": "Dataset2;something about Dataset2;stephan;grep;binary;huhu /tmp/file"
}
ok: [localhost] => (item={u'type': u'bash_script', u'parameter': u'out="hello world"', u'command_name': u'hello_world.sh'}) => {
  "msg": "Dataset2;something about Dataset2;stephan;hello_world.sh;bash_script;out=\"hello world\""
}

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

Top


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

Top


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) }}"
Top


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
Top


'
-->