简体   繁体   中英

Puppet Facts to retrieve instanceProfileArn

I am working on a script that would require passing the Instance profile arn. I have been using puppet to retrieve some information using its facter capability. Below is an (snippet)example of a facter output found online, the full output can be found here ( https://gist.github.com/cliff-wakefield/b232ef51799908a0264eb7e95af09092 ). What I'd like to obtain is the "InstanceProfileArn"

ec2_metadata => {
  ami-id => "ami-34281c57",
  ami-launch-index => "0",
  ami-manifest-path => "(unknown)",
  block-device-mapping => {
    ami => "/dev/sda1",
    root => "/dev/sda1"
  },
  hostname => "ip-10-180-0-40.ap-southeast-2.compute.internal",
  iam => {
    info => "{
  "Code" : "Success",
  "LastUpdated" : "2016-08-28T23:12:36Z",
  "InstanceProfileArn" : "arn:aws:iam::750105279227:instance-profile/AnexPrereqs-AnexIAMInstanceProfile-11O8QJAS4XO7S",
  "InstanceProfileId" : "AIPAI6YKKPRVVX2XD6LCK"
}"

By running facter ec2_metadata.iam.info , I get:

{
      "Code" : "Success",
      "LastUpdated" : "2016-08-28T23:12:36Z",
      "InstanceProfileArn" : "arn:aws:iam::750105279227:instance-profile/AnexPrereqs-AnexIAMInstanceProfile-11O8QJAS4XO7S",
      "InstanceProfileId" : "AIPAI6YKKPRVVX2XD6LCK"
    }

However, I am struggling to get the "InstanceProfileArn" printed on the console.

So, two things I want to be able to achieve:

  • By running facter ec2_metadata.iam.info.<InstanceProfileArn> from within my instance, I want to be able to see instance profile arn printed in the console.
  • Secondly, I understand that the way the above command is passed in puppet will be slightly different and would look something like $facts[ec2_metadata][iam][info][InstanceProfileArn] . What would be the correct syntax to then be passed into puppet manifest?

There is a function called parsejson in the Puppet Forge stdlib module . It can be used to parse a string containing JSON into a Puppet hash. An example using your data:

$ cat Puppetfile
forge "https://forgeapi.puppetlabs.com"
  mod "puppetlabs-stdlib", "4.25.1"
$ r10k puppetfile install
$ cat foo.pp
include stdlib

# should be $info_json = $facts[ec2_metadata][iam][info], but for this example
# we'll use a literal...
$info_json = @(INFO)
{
      "Code" : "Success",
      "LastUpdated" : "2016-08-28T23:12:36Z",
      "InstanceProfileArn" : "arn:aws:iam::750105279227:instance-profile/AnexPrereqs-AnexIAMInstanceProfile-11O8QJAS4XO7S",
      "InstanceProfileId" : "AIPAI6YKKPRVVX2XD6LCK"
}
INFO
$info = parsejson($info_json)
$instance_profile_arn = $info['InstanceProfileArn']
notice($instance_profile_arn)
$ puppet apply --modulepath=modules foo.pp
Notice: Scope(Class[main]): arn:aws:iam::750105279227:instance-profile/AnexPrereqs-AnexIAMInstanceProfile-11O8QJAS4XO7S
[...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM