如何使用ansible来配置vim vundle插件?

时间:2015-11-12 13:25:46

标签: vim ansible vundle

我使用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

当它无法找到.vimrc中指定的插件时,似乎停止了vim。 知道怎么继续吗?

3 个答案:

答案 0 :(得分:8)

在这种情况下,您希望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行跟在它后面。

答案 1 :(得分:0)

问题是询问vim,但是如果您使用的是neovim,则--headless标志可以很好地解决此问题。

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

答案 2 :(得分:0)

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

角色/vars/main.yml:

server_users:
  - joe
  - jane

在角色/任务/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