0

I have tried to create a custom resource type on Puppet which creates a file. However, after adding the type and providers in my module/lib folder and running puppet agent, using the new custom resource, it shows me an error with Unknown resource type. Not sure what I have done wrong.

My Module Structure is like this:

.
├── examples
│   └── init.pp
├── Gemfile
├── lib
│   ├── provider
│   │   ├── create_file
│   │   │   └── windows.rb
│   │   └── iis_powershell.rb
│   └── type
│       └── create_file.rb
├── manifests
│   └── init.pp
├── metadata.json
├── Rakefile
├── README.md
└── spec
    ├── classes
    │   └── init_spec.rb
    └── spec_helper.rb

My type file (i.e. create_file.rb)

Puppet::Type.newtype(:create_file) do
    @doc = "Creates New Files"
    ensurable
    newparam(:filepath) do
        desc "Fully qualified filepath"
        validate do |value|
            if value.nil? or value.empty?
                raise ArgumentError, "A non-empty filepath must be specified."
            end
            fail("File paths must be fully qualified, not '#{value}'") unless value =~ /^.:(\/|\\)/
        end
    end
end

My provider file (i.e. windows.rb)

Puppet::Type.type(:create_file).provide: windows do
    desc "Powershell Provider to create new files"
  confine :operatingsystem => :windows
  defaultfor :operatingsystem => :windows




    def filepath=(value)
        @property_flush[:filepath] = value
    end

    def create
        create_cmd = "New-Item -Path \"#{@resource[:filepath]}\" -ItemType File"
        result = self.class.run(create_cmd)
        fail "Error creating file: #{result[:errormessage]}" unless result[:exitcode] == 0
        @property_hash[:ensure] = :present
    end

    def destroy
        destroy_cmd = "Remove-Item -Path \"#{@resource[:filepath]}\" "
        result = self.class.run(destroy_cmd)
        fail "Error removing file: #{result[:errormessage]}" unless result[:exitcode] == 0
        @property_hash[:ensure] = :absent
    end

end

My init.pp

class custom_resource {
    create_file{"NewTestFile":
        ensure=> present,
        filepath => "C:/testfile.txt",
    }

}

The error message I got after running the module:

Could not retrieve catalog from remote server: Error 500 on SERVER:
Server Error: 
Evaluation Error: Error while evaluating a Resource Statement, 
Evaluation Error: Error while evaluating a Resource Statement, Unknown resource type: 'create_file' 
at /etc/puppetlabs/code/environments/dev/modules/custom_resource/manifests/init.pp:
46:2 on node 
0

The lib files are in wrong path. Put the type and provider directories under module/lib/puppet after which your module structure will be like the following

.
├── examples
│   └── init.pp
├── Gemfile
├── lib
│   └── puppet
│       ├── provider
│       │   ├── create_file
│       │   │   └── windows.rb
│       │   └── iis_powershell.rb
│       └── type
│           └── create_file.rb
├── manifests
│   └── init.pp
├── metadata.json
├── Rakefile
├── README.md
└── spec
    ├── classes
    │   └── init_spec.rb
    └── spec_helper.rb

I hope that will help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.