我尝试在 Puppet 上创建一个自定义资源类型,它会创建一个文件。但是,在我的模块/库文件夹并运行 puppet 代理,使用新的自定义资源,它显示了一个错误未知资源类型。不知道我做错了什么。
我的模块结构是这样的:
.
├── 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
我的类型文件(即 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
我的提供程序文件(即 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
我的 init.pp
class custom_resource {
create_file{"NewTestFile":
ensure=> present,
filepath => "C:/testfile.txt",
}
}
运行模块后我收到的错误消息:
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
答案1
lib 文件路径错误。将类型和提供程序目录放在模块/lib/puppet之后你的模块结构将如下所示
.
├── 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
我希望这能有所帮助