DevOps

  • Git,  Korn Shell / Bash,  PowerShell,  Windows Bash

    Quick CRLF-to-LF Conversion in PowerShell Without `dos2unix`

    Although this topic has been discussed so often, I think I can still contribute something regarding CRLF/LF. You write a shell script on Windows, copy it to a Linux server, run it – and get: (Newer bash versions are even less helpful: cannot execute: required file not found.) The culprit is Windows line endings: **CRLF** (\r\n) instead of **LF** (\n). Bash treats the trailing \r as part of the command. If dos2unix is not at hand, PowerShell handles the conversion in a single line that isn’t particularly easy to remember: ## How it works – [IO.File]::ReadAllText($p) reads the whole file into a single string – line endings included. – -replace…

  • Git

    git switch and git restore: the split you probably missed (as I did)

    git checkout has been split into git switch and git restore. git checkout main and git switch main do exactly the same thing. So why does the second one exist? Because checkout is two commands wearing one name. It moves HEAD to another branch, and it overwrites files in your working tree from some commit. Those operations have nothing in common except that both touch files on disk — and one of them is destructive. That collision produces a genuinely bad failure mode: Same verb, same shape, no confirmation, no reflog entry to crawl back through. The working-tree copy is simply gone. And when a branch and a path share…

  • DevOps,  Python

    Quick YAML Validation Without Installing `yamllint`

    If Python and the PyYAML module are available, you can perform a simple YAML syntax check without installing yamllint: If the file is valid YAML, the command prints: If the parser encounters invalid YAML syntax, PyYAML raises an exception and reports the location of the error. This approach verifies that the file can be successfully parsed, making it useful for quick checks in scripts, CI jobs, or restricted environments where additional tools cannot be installed. However, it is important to understand that parsing is not the same as linting. PyYAML only checks whether the YAML syntax is valid. It does not detect style issues, formatting inconsistencies, duplicate keys, excessive line…

  • ansible,  Konzepte

    Deep Dive: Mastering the “Missing File Trap”

    This is a follow up to my previous post Mastering Dynamic Task Includes in Ansible. Because include_tasks evaluates variables at runtime (right when the play reaches that specific step), Ansible has no idea whether the target file actually exists when the playbook first starts. If a user passes a typo like action=instal instead of install, Ansible will execute every task right up to your include step, and then crash with a fatal “file not found” error. To prevent this, we use the with_first_found lookup plugin. It scans a list of files sequentially and includes the first one it actually finds on disk. By designing a deliberate fallback strategy, default_action.yml can…

  • ansible,  Konzepte

    Mastering Dynamic Task Includes in Ansible

    One of the best ways (and easy) to keep your Ansible playbooks clean, modular, and DRY (Don’t Repeat Yourself) is by using dynamic task includes. Instead of writing massive, conditional playbooks with dozens of when statements, you can let your data drive your execution. The core idea is beautifully simple: Depending on the value of the action variable (e.g., install, configure, or backup), Ansible will look for and execute install.yml, configure.yml, or backup.yml on the fly. Why Use This Pattern? ⚠️ The Gotchas (What to Watch Out For) While highly effective, include_tasks is evaluated at runtime, which introduces a couple of architectural quirks you need to design around. 1. The…

  • ansible,  Korn Shell / Bash,  Security

    Keeping an Eye on SSH Keys: Easily Review authorized_keys with awk

    Hello fellow Admins! Who hasn’t been there? The ~/.ssh/authorized_keys file is a crucial component for the security of your servers. But sometimes, especially when many keys are stored or very long keys are in use, the file can quickly become overwhelming. Getting a quick overview without endlessly scrolling through lengthy character strings is truly golden. That’s precisely why I have a small but powerful tip for you: an awk script that makes your authorized_keys more readable by shortening the SSH keys to a manageable length, without losing any vital information. Why is this useful? The awk Magic (Ansible-Aware!) This awk script shortens the actual SSH keys (the second column) to…

  • ansible,  CI/CD Pipelines

    Conditional Privilege Escalation in Ansible Playbooks

    Fixing become_user Failures When Already Logged in as this user, e.g. ansible_user Problem Description This error occurs when an Ansible task is configured with become: true and a become_user that matches the current login user (the remote_user). Even if you are already logged in as the target user, Ansible attempts to wrap the module execution in a privilege escalation command (typically sudo -u target_user). If the target user is not explicitly permitted in the /etc/sudoers file to “sudo to themselves,” the OS rejects the command, requesting a password that Ansible cannot provide. This is the classic “I am who I say I am, but I can’t prove it to myself”…

  • ansible,  Konzepte,  YAML

    Ansible Formatting Best Practices

    Readable and consistent Ansible code makes reviews easier, reduces errors, and helps linters such as yamllint and ansible-lint work reliably. The following conventions have proven useful in larger playbooks and roles. 1. Use folded scalars for long Jinja expressions Multiline Jinja expressions inside a quoted YAML scalar can confuse YAML parsers and linters. Avoid this: The expression is hard to read and may break YAML formatting checks. Use folded scalars instead: Advantages: 2. General YAML formatting Maintain a consistent layout across all playbooks. Recommended conventions: Example: This improves readability and keeps file structure consistent. 3. Always use Fully Qualified Collection Names (FQCN) Using FQCN avoids ambiguity and improves compatibility with…

  • CI/CD Pipelines,  Git,  Github,  Github Actions,  Gitlab,  Gitlab Runner

    Reliably Fetching Git Tags in GitLab Pipelines (and the GIT_DEPTH Pitfall)

    Finding the right version string in a CI/CD pipeline can be surprisingly tricky. If you’ve been searching for a way to grab the “latest” tag in your GitLab runner, you’ve likely stumbled across this specific combination of Git commands. At least I did… Here is a breakdown of what that command does, why it works, and the “gotchas” you need to look out for. The Command Breakdown The command is actually two Git commands nested together: 1. The Inner Command: git rev-list The part inside the parenthesis, git rev-list --tags --max-count=1, acts as a locator. 2. The Outer Command: git describe Once the inner command finds the “hash” (the unique…

  • ansible

    Get systemd services in ansible plays

    Hi, this is a tip to get details about linux (e.g. systemd) services in an ansible play. Just use the ansible service_facts module. You can use it without passing any argument. You can access services by using ansible_facts.services. As this is an dictionary, a single service is addressable by using ansible_facts.services[{{ service-name }}]. A few more examples: