Foreman import anssible roles

Hello,
As I’m quiet noob in foreman world :slight_smile:

I have 2 servers ( foreman master + puppetmaster( also CA)
+20VMs registered to puppet server

foreman-m01.local-domain.cz (rhel7)
puppet-m01.local-domain.cz (rhel8)

on foreman server:

 foreman-installer 	\
	--puppet-server=false \
	--foreman-proxy-puppet=false \
	--foreman-proxy-puppetca=false \
	--foreman-proxy-oauth-consumer-key="Pickachu1" \
	--foreman-proxy-oauth-consumer-secret="Pickachu1"  \
	--foreman-admin-username=admin  \
	--foreman-admin-password=strongpassword

#smart-proxy(puppet master)

foreman-installer -v \
	--no-enable-foreman \
	--no-enable-foreman-cli \
	--no-enable-foreman-plugin-bootdisk \
	--no-enable-foreman-plugin-setup \
	--no-enable-puppet \
	--puppet-server-ca=false \
	--puppet-server-foreman-url=https://foreman-m01.local-domain.cz \
	--enable-foreman-proxy \
	--foreman-proxy-puppetca=false \
	--foreman-proxy-tftp=false \
	--foreman-proxy-foreman-base-url=https://foreman-m01.local-domain.cz   \
	--foreman-proxy-trusted-hosts=foreman-m01.local-domain.cz    \
	--foreman-proxy-oauth-consumer-key=LYQUUBBQhckXmGfPeNhgiTFqpB \
	--foreman-proxy-oauth-consumer-secret==hKBcCCxVnExN7QyMucbFAxH \
	--foreman-proxy-register-in-foreman=false

cat /etc/puppetlabs/puppet/foreman.yaml

:ssl_ca: "/etc/puppetlabs/puppet/ssl/certs/ca.pem"
:ssl_cert: "/etc/puppetlabs/puppet/ssl/certs/puppet-m01.local-domain.cz.pem"
:ssl_key": "/etc/puppetlabs/puppet/ssl/private_keys/puppet-m01.local-domain.cz.pem"

:url: "https://foreman-m01.local-domain.cz"
:puppetdir: "/opt/puppetlabs/server/data/puppetserver"

:facts: true
:timeout: 60
:report_timeout: 60

on foreman server

[root@foreman-m01 ~]# rpm -qa |grep -E "ansible|dynf"
tfm-rubygem-foreman_ansible_core-4.2.1-1.fm2_5.el7.noarch
tfm-rubygem-smart_proxy_dynflow_core-0.3.3-1.fm2_5.el7.noarch
tfm-rubygem-dynflow-1.4.9-1.fm2_5.el7.noarch
tfm-rubygem-foreman_ansible-6.3.4-1.fm2_5.el7.noarch

So far so good:

Hosts/puppet/ansible facts are visible in foreman, however I failed miserable to understand how to import ansible roles:

On foreman host I do have /etc/ansible/roles/
I’d like foreman host to act also as ansile tower/control node.

1 Like

to mention: foreman/foreman-proxy 2.5.4

Hi,
I see you have foreman_ansible plugin, check if you have smart_proxy_ansible on smart-proxy (can be added with --enable-foreman-proxy-plugin-ansible) - Smart proxy should have Ansible feature when you refresh features.

  1. install role on smart-proxy: ansible-galaxy install -p /etc/ansible/roles adriagalin.motd
  2. import from Foreman under Configure → Ansible Roles

1 Like

Hi Ondra,

So,

[root@puppet-m01 ~]# tree /etc/ansible/roles/
/etc/ansible/roles/
├── adriagalin.motd
│   ├── CHANGELOG
│   ├── defaults
│   │   └── main.yml
│   ├── files
│   │   └── 99-footer
│   ├── LICENSE
│   ├── meta
│   │   └── main.yml
│   ├── README.md
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   │   └── etc
│   │       ├── motd.j2
│   │       └── update-motd.d
│   │           └── 20-update.j2
│   └── tests
│       ├── inventory
│       └── test.yml
└── mysql_new
    ├── defaults
    │   └── xmain.yml
    ├── files
    ├── handlers
    │   └── main.yml
    ├── meta
    │   └── main.yml
    ├── README.md
    ├── tasks
    │   └── main.yml
    ├── templates
    │   └── my.cnf
    ├── tests
    │   ├── inventory
    │   └── test.yml
    └── vars
        └── main.yml

[root@puppet-m01 ~]# ls -lad /etc/ansible/roles/*
drwxr-xr-x 8 root root 200 Nov 10 18:38 /etc/ansible/roles/adriagalin.motd
drwxr-xr-x 10 foreman-proxy root 135 Nov 10 18:25 /etc/ansible/roles/mysql_new

now in foreman, i can see the roles, I have the option to import, but… I get internal server error during import

on foreman server

2021-11-10T18:48:14 [I|app|ab33feeb]   Parameters: {"changed"=>{"new"=>{"mysql_new"=>{"id"=>nil, "name"=>"mysql_new", "created_at"=>nil, "updated_at"=>nil}, "adriagalin.motd"=>{"id"=>nil, "name"=>"adriagalin.motd", "created_at"=>nil, "updated_at"=>nil}}}, "proxy"=>1, "ansible_role"=>{}}
2021-11-10T18:48:14 [I|app|ab33feeb] Enqueued SyncRolesAndVariables (Job ID: 3d864926-bca7-4fb8-8944-5efaf635a85f) to Dynflow(default) with arguments: {"new"=>{"mysql_new"=>{"id"=>nil, "name"=>"mysql_new", "created_at"=>nil, "updated_at"=>nil}, "adriagalin.motd"=>{"id"=>nil, "name"=>"adriagalin.motd", "created_at"=>nil, "updated_at"=>nil}}}, #<GlobalID:0x0000000011828320 @uri=#<URI::GID gid://foreman/SmartProxy/1>>
2021-11-10T18:48:14 [W|app|ab33feeb] The Dynflow world was not initialized yet. If your plugin uses it, make sure to call Rails.application.dynflow.require! in some initializer
2021-11-10T18:48:14 [I|app|ab33feeb] Backtrace for 'The Dynflow world was not initialized yet. If your plugin uses it, make sure to call Rails.application.dynflow.require! in some initializer' error (RuntimeError): The Dynflow world was not initialized yet. If your plugin uses it, make sure to call Rails.application.dynflow.require! in some initializer
/opt/theforeman/tfm/root/usr/share/gems/gems/dynflow-1.4.9/lib/dynflow/rails.rb:75:in `world'
.....
/usr/share/passenger/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
/usr/share/passenger/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
/usr/share/passenger/phusion_passenger/request_handler.rb:455:in `block (3 levels) in start_threads'
/opt/theforeman/tfm/root/usr/share/gems/gems/logging-2.3.0/lib/logging/diagnostic_context.rb:474:in `block in create_with_logging_context'
2021-11-10T18:48:14 [I|app|ab33feeb]   Rendering common/500.html.erb
2021-11-10T18:48:14 [I|app|ab33feeb]   Rendered common/500.html.erb (Duration: 9.4ms | Allocations: 5915)
2021-11-10T18:48:14 [I|app|ab33feeb] Completed 500 Internal Server Error in 28ms (Views: 10.2ms | ActiveRecord: 2.7ms | Allocations: 10354)

Hm, I have no idea why Dynflow was not initialized, it should have been at this point…

A workaround is to use hammer commands (from hammer_cli_foreman_ansible):

hammer ansible roles import --proxy-id $my_proxy_id
hammer ansible variables import --proxy-id $my_proxy_id

Any idea how I can force a dynflow initialization ( some component/package) that need to be reinstalled on foreman server or puppet (smart-proxy) server ?

2nd:

[root@foreman-m01 ~]# rpm -qa |grep hammer_cli
tfm-rubygem-hammer_cli_foreman-2.5.1-1.el7.noarch
tfm-rubygem-hammer_cli-2.5.1-1.el7.noarch
tfm-rubygem-hammer_cli_foreman_ansible-0.3.4-1.fm2_5.el7.noarch
root@foreman-m01 ~]# hammer  ansible roles fetch
Could not fetch roles:
  Error: undefined method `action' for nil:NilClass

[root@foreman-m01 ~]# hammer  ansible roles import --proxy-id aaaa
Could not import roles:
  Error: Unrecognised option '--proxy-id'.
[root@foreman-m01 ~]# hammer -d  ansible roles fetch
[ INFO 2021-11-11T14:34:07 Init] Initialization of Hammer CLI (2.5.1) has started...
[DEBUG 2021-11-11T14:34:07 Init] Running at ruby 2.7.3-p183
[ INFO 2021-11-11T14:34:07 Init] Configuration from the file /etc/hammer/cli_config.yml has been loaded
[ INFO 2021-11-11T14:34:07 Init] Configuration from the file /etc/hammer/cli.modules.d/foreman.yml has been loaded
[ INFO 2021-11-11T14:34:07 Init] Configuration from the file /etc/hammer/cli.modules.d/foreman_ansible.yml has been loaded
[ INFO 2021-11-11T14:34:07 Init] Configuration from the file /root/.hammer/cli.modules.d/foreman.yml has been loaded
[DEBUG 2021-11-11T14:34:08 SSLoptions] SSL options: {
    :ssl_ca_file => "/etc/puppetlabs/puppet/ssl/certs/ca.pem",
     :verify_ssl => true
}
....
:Hostgroup::UpdateCommand:
        #<Proc:0x0000000001f59fc8 /opt/theforeman/tfm/root/usr/share/gems/gems/hammer_cli_foreman-2.5.1/lib/hammer_cli_foreman/command_extensions/puppet_environment.rb:11>
[ INFO 2021-11-11T14:34:08 Extensions] Applied HammerCLIForeman::CommandExtensions::PuppetEnvironment for *all* except *nothing* on HammerCLIForeman::Hostgroup::UpdateCommand.
[ WARN 2021-11-11T14:34:08 HammerCLIForemanAnsible::AnsibleRolesCommand] Resource 'ansible_roles' does not exist in the API
[ INFO 2021-11-11T14:34:08 Modules] Extension module hammer_cli_foreman_ansible (0.3.4) loaded.
[DEBUG 2021-11-11T14:34:08 Init] Using locale 'en'
[DEBUG 2021-11-11T14:34:08 Init] 'mo' files for locale domain 'hammer-cli' loaded from '/opt/theforeman/tfm/root/usr/share/gems/gems/hammer_cli-2.5.1/locale'
[DEBUG 2021-11-11T14:34:08 Init] 'mo' files for locale domain 'hammer-cli-foreman' loaded from '/opt/theforeman/tfm/root/usr/share/gems/gems/hammer_cli_foreman-2.5.1/locale'
[ INFO 2021-11-11T14:34:08 HammerCLI::MainCommand] Called with options: {"option_debug"=>true}
[ INFO 2021-11-11T14:34:08 HammerCLIForemanAnsible::AnsibleCommand] Called with options: {}
[DEBUG 2021-11-11T14:34:08 HammerCLIForeman::CommandExtensions::OptionSources] Called block for HammerCLIForeman::Command option sources:
        #<Proc:0x000000000185ffb0 /opt/theforeman/tfm/root/usr/share/gems/gems/hammer_cli_foreman-2.5.1/lib/hammer_cli_foreman/command_extensions/option_sources.rb:6>
[ INFO 2021-11-11T14:34:08 HammerCLIForemanAnsible::AnsibleRolesCommand] Called with options: {}
[DEBUG 2021-11-11T14:34:08 HammerCLIForeman::CommandExtensions::OptionSources] Called block for HammerCLIForeman::Command option sources:
        #<Proc:0x000000000185ffb0 /opt/theforeman/tfm/root/usr/share/gems/gems/hammer_cli_foreman-2.5.1/lib/hammer_cli_foreman/command_extensions/option_sources.rb:6>
[DEBUG 2021-11-11T14:34:08 Exception] Using exception handler HammerCLIForeman::ExceptionHandler#handle_general_exception
[ERROR 2021-11-11T14:34:08 Exception] Error: undefined method `action' for nil:NilClass
Could not fetch roles:
  Error: undefined method `action' for nil:NilClass
....

@fritz0011, the hammer_cli_foreman_ansible requires foreman_ansible to be installed to work. If you already have foreman_ansible installed prior to hammer_cli_foreman_ansible you might need to run the following commands:
$ foreman-rake apipie:cache (this will update the cache for API documentation, if you’re using English version only, you can speed the generation by using FOREMAN_APIPIE_LANGS=en env variable.
$ hammer --reload-cache (this will reload the cache from the server to update the commands)

@aruzicka have you seen the Dynflow error before?

Super thanks for @Ondrej_Prazak and @ofedoren .

Command line saved my day !! :slight_smile:
However, still trying to understand why I get internal server error while trying to import through GUI !! (foreman ruining under passenger)

Back to square 1…

Schedule run ansible role…

Why are you running Foreman under passenger? As far as I know that is unsupported[1, 2] and most likely is the reason why you’re hitting that error. We already dropped some workarounds[3] which were need for Dynflow to work under passenger

[1] - Foreman :: Manual
[2] - Drop Passenger support
[3] - https://github.com/theforeman/foreman/commit/66e39019953c9f993d6951cca0d0c0567050a3c1

Hi @aruzicka

I’ve upgraded directly from 1.2xx to 2.5.4
So… I’m in this situation,
" After defaulting to Puma in Foreman 2.1 and deprecation in Foreman 2.4, support for mod_passenger is dropped. The packages have been dropped and the installer can no longer configure it. Users who upgrade using the installer will be migrated automatically but users who upgrade without the installer must take care of this themselves."

In this case what are the necessary steps to install/configure puma ?

OK, figured out.

Just took a postgres dump, wiped out all rpms and did a clean reinstall.
Now everything is working well. :slight_smile:

Now, question for RPM packagers, should modify the rpm script in case of upgrade from 1.xx to 2.XX to remove passenger packages and reconfigure puma.

Oof, that might have actually broken your installation on quite a few places. Normally you should upgrade to all versions in between first too.

If you don’t you might end up with weird stuff, like you’ve seen.

I would ‘just’ reinstall if you need to upgrade more then 2 versions. I think it’s less work (especially if you automate your Foreman configuration in Ansible too)