模塊、文件同步問題

一、salt 倉庫目錄配置

file_roots:
  base:
    - /data/salt/
  dev:
    - /data/salt/dev/services
    - /data/salt/dev/states
  prod:
    - /data/salt/prod/services
    - /data/salt/prod/states
  winrepo:
    - /data/salt/winrepo/files
    - /data/salt/winrepo/states

二、倉庫目錄結構

[root@salt-master01 salt]# tree -a
.
├── _modules
│   ├── mygit.py
│   └── net_ops.py
└── winrepo
    ├── files
    │   ├── .gitconfig
    │   ├── .git-credentials
    │   ├── git-lfs-windows-2.0.2.exe
    │   └── iiscrash.reg
    └── states
        └── git-config.sls

4 directories, 7 files

目前先只建了 winrepo

三、sls 配置

[root@salt-master01 salt]# cat winrepo/states/git-config.sls 
C:\Windows\System32\config\systemprofile\.gitconfig:
  file.managed:
    - source: salt://winrepo/files/.gitconfig

C:\Windows\System32\config\systemprofile\.git-credentials:
  file.managed:
    - source: salt://winrepo/files/.git-credentials

四、用法

winrepo.states.git-config 即是 按照目錄結構

[root@salt-master01 salt]# salt "net-api05.ycf.com" state.sls winrepo.states.git-config
net-api05.ycf.com:
----------
          ID: C:\Windows\System32\config\systemprofile\.gitconfig
    Function: file.managed
      Result: True
     Comment: File C:\Windows\System32\config\systemprofile\.gitconfig is in the correct state
     Started: 12:07:57.960000
    Duration: 102.0 ms
     Changes:   
----------
          ID: C:\Windows\System32\config\systemprofile\.git-credentials
    Function: file.managed
      Result: True
     Comment: File C:\Windows\System32\config\systemprofile\.git-credentials is in the correct state
     Started: 12:07:58.063000
    Duration: 13.0 ms
     Changes:   

Summary for net-api05.ycf.com
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
Total run time: 115.000 ms

或者直接使用

salt "net-api05.ycf.com" state.highstate

但要求有top.sls,因爲 stat.highstate 默認是讀取 base 倉庫下的top.sls 文件

[root@salt-master01 salt]# cat top.sls 
winrepo:
  '*':
    - git-config

但是配置了top.sls 之後,saltutil.sync_modules 模塊同步就失效了

查看文檔:

[root@salt-master01 salt]# sudo salt "net-api05.ycf.com" sys.doc saltutil.sync_modules
saltutil.sync_modules:

    New in version 0.10.0

    Sync execution modules from ``salt://_modules`` to the minion

    saltenv
        The fileserver environment from which to sync. To sync from more than
        one environment, pass a comma-separated list.

        If not passed, then all environments configured in the :ref:`top files
        <states-top>` will be checked for execution modules to sync. If no top
        files are found, then the ``base`` environment will be synced.

    refresh : True
        If ``True``, refresh the available execution modules on the minion.
        This refresh will be performed even if no new execution modules are
        synced. Set to ``False`` to prevent this refresh.

    .. important::

        If this function is executed using a :py:func:`module.run
        <salt.states.module.run>` state, the SLS file will not have access to
        newly synced execution modules unless a ``refresh`` argument is
        added to the state, like so:

            load_my_custom_module:
              module.run:
                - name: saltutil.sync_modules
                - refresh: True

        See :ref:`here <reloading-modules>` for a more detailed explanation of
        why this is necessary.

    extmod_whitelist : None
        comma-seperated list of modules to sync

    extmod_blacklist : None
        comma-seperated list of modules to blacklist based on type

    CLI Example:

        salt '*' saltutil.sync_modules
        salt '*' saltutil.sync_modules saltenv=dev
        salt '*' saltutil.sync_modules saltenv=base,dev

原因是saltutil.sync_modules 模塊同步會默認讀取top.sls,根據配置的倉庫來同步模塊,上面top.sls 配置的是winrepo,不是base,所以同步不了base 倉庫目錄下的_modules 文件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章