skip to Main Content

I am trying to use Prometheus Role: https://github.com/prometheus-community/ansible

I created a playbook – playbook.yaml, with the following content:

---
- hosts: prometheus-server
  become: true
  roles:
    - role: prometheus.prometheus.prometheus
      prometheus_version: "2.43.0"  
      prometheus_config_dir: "/sites/prometheus"
      prometheus_web_listen_address: "0.0.0.0:9090"  
      prometheus_global: {"evaluation_interval": "1m5s", "scrape_interval": "1m", "scrape_timeout": "59s"}
      prometheus_external_labels: {"environment": "env1"}
      prometheus_alert_rules_files: ["/sites/prometheus/rules.d/rules.yml"]
      prometheus_alertmanager_config: 
        - scheme: http
          static_configs:
          - targets:
            - alertmanager-1.xxx:9093
            

The inventory is OK:

all:
  hosts:
    prometheus-server:
      ansible_host: x.x.x.x  
  vars:
    ansible_user: ubuntu
    ansible_ssh_private_key_file: ~/.ssh/id_rsa_test
    ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -i ~/.ssh/id_rsa [email protected]"'

I changed all these values to use rules.d:

In roles/prometheus/templates/prometheus.yml.j2:

{% if not prometheus_agent_mode and prometheus_alert_rules_files != [] %}
rule_files:
  - {{ prometheus_config_dir }}/rules.d/*.rules
{% endif %}

In roles/prometheus/tasks/configure.yml

src: "{{ item }}"
dest: "{{ prometheus_config_dir }}/rules.d/"
owner: root

In ansible/roles/prometheus/meta/argument_specs.yml, nothing to change:

  prometheus_alert_rules:
    description:
      - "Full list of alerting rules which will be copied to C({{ prometheus_config_dir }}/rules.d/ansible_managed.rules)."
      - "Alerting rules can be also provided by other files located in C({{ prometheus_config_dir }}/rules.d/) which have C(*.rules) extension"
      - "Please see default values in role defaults/main.yml"
    type: "list"
    elements: "dict"

When running the playbook, it still returns the default route (even if changed and saved).

I also tried manually adding other tasks, that are not being executed, in tasks:

https://github.com/prometheus-community/ansible/tree/9fd1b0f52954b3f6e1338ef1466a0cb8fd30fd9f/roles/prometheus/tasks

But nothing…

In my server, I manually changed the value and restarted prometheus, to run the playbook and check that it changes again to the "rules" route (to see that the playbook works).

Not sure what I can do…

To help with troubleshooting:

  • Permissions in the destination server are the same.
  • I even created some tasks that are not being executed in the role.
  • SCP WARNINGS are triggered in the playbook (I think that is not related to the problem).

Some of the warnings I receive:
Adding WARNINGS:

[WARNING]: sftp transfer mechanism failed on [X.X.X.X]. Use ANSIBLE_DEBUG=1 to see detailed information
[WARNING]: scp transfer mechanism failed on [X.X.X.X]. Use ANSIBLE_DEBUG=1 to see detailed information

2

Answers


  1. Chosen as BEST ANSWER

    The problem was finally related to using prometheus.prometheus.prometheus, and because the playbook was inside of the role, causing some problems while executing.

    By changing the route of the playbook, and using only "prometheus" as role, this was solved.


  2. I created a playbook – playbook.yaml, with the following content:

    If this is the exact content of your playbook, your variables are ignored because you passed them outside of the vars keyword (please check Using roles at the play level for details). It should have been something like this:

    ---
    - hosts: prometheus-server
      become: true
      roles:
        - role: prometheus.prometheus.prometheus
          vars:
            prometheus_version: "2.43.0"  
            prometheus_config_dir: "/sites/prometheus"
            prometheus_web_listen_address: "0.0.0.0:9090"  
            prometheus_global: {"evaluation_interval": "1m5s", "scrape_interval": "1m", "scrape_timeout": "59s"}
            prometheus_external_labels: {"environment": "env1"}
            prometheus_alert_rules_files: ["/sites/prometheus/rules.d/rules.yml"]
            prometheus_alertmanager_config: 
              - scheme: http
                static_configs:
                  - targets:
                    - alertmanager-1.xxx:9093
    

    Alternatively, and I would call it a preferable way as it’s more flexible and doesn’t require playbook changes, you could put the variables directly into your inventory:

    ---
    all:
      hosts:
        prometheus-server:
          ansible_host: x.x.x.x  
      vars:
        ansible_user: ubuntu
        ansible_ssh_private_key_file: ~/.ssh/id_rsa_test
        ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -i ~/.ssh/id_rsa [email protected]"'
        prometheus_version: "2.43.0"  
        prometheus_config_dir: "/sites/prometheus"
        prometheus_web_listen_address: "0.0.0.0:9090"  
        prometheus_global: {"evaluation_interval": "1m5s", "scrape_interval": "1m", "scrape_timeout": "59s"}
        prometheus_external_labels: {"environment": "env1"}
        prometheus_alert_rules_files: ["/sites/prometheus/rules.d/rules.yml"]
        prometheus_alertmanager_config: 
          - scheme: http
            static_configs:
              - targets:
                - alertmanager-1.xxx:9093
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search