Groesbeek, view of the 'National Liberation Museum 1944-1945' in Groesbeek. © Ton Kersten
Fork me on GitHub
Posts tagged as sysadm

Ansible with loops or lookup

2019-02-23 (150) by Ton Kersten, tagged as ansible, sysadm

Since Ansible version 2.5 there is a lot of discussion and confusion about the loop syntax. There is also discussion if with_...: will be replaced by loop: deprecating the with_... keywords. Even Ansibles documentation is not clear about this.

Should I use loop: or with_...:, in fact nobody really knows. What would the correct syntax be?

---
- name: Loops with with_ and lookup
  hosts: localhost
  connection: local
  gather_facts: no
  vars:
    people:
      - john
      - paul
      - mary
    drinks:
      - beer
      - wine
      - whisky

  tasks:
    - name: with nested
      debug:
        msg: "with_nested: item[0] is '{{ item[0] }}' and item[1] is '{{ item[1] }}'"
      with_nested:
        - "{{ people }}"
        - "{{ drinks }}"

    - name: nested and loop
      debug:
        msg: "nested_loop: item[0] is '{{ item[0] }}' and item[1] is '{{ item[1] }}'"
      loop:
        - "{{ people }}"
        - "{{ drinks }}"

Read more »

Ansible: One Role to Rule them All

2019-02-07 (149) by Ton Kersten, tagged as ansible, sysadm

I am a long time Ansible user and contributor (since 2012) and I have been struggling with a decent setup for a multi-environment case. I have been designing and re-designing a lot, until I came up with this design. And what a coincidence, a customer wanted a setup that was exactly this. So this concept is a real world setup, working in a production environment.

Did I get your attention? Read after the break, but take your time. it is a long read.

Read more »

Running it through Tattr (part 2)

2018-08-08 (148) by Ton Kersten, tagged as ansible, sysadm

Some time ago I created a playbook to show the content of a rendered template. When you keep digging in the Ansible documentation, you suddenly stumble over the template lookup-plugin. And then it turns out that my playbook is a bit clumsy.

A nicer and shorter way to do it:

---
#
# This playbook renders a template and shows the results
# Run this playbook with:
#
#       ansible-playbook -e templ=<name of the template> template_test.yml
#
- hosts: localhost
  become: false
  connection: local

  tasks:
    - fail:
        msg: "Bailing out. The play requires a template name (templ=...)"
      when: templ is undefined

    - name: show templating results
      debug:
        msg: "{{ lookup('template', templ) }}"

Ansible, loop in loop in loop in loop in loop

2018-06-08 (147) by Ton Kersten, tagged as ansible, loop, sysadm

A couple of days ago a client asked me if I could solve the following problem:

They have a large number of web servers, all running a plethora of PHP versions. These machines are locally managed with DirectAdmin, which manages the PHP configuration files as well. They are also running Ansible for all kind of configuration tasks. What they want is a simple playbook that ensures a certain line in all PHP ini files for all PHP versions on all webservers.

All the PHP directories match the pattern /etc/php[0-9][0-9].d.

Thinking about this, I came up with this solution (took me some time, though) smiley

---
- name: find all ini files in all /etc/php directories
  hosts: webservers
  user: ansible
  become: True
  become_user: root

  tasks:
    - name: get php directories
      find:
        file_type: directory
        paths:
          - /etc
        patterns:
           - php[0-9][0-9].d
      register: dirs

    - name: get files in php directories
      find:
        paths:
          - "{{ item.path }}"
        patterns:
          - "*.ini"
      loop: "{{ dirs.files }}"
      register: phpfiles

    - name: show all found files
      debug:
        msg: "Files is {{ item.1.path }}"
      with_subelements:
        - "{{ phpfiles.results }}"
        - files

The part with the with_subelements did the trick. Of course this line can be written as:

loop: "{{ query('subelements', phpfiles.results, files) }}"

Did you run it through TAttr

2017-08-15 (145) by Ton Kersten, tagged as ansible, sysadm

During my last Ansible training the students needed to create some Ansible templates for them selfs. As I do not want to run a testing template against some, or all, machines under Ansible control I created a small Ansible playbook to test templates.

Read more »

Stupid Fedora

2016-05-26 (144) by Ton Kersten, tagged as sysadm

Yesterday I removed a simple package from my Fedora 23 machine and after that I got the message

error: Failed to initialize NSS library

WTF??????

Searching the interwebs I found out I wasn’t the first, and probably not the last, to run into this problem.

It seems that, one way or another, the DNF package doesn’t know about the dependency it has on SQLite. So, when a package removal requests to remove SQLite, DNF removes it without questions. Ans thus break itself.

But how to fix this? DNF doesn’t work, but RPM doesn’t either, so there is no way to reinstall the SQLite packages.

Tinkering and probing I found this solution:

#!/bin/bash
url="http://ftp.nluug.nl/os/Linux/distr/fedora/linux/updates/23/x86_64/s/"
ver="3.11.0-3"

wget ${url}/sqlite-${ver}.fc23.x86_64.rpm
wget ${url}/sqlite-libs-${ver}.fc23.x86_64.rpm
rpm2cpio sqlite-${ver}.fc23.x86_64.rpm | cpio -idmv
rpm2cpio sqlite-libs-${ver}.fc23.x86_64.rpm | cpio -idmv
cp -Rp usr /
dnf --best --allowerasing install sqlite.x86_64

This downloads the SQLite package and SQLite library packages, extracts them and copies the missing files to their /usr destination. After doing that, DNF and RPM get working again. It could be that I downloaded an older version of the SQLite stuff, so to make sure I have a current version I reinstall SQLite again.

Maybe a good idea to fix that in DNF!

Docker panics

2014-04-14 (140) by Ton Kersten, tagged as sysadm

This morning I was messing around with Docker and I wanted to build me a nice, clean container with Ubuntu in it, to test Ansible thingies. I’ve done that before and everything worked as a charm. Until today.

I have this Dockerfile (I’ve stripped it to the bare bones that still fail):

FROM ubuntu:latest
MAINTAINER Ton_Kersten
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get -y update
RUN apt-get -y upgrade
RUN apt-get -y install git git-flow
RUN apt-add-repository -y ppa:mozillateam/firefox-next
RUN apt-get install -y firefox

and when I run

docker build .

I end up with a beautiful kernel panic. Whatever I try, panic Nothing in any logfile

I’m running kernel version Linux lynx 3.2.0-60-generic #91-Ubuntu SMP Wed Feb 19 03:54:44 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux which had no problems before.

The Docker version is Docker version 0.10.0, build dc9c28f

Is there anybody out there that knows what’s happening?

Thanks.

Ansible @ Loadays

2014-04-05 (139) by Ton Kersten, tagged as ansible, sysadm

Last Saturday I attended Loadays in Antwerp, Belgium.

After listening to Jan Piet Mens’s talk about Ansible, I was up for it.

At 11:30 sharp, I started my own presentation for an almost packed room. It’s called “Ansible, why and how I use it” and you can find it on SpeackerDeck.

It was a lovely talk, with a very knowledgeable crowd.

Please, have a look at it and if you have any questions, let me know.

Thanks to the crew for organizing such a lovely event, every year.

Photos of the event where taken by Robert Keerse and you can see them at his Google Plus page. Do enjoy!!

For those of you with a strong stomach, the complete presentation is on Youtube as well. Have a look at the Youtube stream

rsync on a not standard port

2013-01-21 (131) by Ton Kersten, tagged as linux, sysadm

Today a colleague asked me to sync some files to a server that is not listening on SSH port 22.

I normally create a configuration entry in my ~/.ssh/config file, like

Host tosync
    Hostname syncer.example.com
    Port 1234
    User syncuser

and then command

rsync -va --progress --inplace . tosync:

But this time I didn't want to create the entry in my SSH configuration, because I need this trick in a script. So I started to read the rsync manpage and after some experimenting I found

rsync -va --progress --inplace --rsh='ssh -p1234' . syncuser@syncer.example.com:

This syncs the current directory to host syncer.example.com on port 1234 as user syncuser.

Resize a partition

2012-10-19 (127) by Ton Kersten, tagged as sysadm

I often have to increase the size of a virtual disk on a virtual machine. But I always seem to forget how to do it. I guess I have done it over a 100 times and I cannot remember exactly how I did it. So this blog entry is to help people on how to do this and as a reminder to myself.

This example is done on a virtual machine with CentOS 6, but it can be done on every Linux. And in the fdisk examples I have left out some of the not to interesting lines.

Oke, here we go:

First, shut down your virtual machine and increase the disk size. Then start your virtual machine and go to the console. Now you have a virtual machine with a new disk size, but the current partition table needs to be adjusted to the new disk size. I know it’s possible with parted, but I always seem to end up on systems where it’s not available. So I just use fdisk.

# fdisk /dev/sda

Now give the p command, which prints the partition table and make a note of the start cylinder of the Linux LVM partition. This is the partition we are going to increase.

Please be very careful This trick only works if the partition you want to resize is at the end of the disk and contains a logical volume type system.

Command (m for help): p

Disk /dev/sda: 17.2 GB, 17179869184 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
/dev/sda2              65        2089    16264192   8e  Linux LVM

Delete the LVM partition (we will recreate this later)

Command (m for help): d
Partition number (1-5): 2

and create a new partition with the original starting point. The starting point should be the same, because all the partitions meta data is at the start of the partition.

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2

And fill in the start cylinder of the LVM partition we deleted above

First cylinder (1-25600, default 1): 65
Last cylinder, +cylinders or +size{K,M,G} (65-25600, default 25600):
Using default value 25600

and change the type to Linux LVM

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sda: 26.8 GB, 26843545600 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           2          64      262144   83  Linux
/dev/sda2              65        2089    24567892   8e  Linux LVM

If you agree with the new layout, write it to disk with the w command and quit with q. If it’s not the disk disk with the root volume on it, it could be possible that you can skip the next reboot. Just a partprobe could do the trick.

# reboot

First it’s needed to resize physical volume.

# pvresize /dev/sda2

Make sure you know how much free space you now have

# vgdisplay

Make a note of the “LV Name” of the logical volume you want to resize

# lvdisplay

Resize the logical volume. I use gigabytes as an example here.

# lvresize -L +[Size]GB [LV Name]

Resize the file system on the logical volume.

# resize2fs [LV Name]