17

I use vundle as plugin manager for vim. And I want to use ansible to automate vundle plugin installation.

But I just can't get ansible to do provision automatically:

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

above is the ansible playbook YML file for vim. When ansible start to run this task, it just goes on forever, it never ends and it never fails. Until I force it to stop by CTRL C.

If I run that command directly in the guest os, it works fine, vim shows up and finishes installation.

What's the problem here?

==========================================
Edit:

After read Roy Zuo's answer, and turn on verbose mode of vim, I tried the following command:

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

and below is the output:

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

It seems vim stopped when it can't find the plugin specified in .vimrc. Any idea how to continue?

Aaron Shen
  • 8,124
  • 10
  • 44
  • 86
  • Could be do to environment differences. Are you running your play as the same user that you are logging into the guest OS as? – Petro026 Nov 12 '15 at 17:53

3 Answers3

10

You would want vim in this case to run in EX mode which avoids bringing up the visual interface which requires a tty for display. Try the following command instead.

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

Here -E tells vim to start in EX mode, and "-s" (only available in EX mode, help -s-ex) means we want it to run silently without any prompts or informative messages. Moreover, without sourcing your runtime file, EX mode does not know how to execute PluginInstall command.

 -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.

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

As for your Solarized color scheme missing, since you are already with Vundle, it is easy to have the following in your vimrc.

Plugin 'altercation/vim-colors-solarized'

and you should make sure colorscheme solarized line come after it.

Roy Zuo
  • 146
  • 6
  • Please also be reminded ansible might not be able to expand `~`, but I have not delve into it. It is always easy to replace it with full path or an ansible variable. – Roy Zuo Nov 16 '15 at 11:57
  • I tried your command, but ansible fails: ``` 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", "warnings": []} ``` – Aaron Shen Nov 16 '15 at 13:00
  • I have tried setting up a playbook and this command correctly installs plugins as expected. The command I used was `vim -E -s -c "source ~/.vimrc" -c PlugInstall -c qa`, of which `PlugInstall` is the command from [vim-plug](https://github.com/junegunn/vim-plug). However, the return code is indeed **1**. Trying the same command directly in terminal gives a **0**. I do not know what is going on, but if you want your playbook to continue, a `ignore_errors: yes` might help. – Roy Zuo Nov 16 '15 at 22:37
  • Hi Roy, thanks for your effort. I'm using ansible with vagrant, my guest os is running in a headless mode, could that be the problem?? – Aaron Shen Nov 17 '15 at 01:39
  • It should not be a problem. I ran ansible with localhost ( a macbook ), and spot the same issue. A `ignore_errors` or define your own `failed_when` would be of some help to mitigate it. – Roy Zuo Nov 17 '15 at 03:40
  • @AaronShen I had the same problem. I resolved the issue using ```become``` and ```become_user``` in the task. – ariel17 Jun 25 '17 at 00:57
  • You may also add `silent! colorscheme solarized` in your `.vimrc`. Also, if you include `+visual` you'll avoid the exit `1` error code (at least, this is the case for vim-plug). Try: `vim -E -s -c "source ~/.vimrc" +PluginInstall +visual +qall; echo $?` == `0`. – brettinternet Nov 20 '22 at 19:42
0

The question is asking about vim, but if you're using neovim the --headless flag solves this nicely.

- name: install vundle plugin
  shell: nvim +PluginInstall +qall --headless
gluxon
  • 750
  • 1
  • 7
  • 16
0

The following solves it for me. I created a role to setup server user environments. So that includes setting up vim. I use Vundle so I show how to set it up in ansible too. I used Roy Zuo his vim command in the implementation. The echo -ne '\n' sends an enter when the vim command sources the .vimrc file. Vim complained about the plugins that weren't installed yet requiring me to press enter to continue, hence this solution.

in role/vars/main.yml:

server_users:
  - joe
  - jane

in role/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 }}"

I keep server user their vimrc files here

role/files/:
.vimrc_joe
.vimrc_jane