---
- name: PlayBook Betriebssystemversion anzeigen
hosts: all
gather_facts: yes
become: true
tasks:
- name: "jammy"
ansible.builtin.debug:
var: ansible_facts.lsb.codename
- name: "Ubuntu 22.04.4 LTS"
ansible.builtin.debug:
var: ansible_facts.lsb.description
- name: "Ubuntu"
ansible.builtin.debug:
var: ansible_facts.lsb.id
- name: "22"
ansible.builtin.debug:
var: ansible_facts.lsb.major_release
- name: "22.04"
ansible.builtin.debug:
var: ansible_facts.lsb.release
> ansible-playbook -i inventory.yml -l test01 playbook_update.yml
> ansible-playbook -i inventory.yml -l test02 playbook_update.yml
---
- name: Dateien und Verzeichnisse löschen
file:
path: "{{ item }}"
state: absent
with_items:
- /eine/datei.txt
- /ein/verzeichnis/
---
# Inhalte von einem Verzeichnis löschen; z.B. von einem Mount-Point
- block:
- name: Dateien im Verzeichnis einlesen
find:
paths: "/etc/nginx/modules-enabled"
hidden: True
recurse: True
register: collected_files
- name: Sym-Links im Verzeichnis einlesen
find:
paths: "/etc/nginx/modules-enabled"
hidden: True
recurse: True
file_type: link
register: collected_link
- name: Unterverzeichnisse einlesen
find:
paths: "/etc/nginx/modules-enabled"
hidden: True
recurse: True
file_type: directory
register: collected_directories
- name: entferne Dateien und Unterverzeichnisse aus dem Verzeichnis
file:
path: "{{ item.path }}"
state: absent
with_items: >
{{
collected_files.files
+ collected_link.files
+ collected_directories.files
}}
===== LineInFile =====
---
# -----------------------------------------------------------
# Tests
# -----------------------------------------------------------
- name: "Install Squid on remote hosts"
gather_facts: false
become: true
hosts: servers
vars_files:
- include_variables.yml
# https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html#examples
tasks:
- name: Add a line to a file if the file does not exist, without passing regexp
ansible.builtin.lineinfile:
path: /tmp/testfile
line: 192.168.1.99 foo.lab.net foo
create: yes
===== BlockInFile =====
[[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/blockinfile_module.html]]
- name: Insert/Update HTML surrounded by custom markers after line
ansible.builtin.blockinfile:
path: /var/www/html/index.html
marker: ""
insertafter: ""
block: |
Welcome to {{ ansible_hostname }}
Last updated on {{ ansible_date_time.iso8601 }}
- name: Remove HTML as well as surrounding markers
ansible.builtin.blockinfile:
path: /var/www/html/index.html
marker: ""
block: ""
- name: Textblock aus der Datei entfernen
ansible.builtin.blockinfile:
path: /tmp/test.txt
marker: "# {mark} Sicherheitssektion"
block: ""
state: absent
- name: Replace old hostname with new hostname (requires Ansible >= 2.4)
ansible.builtin.replace:
path: /etc/hosts
regexp: '(\s+)old\.host\.name(\s+.*)?$'
replace: '\1new.host.name\2'
- name: Supports a validate command
ansible.builtin.replace:
path: /etc/apache/ports
regexp: '^(NameVirtualHost|Listen)\s+80\s*$'
replace: '\1 127.0.0.1:8080'
validate: '/usr/sbin/apache2ctl -f %s -t'