如何使用 Vagrant 自动安装 Ansible Galaxy 角色? [英] How to automatically install Ansible Galaxy roles, using Vagrant?

查看:27
本文介绍了如何使用 Vagrant 自动安装 Ansible Galaxy 角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅使用一本剧本,则不可能让 Ansible 自动安装依赖角色.至少根据这个SO线程.

但是,我有使用 Vagrant 和 Vagrant 的 Ansible 本地配置器 的附加优势".我可以应用任何技巧吗?

解决方案

更新 2018-11-22!

鉴于软件的发展,我不能保证下面的所有旧东西/答案"仍然合法并且不会让您的机器着火哈哈.但是,我确实维护了一个 GitHub 上的 Vagrantfile 来安装 Ansible Galaxy角色自动和这个家伙基本上应该被视为唯一的和神圣的真理(在文件中向下滚动一点).如果 GitHub-guy-file 对你不起作用,请在 GitHub 跟踪器上提交问题,我会注意的.

旧东西/答案..

类似这样的东西(Vagrantfile):

config.vm.provision 'ansible',运行:'always',输入::ansible_local do |ansible|ansible.galaxy_role_file = 'requirements.yml'ansible.playbook = 'playbook.yml'结尾

requirements.yml 的内容:

<块引用>

---
- 源代码:mongrelion.docker

请注意我的 1 角色是如何在单独的文件中指定的,这完全是矫枉过正.我想在 Vagrantfile 中指定角色,而不需要添加另一个 shell 脚本来在 Vagrant 有机会之前安装 Ansible 然后运行 ​​Galaxy install 命令.我发现让 Vagrant 安装 Ansible 很方便,而且当 Ansible 应该是供应商时,我投入的每一个额外的脚本"都让我感觉很糟糕,哈哈.

对于记录,ansible-galaxy install 不需要指定角色file;角色 name 作为命令的参数就足够了,在这种情况下,角色将被下载 (文档).

我尝试了大约半天来破解和削减可能设置的各种 Vagrant 选项,例如 galaxy_command 但是每一个新的诡计都会引起一个新的问题,这让 Vagrant 很难停下来(听起来很熟悉??哈哈哈).

如果你找到了一种方法来设置角色内联而不依赖于 Vagrantfile 中的另一个文件或被黑的 shell 脚本,请对我大喊大叫 =)

如何更改 Ansible 角色文件的存储位置?

默认情况下,Ansible Galaxy 将角色文件下载到项目中的子目录:./roles/.Ansible 在解析 playbook 文件时会自动在此子目录中查找角色.

我觉得这个位置不方便,因为我的个人目标是保持项目文件夹干净并尽可能少地向我的存储库提交垃圾.幸运的是,可以通过设置 galaxy_roles_path 到 Ansible 也用来在以下位置查找角色的另一个位置:/etc/ansible/roles/.

要是这么简单就好了.

[至少在我的机器上:] 当 Vagrant 安装 Ansible 时,创建的文件夹只对 root 用户具有写权限.即,当 ansible-galaxy install 在 1 分钟后运行并下载角色时,由于权限不足,一切都崩溃了.或者换一种说法,Ansible 不能把狗屎放在他自己的主文件夹"中.其实有点好笑.

解决方法是在 Ansible 自己搞砸之前向文件夹注入一些 chmod 魔法:

config.vm.provision '抢先给予其他人对/etc/ansible/roles 的写访问权限',输入::shell,内联:<<~'EOM'mkdir/etc/ansible/roles -pchmod o+w/etc/ansible/rolesEOMconfig.vm.provision 'ansible', 运行: 'always', 输入: :ansible_local do |ansible|ansible.galaxy_role_file = 'requirements.yml'ansible.galaxy_roles_path = '/etc/ansible/roles'ansible.playbook = 'playbook.yml'结尾

如何阻止角色不断重新安装?

我发现让 Ansible 供应商始终运行是一个很好的做法.这样他就可以做他的事,可以这么说.终端输出显示 vagrant up 在每次运行时下载并安装角色.这让我很烦.

galaxy_command 默认使用--force 标志 - 为什么?不知道.但是删除它可以解决问题.现在我们收到一个警告:

<块引用>

[警告]: - mongrelion.docker (master) 已经安装 - 使用 --force将版本更改为未指定

好累.我应该感到受宠若惊,而不是警告;) Vagrant 是否成功添加了 --force 标志来抑制此警告?偶然地,我找到了另一个解决方案.将显式版本标记(docs)添加到角色定义中,瞧,警告被替换为可爱的东西:

<块引用>

- mongrelion.docker (6a4fe8fc18550bfff8eeecd89888cf817cdf4bfc) 已经已安装,跳过.

如果您仍然收到警告消息,那是因为您在显式添加版本标记之前已经安装了另一个版本.所以,现在你必须至少使用一次 --force 来强制更新或手动删除旧的(ansible-galaxy remove paste.role).

说了这么多,这就是我最后的内容了..

流浪文件:

config.vm.provision '抢先给予其他人对/etc/ansible/roles 的写访问权限',输入::shell,内联:<<~'EOM'须藤 mkdir/etc/ansible/roles -p须藤 chmod o+w/etc/ansible/rolesEOMconfig.vm.provision 'ansible', 运行: 'always', 输入: :ansible_local do |ansible|ansible.galaxy_role_file = 'requirements.yml'ansible.galaxy_roles_path = '/etc/ansible/roles'ansible.galaxy_command = 'ansible-galaxy 安装 --role-file=%{role_file} --roles-path=%{roles_path}'ansible.playbook = 'playbook.yml'结尾

要求.yml:<块引用>

---
- 源代码:mongrelion.docker
  版本:6a4fe8fc18550bfff8eeecd89888cf817cdf4bfc

Using one playbook only, then it's not possible to have Ansible automagically install the dependent roles. At least according to this SO thread.

But, I have the added "advantage" of using Vagrant and Vagrant's Ansible local provisioner. Any tricks I may apply?

解决方案

Update 2018-11-22!

Given the evolution of software, I can not guarantee that all of the "old stuff/answer" below is still legit and won't set your machine on fire lol. I do, however, maintain a Vagrantfile on GitHub that does install Ansible Galaxy roles automatically and this guy should essentially be regarded as the only one and holy truth (scroll down a bit in the file). If the GitHub-guy-file doesn't work for ya, toss in an issue on the GitHub tracker and I'll see to it.

Old stuff/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

Contents of requirements.yml:

---
- src: mongrelion.docker

Please note how my 1 role is specified in a separate file, which is totally overkill. I would like to specify the role in the Vagrantfile, without adding yet another shell script that install Ansible before Vagrant has had a chance and then run the galaxy install command. I find it convenient to let Vagrant install Ansible and it just feels bad for every extra "script" I throw in when Ansible is supposed to be the provisioner lol.

For the records, ansible-galaxy install does not require a role file to be specified; a role name as an argument to the command would suffice in which case the role would be downloaded (docs).

I tried about half a day to hack and slash various Vagrant options one may set, such as the galaxy_command but each new trickery caused a new problem which was a hard stop for Vagrant (sounds familiar?? hahaha).

If you find a way to set the role inline without depending on yet another file or a nitehacked shell script in the Vagrantfile, please holla at me =)

How do I change where the Ansible role files get stored?

By default, Ansible Galaxy downloads the role files to a subdirectory in the project: ./roles/. Ansible automagically looks for roles in this subdirectory when parsing the playbook file.

I find this location to not be convenient, given my personal goal to keep project folders clean and commit as little junk as possible to my repository. Thankfully, it is possible to change Galaxy's download path by setting galaxy_roles_path to another location that Ansible also uses to look for roles in: /etc/ansible/roles/.

If only it was that easy.

[At least on my machine:] When Vagrant installs Ansible, the folder is created with write-permissions only for the root user. I.e., when ansible-galaxy install run 1 minute later and downloads the role, everything crash because of insufficient permissions. Or to put it differently, Ansible can't put shit in his own "home folder". That's kind of funny actually.

The fix is to inject some chmod magic to the folder before Ansible manages to screw up for himself:

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

How do I stop the role from constantly being reinstalled?

I find it to be a good practice to let the Ansible provisioner always run. So that he can do his thing, so to speak. The terminal output reveals that vagrant up downloads and install the role on each run. This annoys the shit out of me.

The galaxy_command uses by default the --force flag - why? Have no idea. But removing it solves the problem. Now we get a warning instead:

[WARNING]: - mongrelion.docker (master) is already installed - use --force to change version to unspecified

Tiresome. I should be flattered, not warned ;) Vagrant prolly added the --force flag to suppress this warning? By random accident, I found another solution. Add an explicit version tag (docs) to the role definition and voila, warning is replaced with something cute instead:

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

If you still get the warning message, then that's cuz you had another version installed already before adding the version tag explicitly. So, now you must use --force at least once to force the update or manually remove the old (ansible-galaxy remove stupid.role).

Having said all of the above, this is the final contents I ended up with..

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
  version: 6a4fe8fc18550bfff8eeecd89888cf817cdf4bfc

这篇关于如何使用 Vagrant 自动安装 Ansible Galaxy 角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆