Re: [foreman-users] Re: Foreman Template writing - Iterate over Facts Hash/Structured Facts
Thanks Daniel. Please see my comments as below. On Wed, Aug 30, 2017 at 10:38 AM, Daniel Lobato Garcia wrote: > On 08/29, lohit.vall...@gmail.com wrote: > > Does foreman store facts as hash? > > No, but they are presented as a hash when calling `@host.facts`. I saw > it poking around with `foreman-rake console`: > > irb(main):003:0> Host.first.facts.class > => Hash > Yes, i do understand that @host.facts is hash, however i would need the facts mentioned in @host.facts to be in hash format. That is they have multiple levels, and i see that foreman breaks it down with '::' in between. > > > Because when i queried the node using api to gather facts, i see that the > > facts hash that i uploaded is now null: > > Can you share the facts hash that you have uploaded? If it had multiple > levels, they are stored as strings like you posted. This is the simplest of hashes that i have. {"epel"=>{"baseurl"=>"http://10.230.1.106:8090/epel/7/x86_64/"}, "gpfs4"=>{"baseurl"=>"http://10.230.1.106:8090/gpfs/4.2.3.2"}, "base"=>{"baseurl"=>"http://10.230.1.106:8090/centos/7/os/x86_64/"}, "puppetlabs"=>{"baseurl"=>"http://10.230.1.106:8090/puppetlabs/7/x86_64/"}, "updates"=>{"baseurl"=>"http://10.230.1.106:8090/centos/7/updates/x86_64/"}} I see that foreman breaks up the above hash into multiple facts, like kickstart_repos::epel::baseurl. I do not want to be mentioning kickstart::repos::epel::baseurl as a search condition in the template. I would instead want to iterate over the above hash, and get whatever exists in it, since the hash can change its keys names and values with respect to different hosts. > > If you want to iterate through the actual FactValue object, it might > make it simple for you: > > fact_values = FactValue.search_for("host = #{Host.first.fqdn}") > > Or if you want to get ONLY the values with an actual value (so the ones > that are 'leaves' of the tree): > > FactValue.search_for("host = #{Host.first.fqdn}").where.not(:value => > nil) > > I am trying to understand the above syntax. When i do search_for, i still have to mention the key?. How do i search for the above hash which is broken down by foreman into multiple facts? When i do an api query for facts : i get "kickstart_repos": null While instead what i expect is : "kickstart_repos": {"epel"=>{"baseurl"=>" http://10.230.1.106:8090/epel/7/x86_64/"}, "gpfs4"=>{"baseurl"=>" http://10.230.1.106:8090/gpfs/4.2.3.2"}, "base"=>{"baseurl"=>" http://10.230.1.106:8090/centos/7/os/x86_64/"}, "puppetlabs"=>{"baseurl"=>" http://10.230.1.106:8090/puppetlabs/7/x86_64/"}, "updates"=>{"baseurl"=>" http://10.230.1.106:8090/centos/7/updates/x86_64/"}} So that i can iterate in erb, with .each on "kickstart_repos" fact as a hash, and print each url as below, in kickstart template ### Kickstart repos ### repo --name epel --baseurl http://x/epel/7/x86_64/ --install repo --name gpfs4 --baseurl http:///gpfs/4.2.3.2 --install repo --name base --baseurl http://x/centos/7/os/x86_64/ --install repo --name puppetlabs --baseurl http://xx/puppetlabs/7/x86_64/ --install repo --name updates --baseurl http:///centos/7/updates/x86_64/ --install The above could change for different hosts, Some hosts might have "puppetlabs repo" but some might not. > > > "kickstart_repos": null, > > "kickstart_repos::base": null, > > > > I see that it has created the child variables out of the hash. > > > > "kickstart_repos::base::baseurl": > > "http://10.230.1.106:8090/centos/7/os/x86_64/";, > > > > This makes the template writing really difficult, because i will have to > > statically mention each key of the facts hash that i have uploaded, in > the > > templates. > > > > One of the advantages of hash is that it could be dynamic, and we should > be > > able to iterate over whichever keys it contains. > > > > How do i make foreman store the facts as a hash and not function as > above? > > > > > > On Tuesday, August 29, 2017 at 4:31:25 PM UTC-4, lohit@gmail.com > wrote: > > > > > > I am trying to find a way to iterate over facts hash with 'each' > function. > > > But i get this error: > > > > > > ' There was an error rendering the CentOS_test1 template: undefined > method > > > `each' for nil:NilClass' > > > > > > Safe mode rendering is off. > > > > > > How do i iterate over nested structured facts/facts hash in provision > > > templates in foreman? > > > > > > For example - the following does not work. > > > > > > <% @host.facts_hash['kickstart_repos::base'].each do |k| %> > > > <%= k %>: <%= @host.facts_hash[k] %> > > > <% end %> > > > > > > Is there a restriction on what ruby methods i can use inside templates? > > > The same template works fine in puppet/with ruby. > > > > > > I tried searching for examples but could not find any. > > > The documentation mentions that : > > > > > > @host.facts: Contains a hash of facts from Facter/Ohai etc. (1.7.2+) > > > @host.facts_hash : Contains a hash of facts from Facter/Ohai etc. (only > > >
[foreman-users] Re: Turning off/Handling Structured Facts in New Releases of Foreman?
Hello, May i know - if you have a found a solution to this? I have faced a similar issue this week. I have seen that foreman breaks down the hash/structured facts and makes it difficult for me to iterate over the hash. I cannot use the broken up facts, since they are static and my template will become less dynamic with me mentioning each broken up fact. I didnt know, foreman used to support storing nested hash previously? Could we iterate them as hash in templates? I would like to request this feature too, and hope it will be available soon. It was a big surprise for me, that it does not support storing facts as hash - since i believe it does have a way to store smart variables as hash. I used puppet razor before, and i know that it supports storing facts as hash and query them as hash in templates. I have been looking to migrate to foreman from puppet razor, and this is a major roadblock. On Friday, April 28, 2017 at 3:15:17 AM UTC-4, Iain Walmsley wrote: > > Hi there, > > Messaging about the latest release of Foreman (1.16). This may also be > pertinent to older versions as well however. (Can't pin it down easily in > the release notes). > > I'm currently using Foreman 1.11 and one thing I have is a custom plugin > to interrogate certain custom facts I've written and display them. This > collects application specific data by querying custom facts (that are > structured and contain quite a bit of data) using scoped_search. The issue > I'm getting while testing in 1.16, where structured facts are now broken > down by the new fact importer are; > > >- It doesn't seem possible to access an entire structured custom fact? >As far as I can see while being imported it's all broken down and stored >directly. An example would be the following below which previously I used >to pull down from the database directly and parse, in addition as the key >names "info1" etc are dynamic can't hardcode to pull out specific values. > > > {"info1":{"status":"X","time":"Y"},"info2":{"status":"X","time":"Z"},"info3":{"status":"X","time":"Y"},.} > > > >- The custom facts we have mentioned above can be quite verbose - >meaning that what was previous 3 custom facts is translated to upwards of >35-40, and it's not a great user experience on the UI to view data >together, in many ways it was just easier to look at the json. > > I can understand why this change was made for, but was wondering if there > was any way to turn structured fact importing like this off easily? Or if > not if anyone had any ideas how to replicate the previous functionality of > just being able to pull down an entire fact? > -- You received this message because you are subscribed to the Google Groups "Foreman users" group. To unsubscribe from this group and stop receiving emails from it, send an email to foreman-users+unsubscr...@googlegroups.com. To post to this group, send email to foreman-users@googlegroups.com. Visit this group at https://groups.google.com/group/foreman-users. For more options, visit https://groups.google.com/d/optout.
[foreman-users] Re: Foreman Template writing - Iterate over Facts Hash/Structured Facts
Does foreman store facts as hash? Because when i queried the node using api to gather facts, i see that the facts hash that i uploaded is now null: "kickstart_repos": null, "kickstart_repos::base": null, I see that it has created the child variables out of the hash. "kickstart_repos::base::baseurl": "http://10.230.1.106:8090/centos/7/os/x86_64/";, This makes the template writing really difficult, because i will have to statically mention each key of the facts hash that i have uploaded, in the templates. One of the advantages of hash is that it could be dynamic, and we should be able to iterate over whichever keys it contains. How do i make foreman store the facts as a hash and not function as above? On Tuesday, August 29, 2017 at 4:31:25 PM UTC-4, lohit@gmail.com wrote: > > I am trying to find a way to iterate over facts hash with 'each' function. > But i get this error: > > ' There was an error rendering the CentOS_test1 template: undefined method > `each' for nil:NilClass' > > Safe mode rendering is off. > > How do i iterate over nested structured facts/facts hash in provision > templates in foreman? > > For example - the following does not work. > > <% @host.facts_hash['kickstart_repos::base'].each do |k| %> > <%= k %>: <%= @host.facts_hash[k] %> > <% end %> > > Is there a restriction on what ruby methods i can use inside templates? > The same template works fine in puppet/with ruby. > > I tried searching for examples but could not find any. > The documentation mentions that : > > @host.facts: Contains a hash of facts from Facter/Ohai etc. (1.7.2+) > @host.facts_hash : Contains a hash of facts from Facter/Ohai etc. (only > works if safemode_render=false) > > I assume the above means, that both those variables are hashes and can be > iterated? > > -- You received this message because you are subscribed to the Google Groups "Foreman users" group. To unsubscribe from this group and stop receiving emails from it, send an email to foreman-users+unsubscr...@googlegroups.com. To post to this group, send email to foreman-users@googlegroups.com. Visit this group at https://groups.google.com/group/foreman-users. For more options, visit https://groups.google.com/d/optout.
[foreman-users] Foreman Template writing - Iterate over Facts Hash/Structured Facts
I am trying to find a way to iterate over facts hash with 'each' function. But i get this error: ' There was an error rendering the CentOS_test1 template: undefined method `each' for nil:NilClass' Safe mode rendering is off. How do i iterate over nested structured facts/facts hash in provision templates in foreman? For example - the following does not work. <% @host.facts_hash['kickstart_repos::base'].each do |k| %> <%= k %>: <%= @host.facts_hash[k] %> <% end %> Is there a restriction on what ruby methods i can use inside templates? The same template works fine in puppet/with ruby. I tried searching for examples but could not find any. The documentation mentions that : @host.facts: Contains a hash of facts from Facter/Ohai etc. (1.7.2+) @host.facts_hash : Contains a hash of facts from Facter/Ohai etc. (only works if safemode_render=false) I assume the above means, that both those variables are hashes and can be iterated? -- You received this message because you are subscribed to the Google Groups "Foreman users" group. To unsubscribe from this group and stop receiving emails from it, send an email to foreman-users+unsubscr...@googlegroups.com. To post to this group, send email to foreman-users@googlegroups.com. Visit this group at https://groups.google.com/group/foreman-users. For more options, visit https://groups.google.com/d/optout.
Re: [foreman-users] Foreman discovery, use hooks and api to upload custom facts from CMDB.
I figured out this api, and its mandatory facts: /api/v2/discovered_hosts/facts I assumed, that it is only for creating new hosts. I now understand that it is also to update or add custom facts if needed. I think this should answer my question. I will come back with a question, if i still face issues. On Mon, Aug 28, 2017 at 11:04 PM, wrote: > This is the workflow that i am trying with hooks and foreman discovery > > VM/baremetal gets discovered -> after_create hook gets triggered -> Hook > use the discovery api to get more facts from discovered host -> facts are > used to gather more custom Facts from CMDB and they get uploaded to foreman. > > It is important that i upload these facts from CMDB before provisioning > just after a node is discovered, since i use these facts in kickstart > template. > > May i know, if the above is possible, using the below api? > > PUT /api/v2/discovered_hosts/:id > > I have tried to make it work, but it complains about architecture about > string. I am not sure, if the above api allows more custom facts to be > uploaded and would like to confirm the same. > > What are the mandatory facts needed for the above api to work? > > I would really appreciate if anyone could help me with above. > > Thanks, > Lohit > > -- > You received this message because you are subscribed to a topic in the > Google Groups "Foreman users" group. > To unsubscribe from this topic, visit https://groups.google.com/d/ > topic/foreman-users/zG4hKvFBZD8/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > foreman-users+unsubscr...@googlegroups.com. > To post to this group, send email to foreman-users@googlegroups.com. > Visit this group at https://groups.google.com/group/foreman-users. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Foreman users" group. To unsubscribe from this group and stop receiving emails from it, send an email to foreman-users+unsubscr...@googlegroups.com. To post to this group, send email to foreman-users@googlegroups.com. Visit this group at https://groups.google.com/group/foreman-users. For more options, visit https://groups.google.com/d/optout.
[foreman-users] Foreman discovery, use hooks and api to upload custom facts from CMDB.
This is the workflow that i am trying with hooks and foreman discovery VM/baremetal gets discovered -> after_create hook gets triggered -> Hook use the discovery api to get more facts from discovered host -> facts are used to gather more custom Facts from CMDB and they get uploaded to foreman. It is important that i upload these facts from CMDB before provisioning just after a node is discovered, since i use these facts in kickstart template. May i know, if the above is possible, using the below api? PUT /api/v2/discovered_hosts/:id I have tried to make it work, but it complains about architecture about string. I am not sure, if the above api allows more custom facts to be uploaded and would like to confirm the same. What are the mandatory facts needed for the above api to work? I would really appreciate if anyone could help me with above. Thanks, Lohit -- You received this message because you are subscribed to the Google Groups "Foreman users" group. To unsubscribe from this group and stop receiving emails from it, send an email to foreman-users+unsubscr...@googlegroups.com. To post to this group, send email to foreman-users@googlegroups.com. Visit this group at https://groups.google.com/group/foreman-users. For more options, visit https://groups.google.com/d/optout.
Re: [foreman-users] No facts in Json input for hooks after foreman discovery
Thank you for letting me know, that its the expected behavior. I will use the api instead to get the facts. On Tuesday, August 22, 2017 at 8:28:09 AM UTC-4, Lukas Zapletal wrote: > > Hello, > > facts are not reported via hooks. > > LZ > > On Mon, Aug 21, 2017 at 4:37 PM, > > wrote: > > Hello, > > > > The issue is that -> i dont see facts in input json after foreman > discovers > > a VM/baremetal. > > > > This is the workflow that i am trying with hooks and foreman discovery > > > > VM/baremetal gets discovered -> JSON input to hooks -> Hooks use the > facts > > from JSON input to add more facts from CMDB -> Facts get uploaded to > > foreman. > > > > I have created the following hooks: > > > > > /usr/share/foreman/config/hooks/host/discovered/after_create/10-logger.py > > > > ls /usr/share/foreman-community/hooks/ > > functions.py functions.pyc __init__.py > > > > The scripts just get the input json and try to output the json to temp > > directory, to help me understand the structure. > > However - I dont see facts or any other useful information during input > > json. > > > > { > > "id": 22, > > "name": "mac00163e5426c9", > > "last_compile": null, > > "last_report": null, > > "updated_at": "2017-08-18T20:13:44.058Z", > > "created_at": "2017-08-18T20:13:44.058Z", > > "root_pass": null, > > "architecture_id": null, > > "operatingsystem_id": null, > > "environment_id": null, > > "ptable_id": null, > > "medium_id": null, > > "build": false, > > "comment": null, > > "disk": null, > > "installed_at": null, > > "model_id": null, > > "hostgroup_id": null, > > "owner_id": null, > > "owner_type": null, > > "enabled": true, > > "puppet_ca_proxy_id": null, > > "managed": false, > > "use_image": null, > > "image_file": null, > > "uuid": null, > > "compute_resource_id": null, > > "puppet_proxy_id": null, > > "certname": null, > > "image_id": null, > > "organization_id": null, > > "location_id": null, > > "otp": null, > > "realm_id": null, > > "compute_profile_id": null, > > "provision_method": null, > > "grub_pass": "", > > "global_status": 0, > > "lookup_value_matcher": null, > > "pxe_loader": null, > > "discovery_rule_id": null > > } > > > > > > The workaround that i will have to use to get facts is: > > 1. Extract the mac address from the name -> reformat it -> use that to > query > > the id of the host with discovery api > > 2. Use the id to query the facts of that host. > > > > Do i have to follow the above workaround to get facts of the discovered > > system? or am i missing something? > > It would be so much easier, if i could just facts in the input json to > the > > hook. > > > > I am using the following versions foreman and its plugins on CentOS7: > > > > tfm-rubygem-foreman_setup-5.0.0-1.fm1_13.el7.noarch > > foreman-release-1.15.3-1.el7.noarch > > foreman-installer-1.15.3-1.el7.noarch > > foreman-libvirt-1.15.3-1.el7.noarch > > foreman-postgresql-1.15.3-1.el7.noarch > > tfm-rubygem-foreman_hooks-0.3.14-1.fm1_15.el7.noarch > > foreman-selinux-1.15.3-1.el7.noarch > > foreman-debug-1.15.3-1.el7.noarch > > foreman-release-scl-3-1.el7.noarch > > tfm-rubygem-hammer_cli_foreman-0.10.2-1.el7.noarch > > tfm-rubygem-foreman_discovery-9.1.1-1.fm1_15.el7.noarch > > foreman-cli-1.15.3-1.el7.noarch > > tfm-rubygem-foreman_memcache-0.0.6-1.fm1_15.el7.noarch > > foreman-proxy-1.15.3-1.el7.noarch > > foreman-1.15.3-1.el7.noarch > > > > Thanks, > > Lohit > > > > > > -- > > You received this message because you are subscribed to the Google > Groups > > "Foreman users" group. > > To unsubscribe from this group and stop receiving emails from it, send > an > > email to foreman-user...@googlegroups.com . > > To post to this group, send email to forema...@googlegroups.com > . > > Visit this group at https://groups.google.com/group/foreman-users. > > For more options, visit https://groups.google.com/d/optout. > > > > -- > Later, > Lukas @lzap Zapletal > -- You received this message because you are subscribed to the Google Groups "Foreman users" group. To unsubscribe from this group and stop receiving emails from it, send an email to foreman-users+unsubscr...@googlegroups.com. To post to this group, send email to foreman-users@googlegroups.com. Visit this group at https://groups.google.com/group/foreman-users. For more options, visit https://groups.google.com/d/optout.
[foreman-users] No facts in Json input for hooks after foreman discovery
Hello, The issue is that -> i dont see facts in input json after foreman discovers a VM/baremetal. This is the workflow that i am trying with hooks and foreman discovery VM/baremetal gets discovered -> JSON input to hooks -> Hooks use the facts from JSON input to add more facts from CMDB -> Facts get uploaded to foreman. I have created the following hooks: /usr/share/foreman/config/hooks/host/discovered/after_create/10-logger.py ls /usr/share/foreman-community/hooks/ functions.py functions.pyc __init__.py The scripts just get the input json and try to output the json to temp directory, to help me understand the structure. However - I dont see facts or any other useful information during input json. { "id": 22, "name": "mac00163e5426c9", "last_compile": null, "last_report": null, "updated_at": "2017-08-18T20:13:44.058Z", "created_at": "2017-08-18T20:13:44.058Z", "root_pass": null, "architecture_id": null, "operatingsystem_id": null, "environment_id": null, "ptable_id": null, "medium_id": null, "build": false, "comment": null, "disk": null, "installed_at": null, "model_id": null, "hostgroup_id": null, "owner_id": null, "owner_type": null, "enabled": true, "puppet_ca_proxy_id": null, "managed": false, "use_image": null, "image_file": null, "uuid": null, "compute_resource_id": null, "puppet_proxy_id": null, "certname": null, "image_id": null, "organization_id": null, "location_id": null, "otp": null, "realm_id": null, "compute_profile_id": null, "provision_method": null, "grub_pass": "", "global_status": 0, "lookup_value_matcher": null, "pxe_loader": null, "discovery_rule_id": null } The workaround that i will have to use to get facts is: 1. Extract the mac address from the name -> reformat it -> use that to query the id of the host with discovery api 2. Use the id to query the facts of that host. Do i have to follow the above workaround to get facts of the discovered system? or am i missing something? It would be so much easier, if i could just facts in the input json to the hook. I am using the following versions foreman and its plugins on CentOS7: tfm-rubygem-foreman_setup-5.0.0-1.fm1_13.el7.noarch foreman-release-1.15.3-1.el7.noarch foreman-installer-1.15.3-1.el7.noarch foreman-libvirt-1.15.3-1.el7.noarch foreman-postgresql-1.15.3-1.el7.noarch tfm-rubygem-foreman_hooks-0.3.14-1.fm1_15.el7.noarch foreman-selinux-1.15.3-1.el7.noarch foreman-debug-1.15.3-1.el7.noarch foreman-release-scl-3-1.el7.noarch tfm-rubygem-hammer_cli_foreman-0.10.2-1.el7.noarch tfm-rubygem-foreman_discovery-9.1.1-1.fm1_15.el7.noarch foreman-cli-1.15.3-1.el7.noarch tfm-rubygem-foreman_memcache-0.0.6-1.fm1_15.el7.noarch foreman-proxy-1.15.3-1.el7.noarch foreman-1.15.3-1.el7.noarch Thanks, Lohit -- You received this message because you are subscribed to the Google Groups "Foreman users" group. To unsubscribe from this group and stop receiving emails from it, send an email to foreman-users+unsubscr...@googlegroups.com. To post to this group, send email to foreman-users@googlegroups.com. Visit this group at https://groups.google.com/group/foreman-users. For more options, visit https://groups.google.com/d/optout.