首页 文章

在运行角色之前,动态运行“ansible-galaxy install -r requirement.yml -p roles /”作为先决条件

提问于
浏览
1

我试图让以下Ansible剧本工作,但无法弄清楚如何告诉Ansible:

  • 忽略检查是否存在角色

  • 运行ansible-galaxy命令以在使用之前自动加载角色

这是我的代码:

- name: Install prerequisite roles
  hosts: all
  become: no

  tasks:
    - name: run ansible galaxy
      local_action: command ansible-galaxy install -r requirements.yml -p roles/

- name: Install Oracle Java
  hosts: tomcat-servers:ss-servers:ns-servers:ld-servers
  become: yes

  roles:
    - raltd-jdk

我也尝试使用 pre_tasks 进行上述操作,但也无法正常工作 . 我的 requirements.yml 就是这样的:

- src: git@gitserver.mydom.com:ansible/raltd-jdk.git
  scm: git

我收到这个错误:

$ ansible-playbook -i inv/myinventory raltd-jdk.yml
 ERROR! the role 'raltd-jdk' was not found in /path/to/ansible/roles:/etc/ansible/roles:/path/to/ansible

 The error appears to have been in '/path/to/ansible/raltd-jdk.yml': line 16, column 7, but may
 be elsewhere in the file depending on the exact syntax problem.

 The offending line appears to be:

   roles:
       - raltd-jdk
             ^ here

参考文献

2 回答

  • 1

    我收到了Ansible开发团队的以下回复 .

    @slmingol这个特殊的错误是设计的 . 角色和任务在执行任何角色和任务之前预先加载 . 我们有持续的建议,从不同的角度解决这个问题,但没有一个尚未被接受或写入 .

    他还提到了这个提议:Proposal: Auto Install Ansible Roles

    要使用具有相应角色的最新(或甚至特定)版本的剧本,通常需要以下步骤:git pull upstream branch
    ansible-galaxy install -r path / to / rolesfile.yml -p path / to / rolesdir -f
    ansible-playbook run-the-playbook.yml
    这个过程中最容易被遗忘的步骤是中间步骤 . 虽然我们可以改进流程和文档以尝试确保不跳过此步骤,但我们可以改进ansible-playbook,以便不需要该步骤 .

    因此,在该提案得到实施之前,这似乎是不可能的 .

    参考文献

  • 1

    我也在使用requirements.yml,然后使用include_role立即运行它们 .

    include* 陈述是processed as they are encountered .

    - name: Get required roles with ansible galaxy
      local_action: command ansible-galaxy install -r requirements.yml -p roles/
    
    - name: Install some.role
      include_role: 
        name: some.role
    

相关问题