其他分享
首页 > 其他分享> > saltstack 批量修改hostname

saltstack 批量修改hostname

作者:互联网

1. pillor配置

vim /etc/salt/master
#pillar默认不开启,将参数设置为True pillar_opts: True
#设置文件目录 pillar_roots: base: - /srv/salt/pillar

2. 在pillar目录下创建hostname.sls(ip和主机名的键值对)

cat pillar/hostname.sls
ip_hostname:
  172.16.1.12: web1

3.在pillar目录下创建top.sls

base:
  '*':
    - hostname

4. 在salt base目录下创建set-host.sls(默认目录:/srv/salt)

  方式一:用cmd.run模块执行shell命令

{% set ip = salt['network.interface_ip']('eth0') %}
alter_hostname:
  cmd.run:
    - name: hostnamectl  set-hostname {{ pillar['ip_hostname'][ip] }}

  方式二:用自带模块network.mod_hostname修改

{% set ip = salt['network.interface_ip']('eth0') %}
{% set ip_hostname = pillar['ip_hostname'] %}
{% set hostname = ip_hostname[ip] %}

mod_hostname:
  module.run:
    - name: network.mod_hostname
    - hostname: {{hostname}}

5.执行测试

  方式一:

salt-ssh '*' state.sls set-hostname test=True

nginx_01:
----------
          ID: alter_hostname
    Function: cmd.run
        Name: hostnamectl  set-hostname web1
      Result: None
     Comment: Command "hostnamectl  set-hostname web1" would have been executed
     Started: 19:46:34.526655
    Duration: 0.812 ms
     Changes:   

Summary for nginx_01
------------
Succeeded: 1 (unchanged=1)
Failed:    0
------------
Total states run:     1
Total run time:   0.812 ms

  方式二:

salt-ssh '*' state.sls set-hostname1 test=True

nginx_01:
----------
          ID: mod_hostname
    Function: module.run
        Name: network.mod_hostname
      Result: None
     Comment: Module function network.mod_hostname is set to execute
     Started: 19:49:43.979727
    Duration: 3.83 ms
     Changes:   

Summary for nginx_01
------------
Succeeded: 1 (unchanged=1)
Failed:    0
------------
Total states run:     1
Total run time:   3.830 ms

 

标签:set,run,批量,ip,pillar,hostname,saltstack,salt
来源: https://www.cnblogs.com/Jack1023/p/12482252.html