Skip to main content

Salt Diaries: keeping salt up-to-date (episode 4)



Welcome back! In our quest to simplify the configuration and automate our systems we have installed Salt on all our servers and then moved on to some basic state management. We want of course to do more sophisticated stuff with salt and we'll get to that too. But first we want to make sure that all minions are aligned to the same salt version (the latest in this case).

To do that we will add another state to our configuration which we will call (very much unimaginatively) salt.sls. The content is below:
salt-minion:
   pkg:
      - latest
   service:
      - running
      - watch:
         - pkg: salt-minion

This instructs minions to upgrade the salt-minion package on the node and, if upgraded, restart the service. To activate this state we'll edit the top.sls state file as follows:
base:
  '*':
    - ntp
    - salt

We are now ready to apply the changes. Let's start with a guinea-pig minion:
[prompt]# salt 'expendable.local' state.highstate
expendable.local:
----------
    State: - pkg
    Name:      salt-minion
    Function:  latest
        Result:    True
        Comment:   Package salt-minion upgraded to latest
        Changes:   salt-minion: {'new': '0.10.2-2.el5', 'old': '0.10.1-1.el5'}
                   salt: {'new': '0.10.2-2.el5', 'old': '0.10.1-1.el5'}
                   
----------
    State: - service
    Name:      salt-minion
    Function:  running
        Result:    True
        Comment:   Service restarted
        Changes:   salt-minion: True

Seems ok, let's check the package version (note that I turned on verbose logging):
[prompt]# salt -v 'expendable.local' pkg.version salt
Executing job with jid 20120906174807503993
-------------------------------------------

The following minions did not return:
expendable.local

Ooop! Something is not quite right...in fact the minion is up and running, it's just that the server is still using the old connection. A quick check with netstat on the minion side will confirm this. Luckily for us there's no need to login onto each minion to get Salt working again. Simply restart the master and it'll be all ready to go:
[prompt]# /etc/init.d/salt-master restart
Stopping salt-master daemon:                               [  OK  ]
Starting salt-master daemon:                               [  OK  ]
[prompt]# salt -v 'expendable.local' pkg.version salt
Executing job with jid 20120906175310393699
-------------------------------------------
{'expendable.local': '0.10.2-2.el5'}

Very good! Now we can deploy the new state to all minions with:
[prompt]# salt -v -t 60 '*' state.highstate
we'll just have to restart the master after that and we're good to go.

Minions restarting while other states are running or have to be run

One problem with the state definition given above is that the package upgrade (and consequent minion restart) could be executed while other states are also running, so to prevent that we'll edit the state again and add an order condition:
salt-minion:
   pkg:
      - latest
      - order: last
   service:
      - running
      - watch:
         - pkg: salt-minion

What about SuSE-based servers?

Since to install Salt on SuSE servers we did not use the default distro package manager we'll have to script our way out using the same tool that we used for installing Salt: pip.
The new state definition becomes this:
salt-minion:
{% if grains['os'] == 'RedHat' %}
   pkg:
      - latest
{% endif %}
{% if grains['os'] == 'SUSE' %}
   pip:
      - installed
      - name: salt
      - upgrade: True
{% endif %}
      - order: last
   service:
      - running
      - watch:
{% if grains['os'] == 'RedHat' %}
        - pkg: salt-minion
{% endif %}
{% if grains['os'] == 'SUSE' %}
        - pip: salt
{% endif %}

Update: while this is the formally correct way for upgrading SuSE it won't work because the pip state has a bug for which it won't update an already installed package. As we can see here the state returns when a package is already installed. I'm going to open a pull request to get it fixed asap.

Update March 2013: a reader notes that SaltStack rpm packages are available from the OpenSuse repo so I suggest you switch to using those.

Comments

Popular posts from this blog

Indexing Apache access logs with ELK (Elasticsearch+Logstash+Kibana)

Who said that grepping Apache logs has to be boring?

The truth is that, as Enteprise applications move to the browser too, Apache access logs are a gold mine, it does not matter what your role is: developer, support or sysadmin. If you are not mining them you are most likely missing out a ton of information and, probably, making the wrong decisions.
ELK (Elasticsearch, Logstash, Kibana) is a terrific, Open Source stack for visually analyzing Apache (or nginx) logs (but also any other timestamped data).

From 0 to ZFS replication in 5m with syncoid

The ZFS filesystem has many features that once you try them you can never go back. One of the lesser known is probably the support for replicating a zfs filesystem by sending the changes over the network with zfs send/receive.
Technically the filesystem changes don't even need to be sent over a network: you could as well dump them on a removable disk, then receive  from the same removable disk.

A not so short guide to TDD SaltStack formulas

One of the hardest parts about Infrastructure As Code and Configuration Management is establishing a discipline on developing, testing and deploying changes.
Developers follow established practices and tools have been built and perfected over the last decade and a half. On the other hand sysadmins and ops people do not have the same tooling and culture because estensive automation has only become a trend recently.

So if Infrastructure As Code allows you to version the infrastructure your code runs on, what good is it if then there are no tools or established practices to follow?

Luckily the situation is changing and in this post I'm outlining a methodology for test driven development of SaltStack Formulas.

The idea is that with a single command you can run your formula against a matrix of platforms (operating systems) and suites (or configurations). Each cell of the matrix will be tested and the result is a build failure or success much alike to what every half-decent developer of…