Ubuntu ansible:sudo-iu用于交互式shell

Ubuntu ansible:sudo-iu用于交互式shell,ubuntu,automation,ansible,Ubuntu,Automation,Ansible,当需要交互式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 }} whe

当需要交互式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

您可以请求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}}
    不会被插值
  • 您可以用更自然的
    x未定义
    替换
    x未定义
  • 您可能希望在以下时间添加“
    changed\u:
    以控制命令的更改状态,因此您的TAK是幂等的(但似乎只有在某个目录不存在时才运行任务,因此我猜只能跳过任务状态/
    更改

这里需要阅读.bash_配置文件来设置环境?是的,就像我通常使用ssh连接到服务器一样。噢
bash-lc
是的,完全忘记了这一点:)。
bash-lc
对我不起作用,但
bash-lci
起作用。