【问题标题】:How to use ansible to provision vim vundle plugin?如何使用 ansible 配置 vim vundle 插件?
【发布时间】:2015-11-12 13:25:46
【问题描述】:

我使用 vundle 作为 vim 的插件管理器。我想使用 ansible 来自动安装 vundle 插件。

但我无法自动进行配置:

- name: install vundle plugin
  shell: vim +PluginInstall +qall

上面是 vim 的 ansible playbook YML 文件。 当 ansible 开始运行这个任务时,它会永远持续下去,永远不会结束,永远不会失败。直到我强迫它停止CTRL C

如果我直接在来宾操作系统中运行该命令,它工作正常,vim 显示并完成安装。

这里有什么问题?

============================================
编辑:

阅读Roy Zuo的回答后,打开vim的详细模式,我尝试了以下命令:

vim -E -s -c "source ~/.vimrc" +PluginInstall +qall -V

下面是输出:

continuing in /home/vagrant/.vimrc
Searching for "/usr/share/vim/vimfiles/after/syntax/syncolor.vim"
Searching for "/home/vagrant/.vim/after/syntax/syncolor.vim"
Searching for "/home/vagrant/.vim/bundle/Vundle.vim/syntax/syncolor.vim"
Searching for "/after/syntax/syncolor.vim"
Searching for "colors/solarized.vim" in "/home/vagrant/.vim,/usr/share/vim/vimfiles,/usr/share/vim/vim74,/usr/share/vim/vimfiles/after,/home/vagrant/.vim/after,/home/vagrant/.vim/bundle/Vundle.vim,/after"
Searching for "/home/vagrant/.vim/colors/solarized.vim"
Searching for "/usr/share/vim/vimfiles/colors/solarized.vim"
Searching for "/usr/share/vim/vim74/colors/solarized.vim"
Searching for "/usr/share/vim/vimfiles/after/colors/solarized.vim"
Searching for "/home/vagrant/.vim/after/colors/solarized.vim"
Searching for "/home/vagrant/.vim/bundle/Vundle.vim/colors/solarized.vim"
Searching for "/after/colors/solarized.vim"
not found in 'runtimepath': "colors/solarized.vim"
line  188:
E185: Cannot find color scheme 'solarized'
finished sourcing /home/vagrant/.vimrc
continuing in command line

似乎 vim 在找不到 .vimrc 中指定的插件时停止了。 知道如何继续吗?

【问题讨论】:

  • 可能是环境差异造成的。您是否以与登录来宾操作系统相同的用户身份运行游戏?

标签: vim ansible vundle


【解决方案1】:

在这种情况下,您可能希望 vim 在 EX 模式下运行,以避免调出需要 tty 才能显示的可视界面。请尝试以下命令。

vim -E -s -c "source ~/.vimrc" -c PluginInstall -c qa

这里-E 告诉 vim 以 EX 模式启动,而“-s”(仅在 EX 模式下可用,help -s-ex)表示我们希望它在没有任何提示或信息性消息的情况下静默运行。此外,如果不获取运行时文件,EX 模式不知道如何执行PluginInstall 命令。

 -s         Silent or batch mode.  Only when Vim was started as "ex" or
            when preceded with the "-e" argument.  Otherwise see -s,
            which does take an argument while this use of "-s" doesn't.
            To be used when Vim is used to execute Ex commands from a file
            instead of a terminal.  Switches off most prompts and
            informative messages.  Also warnings and error messages.
            The output of these commands is displayed (to stdout):
                    :print
                    :list
                    :number
                    :set      to display option values.

=====================

至于您缺少 Solarized 配色方案,因为您已经使用 Vundle,所以很容易在您的 vimrc 中包含以下内容。

Plugin 'altercation/vim-colors-solarized'

并且你应该确保colorscheme solarized 行在它后面。

【讨论】:

  • 还请注意 ansible 可能无法扩展~,但我没有深入研究。用完整路径或 ansible 变量替换它总是很容易。
  • 我试过你的命令,但是 ansible 失败了:``` failed: [default] => {"changed": true, "cmd": "vim -E -s -c \"source ~ /.vimrc\" -c PluginInstall -c qa", "delta": "0:00:00.050793", "end": "2015-11-16 12:56:56.527921", "rc": 1, "start ": "2015-11-16 12:56:56.477128", "警告": []} ```
  • 我已尝试设置剧本,此命令按预期正确安装插件。我使用的命令是vim -E -s -c "source ~/.vimrc" -c PlugInstall -c qa,其中PlugInstall是来自vim-plug的命令。但是,返回码确实是1。直接在终端中尝试相同的命令会得到 0。我不知道发生了什么,但如果你想让你的剧本继续下去,ignore_errors: yes 可能会有所帮助。
  • 嗨,罗伊,感谢您的努力。我在 vagrant 中使用 ansible,我的客户操作系统在无头模式下运行,这可能是问题吗??
  • 应该没问题。我用 localhost ( macbook ) 运行了 ansible,并发现了同样的问题。 ignore_errors 或定义您自己的 failed_when 将有助于缓解它。
【解决方案2】:

问题是关于vim,但如果您使用neovim--headless 标志可以很好地解决这个问题。

- name: install vundle plugin
  shell: nvim +PluginInstall +qall --headless

【讨论】:

    【解决方案3】:

    以下为我解决了这个问题。我创建了一个角色来设置服务器用户环境。这包括设置 vim。我使用 Vundle,所以我也展示了如何在 ansible 中设置它。 我在执行中使用了 Roy Zuo 他的 vim 命令。当 vim 命令获取 .vimrc 文件时, echo -ne '\n' 发送一个回车。 Vim 抱怨尚未安装的插件需要我按回车键才能继续,因此这个解决方案。

    在角色/vars/main.yml:

    server_users:
      - joe
      - jane
    

    在角色/tasks/main.yml:

    - name: copy .vimrc over to server
      copy:
        src: '.vimrc_{{ item }}'
        dest: '/home/{{ item }}/.vimrc'
        owner: "{{ item }}"
        group: "{{ item }}"
        mode: '0644'
      with_items: "{{ server_users }}"
      become: true 
    
      # https://github.com/VundleVim/Vundle.vim                                                                                                     
    - name: install vim plugin handler                                                                                                            
      git:                                                                                                                                        
        repo: 'https://github.com/VundleVim/Vundle.vim.git'                                                                                       
        dest: '/home/{{ item }}/.vim/bundle/Vundle.vim'
      become_user: "{{ item }}"                                                                                         
      with_items: "{{ server_users }}"                                                                                                            
      become: true
    
    - name: install vim plugins                                                                                                                   
      shell: >                                                                                                                                  
        echo -ne '\n' | vim -E -s -c "source ~/.vimrc" -c PluginInstall -c qa            
      register: resultvim
      become_user: "{{ item }}"
      with_items: "{{ server_users }}"
      become: true
      failed_when: ( result.rc not in [0,1] )
    
    # optional for debugging
    - debug:                                                                                                                                      
      msg: "{{ resultvim }}"
    

    我将服务器用户的 vimrc 文件保存在这里

    role/files/:
    .vimrc_joe
    .vimrc_jane
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多