简体   繁体   中英

Adding sudo_user in ansible playbook makes command take forever

I'm using a gem called homesick to automatically symlink my dotfiles into my home directory and am using this as part of an ansible playbook to bootstrap my development environment. I have the following task to clone the dotfiles using the homesick gem and the command module.

- name: Use homesick to clone dotfiles repo
  command: homesick clone smcabrera/castillo-cabrera
  sudo_user: "{{ username }}"
  args:
    chdir: /home/{{ username }}
    creates: /home/{{ username }}/.homesick/repos/castillo-cabrera

This works just fine. Pretty straightforward--the homesick gem has a subcommand called clone which clones a dotfile repo from github. What took me a little while to figure out was that I need to sudo_user because otherwise it will use the root user and the root user's home directory (/root) and the dotfiles will be cloned there--not what I want. Using sudo_user solves this problem.

But then I try to do the same thing to carry out the symlinking and it doesn't work. See below:

- name: Use homesick to symlink dotfiles to home directory
  command: homesick symlink castillo-cabrera
  sudo_user: {{ username }}
  args:
    chdir: /home/{{ username }}
    creates: /home/{{ username }}/.zshrc

It's hard to know what's wrong because it doesn't even finish and fail so that I can see the error message--it just hangs for a half an hour before I decide to go try something else. It could be that this is just a process that takes a long time--except that if I run the same task without sudo_user it takes no time at all and I get a message.

- name: Use homesick to symlink dotfiles to home directory
  command: homesick symlink castillo-cabrera
  args:
    chdir: /home/{{ username }}
    creates: /home/{{ username }}/.zshrc

changed: [localhost] => {"changed": true, "cmd": ["homesick", "symlink", "castillo-cabrera"], "delta": "0:00:00.623685", "end": "2015-04-20 16:40:29.100744", "item": "", "rc": 0, "start": "2015-04-20 16:40:28.477059", "stderr": "", "stdout": "   identical  /root/.oh-my-zsh-custom\n   identical  /root/.irbrc\n

...(etc.)

Any idea what could be going on here?

EDIT: This is the command I've been running for the playbook:

sudo ansible-playbook -s playbook.yml -i inventory -v

Trying to do something with sudo generally requires a password, so Ansible is probably stuck at the password prompt, thinking there's more to come without realizing it has to enter something. There are two ways to tell Ansible what password to use for sudo :

  • Run ansible-playbook with the -K or --ask-sudo-pass option so that it prompts you for the password.
  • Store the password in the inventory file by adding it after the hostname like so:

     server.example.com ansible_sudo_pass=hunter2 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM