cmd.run with watch or onchanges does not work or I don't understand

1,102 views
Skip to first unread message

Tom K.

unread,
Nov 17, 2016, 9:55:26 AM11/17/16
to Salt-users
I've some misunderstanding with cmd.run and watch/onchanges

I've setup a nrpe nagios server with salt. But I just want do reload/restart the server if there are changes to some configs.
My testings are with -watch and - onchanges. But everytime the state runs, the restartIfNeeded is executed.
cmd.wait is working but it's deprecated, so I don't want to use it anymore.

Thanks,

Tom


salt 2016.3.1 (Boron)


/etc/nagios/nrpe.cfg:
  file.managed:
    - source: salt://default-files/nrpe.cfg.jinja
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - context:
      collector: {{ possibleCollector }}
      
/etc/nagios/nrpe.d/base.cfg:
  file.managed:
    - source: salt://default-files/nrpe-base.cfg.jinja
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - context:
      w1: {{ grains['num_cpus'] * 1.5 }}
      w5: {{ grains['num_cpus'] * 1.5 }}
      w15: {{ grains['num_cpus'] * 1.5 }}
      c1: {{ grains['num_cpus']  * 3 }}
      c5: {{ grains['num_cpus'] * 2 }}
      c15: {{ grains['num_cpus'] * 2 }}
      
      
restartIfNeeded:
  cmd.run:
    - name: 'service nagios-nrpe-server restart'
    - user: root
    - group: root
    - watch:
      - file: /etc/nagios/nrpe.d/base.cfg
      - file: /etc/nagios/nrpe.cfg



Results in



networkingtest-001.off.loc:
----------
          ID: nrpe
    Function: pkg.installed
        Name: nagios-nrpe-server
      Result: True
     Comment: Package nagios-nrpe-server is already installed
     Started: 
    Duration: 
     Changes:   
----------
          ID: nrpe
    Function: service.running
        Name: nagios-nrpe-server
      Result: True
     Comment: Service nagios-nrpe-server has been enabled, and is in the desired state
     Started: 
    Duration: 
     Changes:   
              ----------
              nagios-nrpe-server:
                  True
----------
          ID: /etc/nagios/nrpe.cfg
    Function: file.managed
      Result: True
     Comment: File /etc/nagios/nrpe.cfg is in the correct state
     Started: 
    Duration: 
     Changes:   
----------
          ID: /etc/nagios/nrpe.d/base.cfg
    Function: file.managed
      Result: True
     Comment: File /etc/nagios/nrpe.d/base.cfg is in the correct state
     Started: 
    Duration: 
     Changes:   
----------
          ID: restartIfNeeded
    Function: cmd.run
        Name: service nagios-nrpe-server restart && echo "watch"
      Result: True
     Comment: Command "service nagios-nrpe-server restart" run
     Started: 
    Duration: 
     Changes:   
              ----------
              pid:
                  22303
              retcode:
                  0
              stderr:
              stdout:
                 

Summary for networkingtest-001.off.loc
------------
Succeeded: 5 (changed=2)
Failed:    0
------------

Thomas Phipps

unread,
Nov 17, 2016, 12:01:31 PM11/17/16
to Salt-users
see this https://github.com/saltstack/salt/issues/29492#issuecomment-162967147

also since you are just calling service restart any reason service.running with a watch is not working for you?

--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Thomas Phipps
Linux/Openstack/Logstash

Tom K.

unread,
Nov 17, 2016, 12:20:48 PM11/17/16
to Salt-users
Hi Thomas,

you are right. For the restart if that service it works fine with service.running.

But what about something like

jetty_restart:
  cmd.run:
    - name: '/usr/local/share/jetty9/bin/jetty.sh stop ; sleep 2 ; /usr/local/share/jetty9/bin/jetty.sh start'
    - user: root
    - group: root
    - onchanges:
      - file: /usr/local/share/jetty9/bin/jetty.sh


This should restart if some changes in jetty.sh are made. For example vm settings.

Thomas Phipps

unread,
Nov 17, 2016, 12:28:50 PM11/17/16
to Salt-users
yes, as long as you have a file.managed that handles /usr/local/share/jetty9/bin/jetty.sh yes

Tom K.

unread,
Nov 18, 2016, 5:12:14 AM11/18/16
to Salt-users
Hm,
Hi,

what about that setup? It doesn't matter of I do a -onchanges or -watch by "apply_sysctl"
State "apply_sysctl" always executed.


/etc/sysctl.conf:
  file.managed:
  - name: /etc/sysctl.conf
  - source: salt://default-files/sysctl.conf
  - user: root
  - group: root

apply_sysctl:
  cmd.run:
    - name: 'sysctl -p'
    - user: root
    - group: root
    - watch:
      - file: /etc/sysctl.conf


----------

          ID: /etc/sysctl.conf

    Function: file.managed

      Result: True

     Comment: File /etc/sysctl.conf is in the correct state

     Started: 

    Duration: 

     Changes:   

----------

          ID: apply_sysctl

    Function: cmd.run

        Name: sysctl -p

      Result: True

     Comment: Command "sysctl -p" run

     Started: 

    Duration: 

     Changes:   

              ----------

              pid:

                  24974

              retcode:

                  0

              stderr:

              stdout:

----------


Thanks,

Tom

Dmitry Golubenko

unread,
Nov 18, 2016, 5:30:43 AM11/18/16
to salt-...@googlegroups.com
В Птн, 18/11/2016 в 02:12 -0800, 'Tom K.' via Salt-users пишет:
> Hm,
> Hi,
>
>
> what about that setup? It doesn't matter of I do a -onchanges or
> -watch by "apply_sysctl"
> State "apply_sysctl" always executed.
it's because cmd.run always executed, it can't `watch'. try cmd.wait

Tom K.

unread,
Nov 18, 2016, 5:35:33 AM11/18/16
to Salt-users, dmitry.g...@xiag.ch
Hi Dmitry,

cmd.wait is working but it's deprecated, so I don't want to use it anymore.


Thomas Phipps

unread,
Nov 21, 2016, 10:36:21 AM11/21/16
to salt-...@googlegroups.com
please do NOT use watch with cmd.run ever. watch still runs the sls. always use onchanges with cmd.run if you have to use cmd.run and want it to have a watch like functionality.
also post the output of onchanges. you are still only showing watch. which will never do what you want. 

the main difference between watch and onchanges is watch runs the sls  depending on 



this is the result i get when i use onchanges

[root@salt01 testing]# salt 'salt01*' state.apply testing.test
----------
          ID: test_file
    Function: file.managed
        Name: /tmp/file
      Result: True
     Comment: File /tmp/file is in the correct state
     Started: 09:06:39.964343
    Duration: 19.599 ms
     Changes:
----------
          ID: echo_result
    Function: cmd.run
        Name: echo test
      Result: True
     Comment: State was not run because none of the onchanges reqs changed
     Started:
    Duration:
     Changes:

here is my sls 
test_file:
  file.managed:
    - name: /tmp/file
    - source: salt://testing/files/test
    - user: root
    - group: root

echo_result:
  cmd.run:
    - name: 'echo test'
    - user: root
    - onchanges:
      - file: test_file

Tom K.

unread,
Nov 23, 2016, 11:44:13 AM11/23/16
to Salt-users
Hi Thomas,

hm, what am I doing wrong?? I Think I use onchanges, but it fires up the cmd.run to restart my firewall


/etc/firewall.sh:
  file.managed:
    - name: /etc/firewall.sh
    - source: salt://default-files/firewall/firewall.sh
    - user: root
    - group: root
    - mode: 755

start_firewall:
  cmd.run:
    - name: '/etc/init.d/firewall restart'
    - user: root
    - group: root
    - onchanges:
      - file: /etc/firewall.sh
      
      
----------
          ID: /etc/firewall.sh
    Function: file.managed
      Result: True
     Comment: File /etc/firewall.sh is in the correct state
     Started: 
    Duration: 
     Changes:   
----------
          ID: start_firewall
    Function: cmd.run
        Name: /etc/init.d/firewall restart
      Result: True
     Comment: Command "/etc/init.d/firewall restart" run
     Started: 
    Duration: 
     Changes:   
              ----------
              pid:
                  15954
              retcode:
                  0
              stderr:
              stdout:
                  -e Stopping Firewall ...
                  -e Starting Firewall ...

Thomas Phipps

unread,
Nov 23, 2016, 12:33:10 PM11/23/16
to Salt-users
at this point i am not sure. maybe you are running into a bug that was on 2016.3.1. however i couldn't find a bug on that version. i copied your state into my setup and it worked without issue. 
there is one thing interesting. your file.managed state does not have a start time or a duration time. while mine does even on the unchanged one. one thing you can try doing is using -l debug and salt-call to see more about what salt is trying to do. 
Reply all
Reply to author
Forward
0 new messages