Bug 2209200

Summary: rhc system role does not apply Insights tags
Product: Red Hat Enterprise Linux 9 Reporter: Benjamin Blasco <bblasco>
Component: rhel-system-rolesAssignee: Rich Megginson <rmeggins>
Status: VERIFIED --- QA Contact: Jakub Haruda <jharuda>
Severity: low Docs Contact:
Priority: high    
Version: 9.2CC: bblasco, briasmit, cdonnell, djez, fkrska, jharuda, pkettman, ptoscano, rmeggins, spetrosi, vdanek
Target Milestone: rcKeywords: Triaged, ZStream
Target Release: 9.3Flags: rmeggins: needinfo? (djez)
rmeggins: needinfo? (vdanek)
rmeggins: needinfo? (cdonnell)
fkrska: needinfo+
rmeggins: needinfo? (pkettman)
Hardware: All   
OS: All   
Whiteboard: role:rhc
Fixed In Version: rhel-system-roles-1.22.0-0.7.el9 Doc Type: Bug Fix
Doc Text:
Cause: The `rhc` system role did not write the tags for Insights in the correct file. Consequence: The tags were not sent to Insights, and thus not applied for the systems in the Host Based Inventory (HBI). Fix: The `rhc` system role now writes the tags for Insights in the correct file. Result: The tags are applied correctly for the systems, and thus they are visible in HBI.
Story Points: ---
Clone Of:
: 2209441 2214284 (view as bug list) Environment:
Last Closed: Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 2209441, 2214284    

Description Benjamin Blasco 2023-05-23 05:16:30 UTC
Description of problem:
Following the documentation outlined below, it is not possible to get the rhc system role to apply Insights tags to a system during registration.  

Following documentation here:
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/html-single/automating_system_administration_by_using_rhel_system_roles/index#configuring-insights-tags-using-rhc-system-role_using-the-rhc-system-role-to-register-the-system

Version-Release number of selected component (if applicable):
All

How reproducible:
100% of the time

Steps to Reproduce:
1. Deploy a new RHEL system
2. Run an Ansible playbook containing the following code snippet (or similar):
```
  vars:
    rhc_auth:
      activation_keys:
        keys:
          - "{{ activation_key }}"

    rhc_organization: "{{ organization_id }}"

    rhc_insights:
      state: present
      autoupdate: true
      remediation: present
      tags:
        environment: dev
        location: "au/vic/melbourne"

  tasks:

    - name: Run the RHC role
      include_role:
        name: linux-system-roles.rhc
      tags:
        - rhc

3. Check the tags in the Hybrid Cloud Console inventory here: https://console.redhat.com/insights/inventory/

Actual results:
The registered system(s) do not have any tags set in the Hybrid Cloud Consoleeven though they are defined in the yaml files as shown below:

```
[bblasco@opti rhel]$ ansible 'rhel9*' -m shell -a "cat /etc/insights-client/tags.yml"
rhel92.opti.blasco.id.au | CHANGED | rc=0 >>
environment: dev
location: au/vic/melbourne
rhel91.opti.blasco.id.au | CHANGED | rc=0 >>
environment: dev
location: au/vic/melbourne
```

Expected results:
The registered systems should have the tags defined in the role variables


Additional info:
The (upstream) code snippet turning the variables into yaml for the tags is here: https://github.com/linux-system-roles/rhc/blob/main/tasks/insights-client.yml

```
- name: Add insights tags
  copy:
    dest: /etc/insights-client/tags.yml
    content: "{{ rhc_insights.tags | to_nice_yaml(indent=2) }}"
    mode: '0644'
  register: __insights_tags_added
  when:
    - rhc_insights.tags is defined
    - rhc_insights.tags != __rhc_state_absent
    - rhc_insights.tags | d({}) | length > 0
```

By contrast I have used another insights-client role and successfully added tags to the systems with the hybrid cloud console.  See example below

Code snippet from the RHEL role located at: https://github.com/RedHatInsights/ansible-collections-insights/blob/master/roles/insights_client/tasks/main.yml
```
- name: Deploy Custom Tags
  copy:
    dest: /etc/insights-client/tags.yaml
    content: "{{ insights_tags | to_nice_yaml }}"
    mode: og=r
  become: true
  when: insights_tags is defined
  notify: Run insights-client
```

Example use of the role:
```
  vars:
    insights_tags:
      environment: dev
      #application: web
      location:
        country: au
        state: vic
        city: melbourne
  tasks:
    - name: Run the Insights Client Role
      include_role:
        name: "{{ item }}"
      loop:
        - redhatinsights.insights.insights_client
```

Contents of tags.yaml
```
[bblasco@aap ~]$ cat /etc/insights-client/tags.yaml 
environment: dev
location:
    city: melbourne
    country: au
    state: vic
```
The tags above then appear as the following in the hybrid cloud console:
Name              Value      Tag source
environment       dev        insights-client
location:city     melbourne  insights-client
location:state	  vic        insights-client
location:country  au         insights-client

Comment 2 Benjamin Blasco 2023-05-23 06:17:07 UTC
I have found the issue.

The role is templating out a file called tags.yml when insights expects it to be tags.yaml (YAML not YML).  The code that does this can be found on line 15 here:
https://github.com/linux-system-roles/rhc/blob/main/tasks/insights-client.yml

Comment 3 Pino Toscano 2023-05-23 06:29:44 UTC
(In reply to Benjamin Blasco from comment #2)
> I have found the issue.
> 
> The role is templating out a file called tags.yml when insights expects it
> to be tags.yaml (YAML not YML).  The code that does this can be found on
> line 15 here:
> https://github.com/linux-system-roles/rhc/blob/main/tasks/insights-client.yml

d'oh, thanks!

hopefully easy fix for this: https://github.com/linux-system-roles/rhc/pull/102