External facts is a great way to attach (arbitrary) metadata to a machine during the the launch of a new machine. E.g. when building a Centos 7 aws ec2 instance, you can generate the external facts via userdata.
Announcement
You can find all my latest posts on medium.Puppet can use these external facts in the way as any other fact. You can create external facts by simply creating a file. This file can be a:
- shell script
- yaml file
This file needs to be created inside any of the following 2 folders on a CentOS machine:
- /opt/puppetlabs/facter/facts.d/
- /etc/facter/facts.d
Note: you might need to create the above folders if they don’t already exist.
For example, let’s say I want to create 3 external facts, the ‘key-name=key-values’ are:
role='blog-site' pipeline_color='Blue' standby_mode=true
First off, let’s confirm that these facts don’t already exist:
[root@puppetagent1 ~]# facter -p role [root@puppetagent1 ~]# facter -p pipeline_color [root@puppetagent1 ~]# facter -p standby_mode
First you need to write this information in a shell-script format like this:
$ cat /etc/facter/facts.d/my_external_facts.sh #!/usr/bin/env bash echo "role=blog-site" echo "pipeline_color=Blue" echo "standby_mode=true"
Then make the script executable:
[root@puppetagent1 ~]# chmod ugo+x /etc/facter/facts.d/my_external_facts.sh
After that you should find the new external facts are now working:
[root@puppetagent1 ~]# facter -p role blog-site [root@puppetagent1 ~]# facter -p pipeline_color Blue [root@puppetagent1 ~]# facter -p standby_mode true
Note, instead of creating a shell script, we could have created the following yaml file:
[root@puppetagent1 ~]# cat /etc/facter/facts.d/my_external_facts.yaml --- role: blog-site pipeline_color: Blue standby_mode: true
This has the exact same end result:
[root@puppetagent1 ~]# facter -p role blog-site [root@puppetagent1 ~]# facter -p pipeline_color Blue [root@puppetagent1 ~]# facter -p standby_mode true
In this guide I have covered a few simple examples. The truth is that the external facts file isn’t just limited to either a shell script or yaml file. You can write it in lots of other forms, e.g. python script, json file,…etc. For more info, see the official external facts documentation.
If you are creating your external facts file via ec2 userdata, then here’s a handy example snippet to place inside your userdata script:
#!/usr/bin/env bash role='blog-site' pipeline_color='Blue' standby_mode='true' cat >/opt/puppetlabs/facter/facts.d/my_external_facts.sh << EOF #!/usr/bin/env bash echo "role=${role}" echo "pipeline_color=${pipeline_color}" echo "standby_mode=${standby_mode}" EOF chmod 0755 /opt/puppetlabs/facter/facts.d/my_external_facts.sh