How to automatically install Ansible Galaxy roles using Vagrant?

Using only one book, then it is not possible for Ansible to automatically install dependent roles. At least in accordance with this stream of SO .

But I have the added “advantage” of using Vagrant and Vagrant Ansible local Proviser . What tricks can I apply?

+4
source share
1 answer

Something like this ( Vagrantfile):

config.vm.provision 'ansible', run: 'always', type: :ansible_local do |ansible|
  ansible.galaxy_role_file = 'requirements.yml'
  ansible.playbook = 'playbook.yml'
end

Content requirements.yml:

---
- src: mongrelion.docker

, 1 , . Vagrantfile, script, Ansible, Vagrant , . , Vagrant Ansible, "script", , Ansible lol.

ansible-galaxy install ; , (docs).

, Vagrant, , galaxy_command, , Vagrant ( ).

inline, nitehacked script Vagrantfile, , holla at me =)

, Ansible?

Ansible Galaxy : ./roles/. Ansible automagically -.

, , - . , Galaxy, galaxy_roles_path , Ansible : /etc/ansible/roles/.

.

[ , :] Vagrant Ansible, root. I.e., ansible-galaxy install 1 ​​ , - . , -, Ansible " ". .

, chmod , Ansible :

config.vm.provision 'preemptively give others write access to /etc/ansible/roles', type: :shell, inline: <<~'EOM'
  mkdir /etc/ansible/roles -p
  chmod o+w /etc/ansible/roles
EOM

config.vm.provision 'ansible', run: 'always', type: :ansible_local do |ansible|
  ansible.galaxy_role_file = 'requirements.yml'
  ansible.galaxy_roles_path = '/etc/ansible/roles'
  ansible.playbook = 'playbook.yml'
end

?

, , Ansible Provision . , . , vagrant up . .

galaxy_command --force - ? . . :

[]: - mongrelion.docker() - --force

tiring. I should be flattered, not warned;) Did the tramp add a flag --forceto suppress this warning? By accident, I found another solution. Add an explicit version tag ( docs ) to the role definition and voila, the warning is replaced with something nice instead:

- mongrelion.docker (6a4fe8fc18550bfff8eeecd89888cf817cdf4bfc) is already installed, skipped.

If you are still receiving a warning message, it is because you had another version installed before you explicitly added the version tag. So, now you should use --forceat least once to force update or manually delete the old ( ansible-galaxy remove stupid.role).

Having said all of the above, this is the final content that I ended up in.

Vagrantfile:
config.vm.provision 'preemptively give others write access to /etc/ansible/roles', type: :shell, inline: <<~'EOM'
  sudo mkdir /etc/ansible/roles -p
  sudo chmod o+w /etc/ansible/roles
EOM

config.vm.provision 'ansible', run: 'always', type: :ansible_local do |ansible|
  ansible.galaxy_role_file = 'requirements.yml'
  ansible.galaxy_roles_path = '/etc/ansible/roles'
  ansible.galaxy_command = 'ansible-galaxy install --role-file=%{role_file} --roles-path=%{roles_path}'
  ansible.playbook = 'playbook.yml'
end
requirements.yml:

---
- src: mongrelion.docker
  : 6a4fe8fc18550bfff8eeecd89888cf817cdf4bfc

+8

Source: https://habr.com/ru/post/1685054/


All Articles