amazon-ec2managednewrelicsalt-project

Salt-stack: replace/add minion name to file in managed file formula


I am using New Relic to monitor my Salt managed EC2 servers, and I am trying to insert a "hostname=minion-name" in the newrelic-sysmond confic file, so they show up in New Relic's dashboard with a reconizable name, instead of the EC2 default "ip-123-133...".

I spin my instances with salt-cloud and then apply the following state (trough the top file) to get New Relic sysmond running:

newrelic-repo:
  pkg:
    - installed
    - require:
      - pkgrepo: <my private repo defined elsewhere, just convenient rpm storage>

newrelic-sysmond:
  pkg:
    - installed
    - require:
      - pkg: newrelic-repo
  service:
    - running
    - watch:
      - file: /etc/newrelic/nrsysmond.cfg
    
/etc/newrelic/nrsysmond.cfg:
  file.managed:
    - source: salt://newrelic/nrsysmond.cfg
    - user: newrelic
    - mode: 744
    - require:
      - pkg: newrelic-sysmond

The crux is: /etc/newrelic/nrsysmond.cfg managed file is a version with my personal account key, required by New Relic setup, so it is the same for all my machines.

Is there a way for me have something like hostname=my_placeholder in that file and then in my sls config have it so that when the state is applied that my_placeholder becomes the minion name?

Digging trough the states.file doc (http://docs.saltstack.com/ref/states/all/salt.states.file.html) I get the feeling it is possible, but I am missing some foundational knowledge to figure it out, as I am just getting started with salt. Mainly I think I just need a nudge in a way as to how to reference the variable/grain holding the minion name, and an example of defaults/contexts in use.


Solution

  • First, you have to enable templating of this config file:

    /etc/newrelic/nrsysmond.cfg:
      file.managed:
        - source: salt://newrelic/nrsysmond.cfg
        - user: newrelic
        - mode: '0744'
        - template: jinja
        - require:
          - pkg: newrelic-sysmond
    

    And then this file is treated as jinja template:

    hostname={{ grains["id"] }}
    

    The name of the minion is available in grains["id"]. You can run salt-call grains.items to see available grains.