如何在Ansible中本地运行一些任务

问题描述 投票:0回答:1

我有一个剧本,里面有一些要在远程主机上执行的rolestasks。有一个场景,我想让一些任务在本地执行,比如从svnnexus下载工件到本地服务器。

下面是我的主剧本,我从命令行传递target_env,并使用group_vars目录动态加载变量。

---
 - name: Starting Deployment of Application to tomcat nodes
   hosts: '{{ target_env }}'
   become: yes
   become_user: tomcat
   become_method: sudo
   gather_facts: yes
   roles:
     - role: repodownload
       tags:
         - repodownload
     - role: stoptomcat
       tags:
         - stoptomcat

第一个角色repodownload实际上是从svnnexus下载工件到本地servercontroller。这里是这个角色的main.yml--。

 - name: Downloading MyVM Artifacts on the local server
   delegate_to: localhost
   get_url: url="http://nexus.com/myrelease.war" dest=/tmp/releasename/

 - name: Checkout latest application configuration templates from SVN repo to local server
   delegate_to: localhost
   subversion:
     repo: svn://12.57.98.90/release-management/config
     dest: ../templates
     in_place: yes

但它不工作。难道是因为在我的主yml文件中,我变成了用户,我想用它在远程主机上执行命令。

如果有人能帮助我,请告诉我。这将是感激的。

ERROR -

    "changed": false,
    "module_stderr": "sudo: a password is required\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 1
}
ansible ansible-inventory
1个回答
0
投票

鉴于这个场景,你可以用多种方式来实现。其中一种方法是在远程主机上添加另一个玩法 repodownload 角色到你的主剧本,只在localhost上运行。然后删除 delegate_to: localhost 的角色任务中,并相应地移动变量。

 ---
 - name: Download repo
   hosts: localhost
   gather_facts: yes
   roles:
     - role: repodownload
       tags:
         - repodownload

 - name: Starting Deployment of Application to tomcat nodes
   hosts: '{{ target_env }}'
   become: yes
   become_user: tomcat
   become_method: sudo
   gather_facts: yes
   roles:
     - role: stoptomcat
       tags:
         - stoptomcat

另一种方法是删除 become 从游戏等级添加到角色 stoptomcat. 类似下面的东西应该可以用。

 ---
 - name: Starting Deployment of Application to tomcat nodes
   hosts: '{{ target_env }}'
   gather_facts: yes
   roles:
     - role: repodownload
       tags:
         - repodownload
     - role: stoptomcat
       become: yes
       become_user: tomcat
       become_method: sudo
       tags:
         - stoptomcat

还没有测试代码,所以如果有任何格式问题,请原谅。

© www.soinside.com 2019 - 2024. All rights reserved.