使用vagrant配置更改ansible role_path

Clé*_*ost 7 vagrant ansible

我有一堆ansible galaxy角色和一堆自定义角色.

问题是我无法使用Vagrantfile :ansible配置程序同时加载自定义角色和galaxy角色.

没有vagrant,这个命令很好用,因为我添加了ANSIBLE_ROLES_PATH环境变量:

 ANSIBLE_ROLES_PATH=./custom_roles ansible-playbook project.yml --inventory dev.ini -vvvv --ask-sudo-pass
Run Code Online (Sandbox Code Playgroud)

我的Vagrant配置如下:

config.vm.provision :ansible do |ansible|
   ansible.raw_arguments = ['--timeout=300']
   ansible.playbook = "project.yml"
   ansible.verbose = "vvv"
   ansible.groups = {
     "env-dev:children" => ["app-web", "app-db"],
     "app-web" => ["my-app"],
     "app-db" => ["my-app"],
   }
 end
Run Code Online (Sandbox Code Playgroud)

我可以在里面看到ansible自动加载角色,./roles但由于版本控制./roles,我无法将自定义角色添加到此目录(在我的中.gitignore)

这是目录结构,可以更好地查看正在发生的事情:

 - roles/
     - postgresql/...
     - php/...
 - custom_roles/
     - my-role-1/...
     - my-role-1/...
 - .gitignore         <- ignore ./roles
 - project.yml
 - roles.yml
 - dev.ini
 - Vagrantfile
Run Code Online (Sandbox Code Playgroud)

Tho*_*och 9

所以我遇到了同样的问题,并通过设置ANSIBLE_ROLES_PATH环境变量找到了解决方法.

Vagrantfile:

Vagrant.configure('2') do |config|
  vagrant_root = File.dirname(__FILE__)
  ENV['ANSIBLE_ROLES_PATH'] = "#{vagrant_root}/infrastructure/provisioning/ansible/roles:#{vagrant_root}/infrastructure/provisioning/ansible/galaxy-roles"

  # ansible provisioning
  config.vm.provision :ansible do |ansible|
    ansible.playbook = 'infrastructure/provisioning/ansible/playbook.yml'
    ansible.galaxy_role_file = 'infrastructure/provisioning/ansible/dependencies.yml'
    ansible.galaxy_roles_path = 'infrastructure/provisioning/ansible/galaxy-roles'
  end
end
Run Code Online (Sandbox Code Playgroud)