如何根据用户输入制作一个ansible剧本?

如何根据用户输入制作一个ansible剧本?,ansible,ansible-awx,Ansible,Ansible Awx,在我的ansible playbook中,我从用户那里获取了2个输入,我还想获取第三个输入,该输入有时是可选的,如果用户为var3提供了值,则playbook必须执行一个任务,否则它不应该执行,那么实现这一点的方法是什么 我还想知道,我正在使用ansible的awx开源UI,所以我选择了主机来运行ansible awx清单中的playbook,之后我应该在playbook的“主机”中写些什么,或者可以不写 - name: Updating "{{ service_name }}" serve

在我的ansible playbook中,我从用户那里获取了2个输入,我还想获取第三个输入,该输入有时是可选的,如果用户为var3提供了值,则playbook必须执行一个任务,否则它不应该执行,那么实现这一点的方法是什么

我还想知道,我正在使用ansible的awx开源UI,所以我选择了主机来运行ansible awx清单中的playbook,之后我应该在playbook的“主机”中写些什么,或者可以不写

 - name: Updating "{{ service_name  }}" server codebase and starting its service.
   hosts: all
   tasks:
    - name: Stopping nginx service
      command: sudo service nginx stop


    - name: Performing git checkout in the specified directory "{{ path  }}"
      command: git checkout .
      args:
        chdir: "{{ path }}"

    - name: Running npm install in the directory "{{ path }}"
      command: npm install
      args:
        chdir: "{{ path }}/node_modules"


    - name: Restarting the "{{ service_name }}" service
      command: sudo service "{{ service_name }}" restart

    - name: Restarting the nginx service
      command: sudo service nginx restart

此实例中的用户是谁?你呢?如果您是用户,则可以运行

ansible-playbook -i hosts <your-playbook> -e "service_name=<yourservice>"
ansible playbook-i hosts-e“服务名称=”
在playbook执行时动态更改service_name变量

然后,您也可以将第二个变量添加到命令中,但请注意“可选”第三个变量,因为我确信如果您没有引用playbook中的所有变量,您将得到一个错误

编辑:当您执行ansible playbook命令时,您将需要引用服务名称和路径变量,其中第三个变量似乎不在您提供的代码示例中