Skip to content

Commit f050130

Browse files
committed
Adding to the archive because we got feedback that it was missing.
1 parent 02b7eef commit f050130

16 files changed

+2561
-0
lines changed

hiera/1/command_line.markdown

+185
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
---
2+
layout: default
3+
title: "Hiera 1: Command Line Usage"
4+
---
5+
6+
[priority_lookup]: ./lookup_types.html#priority-default
7+
[hash_lookup]: ./lookup_types.html#hash-merge
8+
[array_lookup]: ./lookup_types.html#array-merge
9+
10+
{% partial /hiera/_hiera_update.md %}
11+
12+
Hiera provides a command line tool that's useful for verifying that your hierarchy is constructed correctly and that your data sources are returning the values you expect. You'll typically run the Hiera command line tool on a puppet master, making up the facts agents would normally provide the puppet master using a variety of [fact sources](#fact-sources).
13+
14+
## Invocation
15+
16+
The simplest Hiera command takes a single argument --- the key to look up --- and will look up the key's value using the static [data sources](data_sources.html) in the [hierarchy](./hierarchy.html).
17+
18+
`$ hiera ntp_server`
19+
20+
A more standard invocation will provide a set of variables for Hiera to use, so that it can also use its dynamic [data sources](./data_sources.html):
21+
22+
`$ hiera ntp_server --yaml web01.example.com.yaml`
23+
24+
## Configuration File Location
25+
26+
The Hiera command line tool looks for its configuration in `/etc/hiera.yaml`, which is different from both Puppet Enterprise and open source Puppet. You can use the `--config` argument to specify a different configuration file. See the documentation on Hiera's [configuration file](configuring.html#location) for notes on where to find this file depending on your Puppet version and operating system, and consider either reconfiguring Puppet to use `/etc/hiera.yaml` (Puppet 3) or set a symlink to `/etc/hiera.yaml` (Puppet 2.7).
27+
28+
### Order of Arguments
29+
30+
Hiera is sensitive to the position of its command-line arguments:
31+
32+
- The first value is always the key to look up.
33+
- The first argument after the key that does not include an equals sign (`=`) becomes the default value, which Hiera will return if no key is found. Without a default value and in the absence of a matching key from the hierarchy, Hiera returns `nil`.
34+
- Remaining arguments should be `variable=value` pairs.
35+
- **Options** may be placed anywhere.
36+
37+
### Options
38+
39+
Hiera accepts the following command line options:
40+
41+
Argument | Use
42+
--------------------------------------|------------------------------------------------------------------
43+
`-V`, `--version` | Version information
44+
`-c`, `--config FILE` | Specify an alternate configuration file location
45+
`-d`, `--debug` | Show debugging information
46+
`-a`, `--array` | Return all values as a flattened array of unique values
47+
`-h`, `--hash` | Return all hash values as a merged hash
48+
`-j`, `--json FILE` | JSON file to load scope from
49+
`-y`, `--yaml FILE` | YAML file to load scope from
50+
`-m`, `--mcollective IDENTITY` | Use facts from a node (via mcollective) as scope
51+
`-i`, `--inventory_service IDENTITY` | Use facts from a node (via Puppet's inventory service) as scope
52+
53+
54+
55+
## Fact Sources
56+
57+
When used from Puppet, Hiera automatically receives all of the facts it needs. On the command line, you'll need to manually pass it those facts.
58+
59+
You'll typically run the Hiera command line tool on your puppet master node, where it will expect the facts to be either:
60+
61+
* Included on the command line as variables (e.g. `::operatingsystem=Debian`)
62+
* Given as a [YAML or JSON scope file](#json-and-yaml-scopes)
63+
* Retrieved on the fly from [MCollective](#mcollective) data
64+
* Looked up from [Puppet's inventory service](#inventory-service)
65+
66+
Descriptions of these choices are below.
67+
68+
### Command Line Variables
69+
70+
Hiera accepts facts from the command line in the form of `variable=value` pairs, e.g. `hiera ntp_server ::osfamily=Debian clientcert="web01.example.com"`. Variables on the command line must be specified in a way that matches how they appear in `hiera.yaml`, including the leading `::` for facts and other top-scope variables. Variable values must be strings and must be quoted if they contain spaces.
71+
72+
This is useful if the values you're testing only rely on a few facts. It can become unwieldy if your hierarchy is large or you need to test values for many nodes at once. In these cases, you should use one of the other options below.
73+
74+
### JSON and YAML Scopes
75+
76+
Rather than passing a list of variables to Hiera as command line arguments, you can use JSON and YAML files. You can construct these files yourself, or use a YAML file retrieved from Puppet's cache or generated with `facter --yaml`.
77+
78+
Given this command using command line variable assignments:
79+
80+
`$ hiera ntp_server osfamily=Debian timezone=CST`
81+
82+
>**Note:** For Puppet, [facts are top-scope variables](/puppet/latest/reference/lang_variables.html#facts-and-built-in-variables), so their [fully-qualified](/puppet/latest/reference/lang_scope.html#accessing-out-of-scope-variables) form is `$::fact_name`. When called from within Puppet, Hiera will correctly interpolate `%{::fact_name}`. However, Facter's command-line output doesn't follow this convention --- top-level facts are simply called `fact_name`. That means you'll run into trouble in this section if you have `%{::fact_name}` in your hierarchy.
83+
84+
The following YAML and JSON examples provide equivalent results:
85+
86+
#### Example YAML Scope
87+
88+
`$ hiera ntp_server -y facts.yaml`
89+
90+
~~~ yaml
91+
# facts.yaml
92+
---
93+
"::osfamily": Debian
94+
"::timezone": CST
95+
~~~
96+
97+
98+
99+
#### Example JSON Scope
100+
101+
`$ hiera ntp_server -j facts.json`
102+
103+
~~~ javascript
104+
// facts.json
105+
{
106+
"::osfamily" : "Debian",
107+
"::timezone" : "CST"
108+
}
109+
~~~
110+
111+
112+
113+
### MCollective
114+
115+
If you're using Hiera from a user account that is allowed to issue MCollective commands, you can ask any node running MCollective to send you its facts. Hiera will then use those facts to drive the lookup.
116+
117+
To do this, use the `-m` or `--mcollective` flag and give it the name of an MCollective node as an argument:
118+
119+
$ hiera ntp_server -m balancer01.example.com
120+
121+
Note that you must be running the Hiera command from a user account that is authorized and configured to send MCollective commands, and is also able to read the Hiera configuration and data files.
122+
123+
#### Puppet Enterprise Example
124+
125+
In Puppet Enterprise 2.x or 3.x, you can do Hiera lookups with MCollective by switching to the `peadmin` account on the puppet master server, which is authorized to issue orchestration commands.
126+
127+
# sudo -iu peadmin
128+
$ hiera ntp_server -m balancer01.example.com
129+
130+
Make sure that the `peadmin` user is allowed to read the Hiera config and data files.
131+
132+
### Inventory Service
133+
134+
If your puppet master is connected to a PuppetDB server (or has the older ActiveRecord inventory service enabled), you can get Hiera lookups using the actual facts reported by an actual puppet agent node. This goes through Puppet's [inventory service](/guides/inventory_service.html) API.
135+
136+
To do this, use the `-i` or `--inventory_service` flag and give it the name of a Puppet node as an argument:
137+
138+
$ hiera ntp_server -i balancer01.example.com
139+
140+
> #### Note: Known Bug With Puppet 3.x
141+
>
142+
> In Hiera 1.3 and earlier, inventory lookups will fail when Puppet 3.x is present. This is a bug in Hiera, which will be fixed in a future release.
143+
144+
145+
146+
#### Allowing Lookups on the Puppet Master
147+
148+
[authconf]: /guides/rest_auth_conf.html
149+
150+
Before you can do Hiera lookups via the inventory, you'll need to enable access in [the puppet master's `auth.conf` file.][authconf] You must ensure that the node you will be doing lookups from can call the `find` method on the `/facts` path. This will probably look something like this:
151+
152+
path /facts
153+
method find, search
154+
auth yes
155+
allow pe-internal-dashboard, puppet.example.com
156+
157+
When choosing the name and certificate to use when contacting the puppet master, Hiera uses the existing puppet.conf and agent certificate on the node. If you are running as root, it will impersonate the agent node you are running on; if you are running as another user, it will use configuration and credentials in `~/.puppet/` instead.
158+
159+
To run as a different user, you may need to request a separate certificate, since the master won't sign two certificates with the same certname. To do this:
160+
161+
1. Create a `~/.puppet/puppet.conf` file and set the `certname` setting to something unique.
162+
2. Run `puppet agent --test` to request a certificate.
163+
3. On the puppet master, sign the certificate.
164+
4. Run `puppet agent --test` again.
165+
166+
## Lookup Types
167+
168+
By default, the Hiera command line tool will use a [priority lookup][priority_lookup], which returns a single value --- the first value found in the hierarchy. There are two other lookup types available: array merge and hash merge.
169+
170+
### Array Merge
171+
172+
An array merge lookup assembles a value by merging every value it finds in the hierarchy into a flattened array of unique values. [See "Array Merge Lookup"][array_lookup] for more details.
173+
174+
Use the `--array` option to do an array merge lookup.
175+
176+
If any of the values found in the data sources are hashes, the `--array` option will cause Hiera to return an error.
177+
178+
### Hash
179+
180+
A hash merge lookup assembles a value by merging the top-level keys of each hash it finds in the hierarchy into a single hash. [See "Hash Merge Lookup"][hash_lookup] for more details.
181+
182+
Use the `--hash` option to do a hash merge lookup.
183+
184+
If any of the values found in the data sources are strings or arrays, the `--hash` option will cause Hiera to return an error.
185+

0 commit comments

Comments
 (0)