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

Dynamically run "ansible-galaxy install -r requirement.yml -p roles/" as a prerequisite before running a role

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

这是我的代码:

- 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

参考资料

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

@slmingol this particular error is by design. Roles and tasks are preloaded before any of them are executed. We have ongoing proposals that tackle this problem from different angles, but none of them have yet to be accepted or written.

他还引用了这个提案:Proposal: Auto Install Ansible Roles

To use the latest (or even a specific) version of a playbook with the appropriate roles, the following steps are typically required:

git pull upstream branch
ansible-galaxy install -r path/to/rolesfile.yml -p path/to/rolesdir -f
ansible-playbook run-the-playbook.yml

The most likely step in this process to be forgotten is the middle step. While we can improve processes and documentation to try and ensure that this step is not skipped, we can improve ansible-playbook so that the step is not required.

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

参考资料

我也在使用 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