Getting host lists from Foreman via Ansible

Problem:

I would like to obtain host lists from Foreman to use this inventory in Ansible.
I found this collection:
https://docs.ansible.com/ansible/latest/collections/theforeman/foreman/foreman_inventory.html

For me it sounds like it should be possible. Ansible is not used yet anywhere. The nodes in Foreman report via Puppet. But this part should not be changed now.

Expected outcome:

I would like to get some examples of Ansible playbooks using this inventory collection, since I didn’t find anything in the docs.

Foreman and Proxy versions:
3.2.1
Foreman and Proxy plugin versions:
3.2.1
Distribution and version:
Ubuntu 20.04 LTS (server)

There is a tutorial Set up Foreman and manage it with Ansible

Thanks! @gvde I was able to run the example playbook.

But there it is about creating organizations in Foreman. I would like to fetch all host data (IP, name, etc) from an running Foreman instance to use them somewhere else.

It looks like there is no role for this, right? https://github.com/theforeman/foreman-ansible-modules/tree/develop/roles

Greets

Check the modules

https://theforeman.org/plugins/foreman-ansible-modules/

I thought I did :thinking: Maybe I am too stupid or it could be improved a bit.

After searching more, I found finally the plugin index.


https://theforeman.org/plugins/foreman-ansible-modules/

This was not so obvious to find I think. Starting here Foreman :: Plugin documentation index
I was expecting to get a hint here:

But this tells about README.md files, so I went to GH: GitHub - theforeman/foreman-ansible-modules: Ansible modules for interacting with the Foreman API and various plugin APIs such as Katello
But it is telling me about the roles here. And not about https://github.com/theforeman/foreman-ansible-modules/tree/develop/plugins/modules

Sometimes it felt like l am lost in a loop. Ansible Galaxy is pointing me to the same information on GH which also points to the Foreman docs with the same (or nearly the same content).

But this is another topic. Maybe some one is interested in this user experience and can improve it.

I finally found the theforeman.foreman.host_info module – Fetch information about Hosts — Foreman Ansible Modules documentation module and was able to get node information out of it :tada:

But I am struggling with:
https://theforeman.github.io/foreman-ansible-modules/latest/plugins/foreman_inventory.html#examples

Get inventory hosts from Foreman. sounds best for my project. But Ansible always complains about the syntax. Maybe someone can give me help here, please?

How about yourself? Everything should be on github GitHub - theforeman/foreman-ansible-modules: Ansible modules for interacting with the Foreman API and various plugin APIs such as Katello Clone it, updates docs as you think they would be easier to understand and create a pull request…

Generally, syntax errors are easiest to troubleshoot if you post the playbook and which errors you get exactly…

As I said it is about the examples here.

Here some fail examples:



theforeman.foreman.host_info works fine like this. But I didn’t get the above one running.

mf1045@pe-nb267:~/ansible/foreman$ cat foreman.yml 
- name: "test"
  hosts: localhost
  tasks:
  - name: show
    theforeman.foreman.host_info:
      server_url: "https://URL"
      username: "NAME"
      password: "PASSOWRD"
      search: "domain= DOMAIN"

Please do not post screenshots. It’s impossible to easily quote from screenshot.

The example you link to does not have a : at the end of the line. The error messages exactly points to that syntax error.

There is also no task with the name “plugin”.

The inventory plugin is a inventory plugin: Inventory plugins — Ansible Documentation

1 Like

I really hate that it is not allowed to edit the own posts afterwards.

But @gvde you are right. Pasting screenshots it not optimal.

Not that it is needed here, since you pointed into the correct way (Ansible Inventories), but maybe it helps someone that is searching for it. here the text outputs.


mf1045@pe-nb267:~/ansible/foreman$ cat my_foreman.yml 
---
- name: "test"
  hosts: localhost
  tasks:
  - name: show
    plugin: theforeman.foreman.foreman
      url: https://foreman.DOMAIN
      user: USERNAME
      password: PASSWORD

mf1045@pe-nb267:~/ansible/foreman$ ansible-playbook  my_foreman.yml 
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost
does not match 'all'
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
  mapping values are not allowed in this context

The error appears to be in '/home/mf1045/ansible/foreman/my_foreman.yml': line 7, column 10, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

    plugin: theforeman.foreman.foreman
      url: https://foreman.DOMAIN
         ^ here
mf1045@pe-nb267:~/ansible/foreman$ vim my_foreman.yml
mf1045@pe-nb267:~/ansible/foreman$ cat my_foreman.yml 
---
- name: "test"
  hosts: localhost
  tasks:
  - name: show
    theforeman.foreman.foreman:
      url: https://foreman.DOMAIN
      user: USERNAME
      password: PASSWORD

mf1045@pe-nb267:~/ansible/foreman$ ansible-playbook  my_foreman.yml 
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost
does not match 'all'
ERROR! couldn't resolve module/action 'theforeman.foreman.foreman'. This often indicates a misspelling, missing collection, or incorrect module path.

The error appears to be in '/home/mf1045/ansible/foreman/my_foreman.yml': line 5, column 5, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks:
  - name: show
    ^ here
mf1045@pe-nb267:~/ansible/foreman$ vim my_foreman.yml 
mf1045@pe-nb267:~/ansible/foreman$ cat my_foreman.yml 
plugin: theforeman.foreman.foreman:
url: https://foreman.DOMAIN
user: USERNAME
password: PASSWORD
host_filters: 'organization="Default Organization"'

mf1045@pe-nb267:~/ansible/foreman$ ansible-playbook  my_foreman.yml 
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost
does not match 'all'
ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.
  mapping values are not allowed in this context

The error appears to be in '/home/mf1045/ansible/foreman/my_foreman.yml': line 1, column 35, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


plugin: theforeman.foreman.foreman:
                                  ^ here
mf1045@pe-nb267:~/ansible/foreman$ cat foreman.yml 
- name: "test"
  hosts: localhost
  tasks:
  - name: show
    theforeman.foreman.host_info:
      server_url: "https://foreman.DOMAIN"
      username: "USERNAME"
      password: "PASSWORD"
      search: "domain= DOMAIN"

Solved by using ansible-inventory :man_facepalming:

ansible-inventory -i inventory/my_foreman.yml --list