Overwrite facter core facts

Quick post today, more like a reminder for my future self than anything else 🙃

If you want to quickly change the value of a facter fact you can use ENV variables[*].

# show the operating system we are running (retrieved from facter)
$ puppet apply -e 'notify { "We are running on ${::operatingsystem}": }'
notice: We are running on Fedora

# override $operatingsystem for testing purposes
$ FACTER_operatingsystem=Debian puppet apply -e 'notify { "We are running on ${::operatingsystem}": }'
notice: We are running on Debian

This works just fine but sometimes you cannot rely on environment variables and you need something consistent system-wide.

The solution™️

Create a new JSON file (YAML is also possible) under /opt/puppetlabs/facter/facts.d/ with the facts and the new values.

{"fact_name": "new_value"}

Changing kernel fact

[root@puppet ~]# facter kernel
Linux
[root@puppet ~]# cat /opt/puppetlabs/facter/facts.d/overwrite_kernel.json
{
  "kernel": "Hurd"
}
[root@puppet ~]# facter kernel
Hurd
[root@puppet ~]#

Cool, what about structured facts

For structured facts you should to provide the whole JSON, otherwise you will lose those facts you don’t specify.

Example (note how release number was changed from v7.8 to v1.3):

[root@puppet ~]# cat /opt/puppetlabs/facter/facts.d/overwrite_os.json
{
  "os": {
    "architecture": "x86_64",
    "distro": {
      "codename": "Maipo",
      "description": "Red Hat Enterprise Linux Server release 7.8 (Maipo)",
      "id": "RedHatEnterpriseServer",
      "release": {
        "full": "1.3",
        "major": "1",
        "minor": "3"
      },
      "specification": ":core-4.1-amd64:core-4.1-noarch"
    },
    "family": "RedHat",
    "hardware": "x86_64",
    "name": "RedHat",
    "release": {
      "full": "1.3",
      "major": "1",
      "minor": "3"
    },
    "selinux": {
      "config_mode": "enforcing",
      "current_mode": "enforcing",
      "enabled": "true",
      "enforced": "false",
      "policy_version": "31"
    }
  }
}

And that’s it, simple and effective 😀

[*] Updated example from: https://www.puppetcookbook.com/posts/override-a-facter-fact.html

Acerca de dafero

Mi nombre es Daniel Fernández, Graduado en Ingeniería Informática. En el caso de querer ponerse en contacto conmigo podéis hacerlo a través de al dirección: daferoES en Gmail.com o a través de Twitter https://twitter.com/dafero
Esta entrada fue publicada en Uncategorized. Guarda el enlace permanente.

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s