zoukankan      html  css  js  c++  java
  • salt 根据ip修改主机名

    首先定义pillar

    [root@web1 pillar]# cat hostname.sls 
    ip_hostname:
      10.1.1.1: web1
      10.1.1.2: web2
      10.1.1.3: mysql1
      10.1.1.4: redis1
      10.1.1.5: tomcat1
      10.1.1.6: dubbo1
      10.1.1.7: zabbix1

    在top.sls中调用

    [root@web1 pillar]# cat top.sls 
    base:
      '*':
        - hostname

    编写状态模块,根据ip通过pillar判断对应的主机名。

    [root@web1 base]# cat set-hostname.sls 
    {% set ip = salt['network.interface_ip']('eth1') %}
    alter_hostname:
      cmd.run:
        - name: hostnamectl  set-hostname {{ pillar['ip_hostname'][ip] }}

    测试执行

    [root@web1 base]# salt-ssh  '*' state.sls set-hostname  test=True
    tomcat1:
    ----------
              ID: alter_hostname
        Function: cmd.run
            Name: hostnamectl  set-hostname tomcat1
          Result: None
         Comment: Command "hostnamectl  set-hostname tomcat1" would have been executed
         Started: 10:40:50.085925
        Duration: 0.949 ms
         Changes:   
    
    Summary for tomcat1
    ------------
    Succeeded: 1 (unchanged=1)
    Failed:    0
    ------------
    Total states run:     1
    Total run time:   0.949 ms
    web1:
    ----------
              ID: alter_hostname
        Function: cmd.run
            Name: hostnamectl  set-hostname web1
          Result: None
         Comment: Command "hostnamectl  set-hostname web1" would have been executed
         Started: 10:40:53.455021
        Duration: 0.814 ms
         Changes:   
    
    Summary for web1
    ------------
    Succeeded: 1 (unchanged=1)
    Failed:    0
    ------------
    Total states run:     1
    Total run time:   0.814 ms
    dubbo1:
    ----------
              ID: alter_hostname
        Function: cmd.run
            Name: hostnamectl  set-hostname dubbo1
          Result: None
         Comment: Command "hostnamectl  set-hostname dubbo1" would have been executed
         Started: 10:40:51.212502
        Duration: 0.817 ms
         Changes:   
    
    Summary for dubbo1
    ------------
    Succeeded: 1 (unchanged=1)
    Failed:    0
    ------------
    Total states run:     1
    Total run time:   0.817 ms
    mysql1:
    ----------
              ID: alter_hostname
        Function: cmd.run
            Name: hostnamectl  set-hostname mysql1
          Result: None
         Comment: Command "hostnamectl  set-hostname mysql1" would have been executed
         Started: 10:40:50.058353
        Duration: 7.89 ms
         Changes:   
    
    Summary for mysql1
    ------------
    Succeeded: 1 (unchanged=1)
    Failed:    0
    ------------
    Total states run:     1
    Total run time:   7.890 ms
    web2:
    ----------
              ID: alter_hostname
        Function: cmd.run
            Name: hostnamectl  set-hostname web2
          Result: None
         Comment: Command "hostnamectl  set-hostname web2" would have been executed
         Started: 10:40:51.869016
        Duration: 0.85 ms
         Changes:   
    
    Summary for web2
    ------------
    Succeeded: 1 (unchanged=1)
    Failed:    0
    ------------
    Total states run:     1
    Total run time:   0.850 ms
    zabbix:
    ----------
              ID: alter_hostname
        Function: cmd.run
            Name: hostnamectl  set-hostname zabbix1
          Result: None
         Comment: Command "hostnamectl  set-hostname zabbix1" would have been executed
         Started: 10:40:53.615229
        Duration: 0.856 ms
         Changes:   
    
    Summary for zabbix
    ------------
    Succeeded: 1 (unchanged=1)
    Failed:    0
    ------------
    Total states run:     1
    Total run time:   0.856 ms
    redis1:
    ----------
              ID: alter_hostname
        Function: cmd.run
            Name: hostnamectl  set-hostname redis1
          Result: None
         Comment: Command "hostnamectl  set-hostname redis1" would have been executed
         Started: 10:40:53.801096
        Duration: 0.857 ms
         Changes:   
    
    Summary for redis1
    ------------
    Succeeded: 1 (unchanged=1)
    Failed:    0
    ------------
    Total states run:     1
    Total run time:   0.857 ms
    View Code
  • 相关阅读:
    [云计算&大数据]概念辨析:数据仓库 | 数据湖 | 数据中心 | 数据中台 | 数据平台 【待续】
    [云计算]概念辨析:云计算 [IaaS/PaaS/SaaS & 公有云/私有云/混合云]
    [Linux]浅析"command > /dev/null 2>&1 &" 与 "command 1>/dev/null 2>&1 &"
    [Python]【Form Data vs Request Payload】之 python 爬虫如何实现 POST request payload 形式的请求
    [Python]PyCharm中出现unresolved reference的解决方法
    [数据库/MYSQL]MYSQL开启Bin-Log
    scrapy采集gb2312网页中文乱码笔记
    uTools 桌面软件。
    Asp.Net Core Grpc 入门实践
    ASP.NET Core 中间件(Middleware)(一)
  • 原文地址:https://www.cnblogs.com/lfdblog/p/9717312.html
Copyright © 2011-2022 走看看