Saltstack file synchronization and python call
Saltstack file synchronization and python call
Saltstack was written in python2.6. It took a lot of effort to call it in python. first install salt-master (or salt-minion), and then enter the following command:
# Python2.6
>>> Import salt. config
No error. Now you can use python to call the salt api.
1. Synchronize files
Slatsatck is used to synchronize the configuration files on the master to the minion node. First, add the settings on the master:
# mkdir -p /srv/salt/dev/services /srv/salt/dev/states /srv/salt/prod/services /srv/salt/prod/services# mkdir /etc/salt/master.d/# vi fileserver.conf# service salt-master restart
The content of fileserver. conf is as follows:
#### fileserver.conf# Salt runs a lightweight file server written in zeromq to deliver files to# minions. This file server is built into the master daemon and does not# require a dedicated port.file_roots: base: - /srv/salt/ dev: - /srv/salt/dev/services - /srv/salt/dev/states prod: - /srv/salt/prod/services - /srv/salt/prod/states
# Salt '*' cp. get_file salt: // first. xml/tmp/first. xml or gzip = 1-9. The larger the number, the higher the compression. makedirs = True automatically creates the directory # salt '*' cp. get_file salt: // first. xml/tmp/salt/first. xml gzip = 9 makedirs = True
In this way, we can use scripts to flexibly control the distribution of configuration files on the server (master.
2. grains
1) The role of grains is to call this interface when the minion service is started to collect information on the minion client. The information data can be directly used in other modules of salt. Note that, this interface is called only once when the minion service on the minion end is started, so the collected data is static and will not change unless you restart the minion service. execute the following command on the master node (202 is the minion node id) to collect information on the minion end:
# salt '202' grains.ls
Display:
- SSDs - biosreleasedate - biosversion - cpu_flags - cpu_model - cpuarch - domain - fqdn - fqdn_ip4...
2) use the grains. items module to list the specific data of all available grains:
# Salt '000000' grains. items3) to view the IP addresses of all nodes:
# salt ‘*‘ grains.item ipv4 # NOT grains.items
4) view the OS version 202:
# Salt '000000' grains. item OS
3. directory synchronization
Cp. get_dir, get_dir, and get_file are similar in usage and are used to distribute the entire directory to minions. In this example, create directories and files under/srv/salt of the master:
/Srv/salt/hadoop_conf_dir/hadoop. config, Which is synchronized to/tmp/hacl_conf of all minion nodes. If the directory does not exist, it is created and compressed
# salt ‘*‘ cp.get_dir salt://hadoop_conf_dir /tmp/hacl_conf gzip=9 makedirs=True
In this way, the minion is:
/Tmp/hacl_conf/hadoop_conf_dir/hadoop. config
4. pillar Interface
TODO: