模块、文件同步问题

一、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
        ` 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
        ` 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 ` 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 文件

你可能感兴趣的:(模块、文件同步问题)