2016-02-17 40 views
2

我有一个任务文件有很多重复。这些shell命令中的每一个都倾向于需要在虚拟环境内,特定工作目录内以及特定用户下工作。例如,我的任务往往是这样的:避免重复在Ansible任务列表中的多个命令

- name: Build thing 
    shell: source ~/project/venv/bin/activate; ./thing build chdir=~/project 
    sudo_user: "{{ thing_user }}" 

- name: Register thing 
    shell: source ~/project/venv/bin/activate; ./thing register chdir=~/project 
    sudo_user: "{{ thing_user }}" 

有什么方法可以让我避免在任务级重复自己?理想情况下,我可以一次声明工作目录,虚拟环境和sudo_users。这对我来说很难在角色或剧本级别上做。

回答

2

由于它们之间唯一的区别是,你可以使用Loops命令所以它会成为

- name: Build thing 
    shell: source ~/project/venv/bin/activate; ./thing {{ item }} chdir=~/project 
    sudo_user: "{{ thing_user }}" 
    with_items: 
    - build 
    - register