当需要交互式shell时,是否有更好的方法为ansible中的每个命令编写sudo -iu username -command-?
示例:
- name: Install nodejs {{ node_version }}
shell: sudo -iu {{ nvm_user }} nvm install {{ node_version }} && sudo -iu {{ nvm_user }} nvm alias default {{ node_version }}
when: not np.stat.isdir is defined发布于 2014-09-05 09:30:27
您可以要求Ansible为您提供sudo,并直接调用bash,如下所示:
- name: Install nodejs {{ node_version }}
sudo_user: "{{ nvm_user }}"
sudo: true
shell: bash -lc 'nvm install {{ node_version }} && nvm alias default {{ node_version }}'
when: np.stat.isdir is not defined几个注意事项:
Install nodejs {{ node_version }}不会被内插not x is defined替换为更自然的x is not definedchanged_when:‘来控制您的命令的更改状态,因此您的taks是幂等的(但似乎只在某个目录不存在时才运行该任务,因此我猜任务状态只能是skipped/changed)。https://stackoverflow.com/questions/25681654
复制相似问题