It has been more than a week since I studied saltstack. The probation period of a company is six months. The most difficult thing is that the company has no permissions in the past six months. For an O & M Company, having no permissions is not the same as having no permissions. The landlord is bored, and he only has to make a small test on his own.
Reactor
Speaking of reactor, we should start with the events of salt. What is the events?
Speaking of events, we have to start with salt's communication mechanism. Salt uses zeromq to communicate. This zeromq communication mode is called
Pub/sub mode,
Obviously pub is publish, meaning publish
Sub is subscribe, meaning subscription.
OK. If you see this, you will probably know that it is just a sending and receiving task.
It's okay to know so much about it. When the landlord just graduated from college, he worked on after-sales maintenance for over half a year, followed by half a year of O & M, and did not do development. If it's too deep, he won't do anything.
OK, the events system is a local zeromq pub interface, so that the events system can generate an event. What is the length of the event? It looks like this.
Event fired at Thu Jun 26 10:43:08 2014*************************Tag: salt/job/20140626104308591305/newData:{‘_stamp‘: ‘2014-06-26T10:43:08.591696‘, ‘arg‘: [‘/bin/ls /home‘], ‘fun‘: ‘cmd.run‘, ‘jid‘: ‘20140626104308591305‘, ‘minions‘: [‘salt-minion‘], ‘tgt‘: ‘*‘, ‘tgt_type‘: ‘glob‘, ‘user‘: ‘root‘}}
Each event has a tag, that is, a tag. With this tag, others can recognize you. Know what you are doing.
Of course, we can also filter or select the expected event according to the tag .... Then the tag is followed by the specific data. The data is a dictionary structure. The dictionary name is data, and the content is the following content.
OK. The above is a brief description of the events system.
What is a reactor?
In fact, the reactor is the tag that matches the event. If a tag that matches my definition is found, I can trigger the corresponding operation.
This is similar to the trigger in our database ..
OK, the reactor system principle. Now we know... Let's see how to implement it.
First, modify the master configuration.
[email protected]:~# cat >>/etc/salt/master.d/reactor.conf <<EOF> reactor:> - ‘lixc‘:> - /srv/reactor/lixc.sls> EOF[email protected]:~#
Okay, reactor. This is the Starting Sign of reactor configuration. 'lixc 'is the tag we want to match,/srv/reactor/lixc. SLS is the operation triggered if a matching tag exists. The compiling rules for this file are similar to those for state.
Check/srv/reactor/lixc. SLS
[email protected]:~# cat >>/srv/reactor/lixc.sls <<EOF> clean_tmp:> cmd.cmd.run:> - tgt: ‘os:Debian‘> - expr_form: grain> - arg:> - echo "hello liss" >>/tmp/test.log> EOF
What does this SLS file mean. In line 3, just give a name. In line 3, what does cmd. cmd. Run mean?
It means to use the command API of salt to execute the run function of the CMD module. What is the command API? That is to say, all the things we execute in the command line can be used in this SLS with this API.
This lixc. SLS should be executed on the command line. This can also be done.
Salt-g' OS: Debian 'cmd. run' echo "Hello Liss>/tmp/test. log "'
OK, lines 3 and 4 are the target and matching method. Lines 5 and 6 are the parameters to be passed in by the cmd. Run function.
OK. Let's test it.
We modified the configuration file of the master. First, restart the master.
[email protected]:~# /etc/init.d/salt-master restart[ ok ] Restarting salt master control daemon: salt-master.
Manually generate an event on Minion,
[email protected]:~# salt ‘*‘ cmd.run "salt-call event.fire_master ‘{"lixc": "lixc"}‘ ‘lixc‘"salt-minion: local: True[email protected]:~#
Let's take a look at the details of this event.
Event fired at Thu Jun 26 12:30:03 2014*************************Tag: lixcData:{‘_stamp‘: ‘2014-06-26T12:30:03.826420‘, ‘cmd‘: ‘_minion_event‘, ‘data‘: {‘lixc‘: ‘lixc‘}, ‘id‘: ‘salt-minion‘, ‘pretag‘: None, ‘tag‘: ‘lixc‘}Event fired at Thu Jun 26 12:30:03 201
Check whether lixc. SlS has been executed?
[email protected]:~# salt ‘*‘ cmd.run ‘cat /tmp/test.log‘salt-minion: Hello liss
OK, reactor can use the command API or Runner
Okay. Let's take a look. Modify/etc/salt/master. d/reactor. conf
[email protected]:~# cat /etc/salt/master.d/reactor.conf reactor: - ‘lixc‘: - /srv/reactor/lixc.sls - /srv/reactor/liss.sls[email protected]:~#
Take a look at/srv/reactor/Liss. SLS. Note-env:/home is the parameter passed to the function lsdir. lsdir. Lsdir is the runner written in the previous article.
[email protected]:~# cat /srv/reactor/liss.sls test_runner: runner.lsdir.lsdir: - env: /home[email protected]:~#
Restart the master and manually trigger an event.
[email protected]ter:~# /etc/init.d/salt-master restart[ ok ] Restarting salt master control daemon: salt-master.[email protected]:~# salt ‘*‘ cmd.run "salt-call event.fire_master ‘{"lixc": "lixc"}‘ ‘lixc‘"salt-minion: local: True
Let's take a look at the result of runner execution.
[email protected]:~# cat /tmp/test.log salt-minion lixc lost+found salt-develop salt-develop.zip
OK. These are the simplest basic functions.
Saltstack started with this. After more than a week of learning, the common functions are basically learned. Because there are too many functions and there is no actual requirement or practice, I don't want to go further. There is another salt API available to summarize ..
Next we started to fight ansible, haha!
This article is from the "West Wind" blog, please be sure to keep this source http://lixcto.blog.51cto.com/4834175/1431162