简体   繁体   English

Ansible:更改剧本位置

[英]Ansible: Change playbooks location

I have all playbooks in /etc/ansible/playbooks and I want to execute them anywhere on the pc我在 /etc/ansible/playbooks 中有所有剧本,我想在电脑上的任何地方执行它们

I tried to configure playbook_dir variable in ansible.cfg我试图在 ansible.cfg 中配置 playbook_dir 变量

[defaults]
playbook_dir = /etc/ansible/playbooks/

and tried to put ANSIBLE_PLAYBOOK_DIR variable in ~/.bashrc并尝试将 ANSIBLE_PLAYBOOK_DIR 变量放入 ~/.bashrc

export ANSIBLE_PLAYBOOK_DIR=/etc/ansible/playbooks/

but I only got the same error in both cases:但在这两种情况下我都得到了同样的错误:

nor@nor:~$ ansible-playbook test3.yaml
ERROR! the playbook: test3.yaml could not be found

This is my ansible version:这是我的 ansible 版本:

ansible 2.9.7
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/nor/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.7/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.7.3 (default, Oct  7 2019, 12:56:13) [GCC 8.3.0]

Does anyone know the problem and how to solve it?有谁知道这个问题以及如何解决它?

According to https://manpages.debian.org/testing/ansible/ansible-inventory.1.en.html :根据https://manpages.debian.org/testing/ansible/ansible-inventory.1.en.html

--playbook-dir 'BASEDIR' --playbook-dir '基础'

Since this tool does not use playbooks, use this as a subsitute playbook directory.This sets the relative path for many features including roles/ group_vars/ etc.由于此工具不使用 playbook,因此将其用作替代 playbook 目录。这设置了许多功能的相对路径,包括角色/ group_vars/ 等。

This means that ANSIBLE_PLAYBOOK_DIR is not used as a replacement for specifying the the absolute / relative path to your playbook, but it tells the playbook where it should look for roles, host/group vars, etc.这意味着 ANSIBLE_PLAYBOOK_DIR 不能用作指定 playbook 的绝对/相对路径的替代品,但它会告诉 playbook 它应该在哪里查找角色、主机/组变量等。

The goal you're trying to achieve is has no solution on the ansible side, you need to achieve this by configuring your shell profile accordingly.您要实现的目标是在 ansible 方面没有解决方案,您需要通过相应地配置 shell 配置文件来实现这一目标。 set the following in your.bashrc file:在 your.bashrc 文件中设置以下内容:

export playbooks_dir=/path/to/playbooks

when you call the playbook use ansible-playbook $playbooks_dir/test3.yml当您调用剧本时,请使用ansible-playbook $playbooks_dir/test3.yml

PLAYBOOK_DIR says: PLAYBOOK_DIR说:

"A number of non-playbook CLIs have a --playbook-dir argument; this sets the default value for it." “许多非剧本 CLI 都有一个 --playbook-dir 参数;这会为其设置默认值。”

Unfortunately, there is no hint in the doc what "the non-playbook CLIs" might be.不幸的是,文档中没有暗示“非剧本 CLI”可能是什么。 ansible-playbook isn't one of them, obviously.显然,ansible-playbook 不是其中之一。


FWIW. FWIW。 If you're looking for a command-line oriented framework try ansible-runner . 如果您正在寻找面向命令行的框架,请尝试ansible-runner For example, export the location of private_data_dir 比如导出private_data_dir的位置

shell> export ansible_private=/path/to/<private-data-dir>

Then run the playbook然后运行剧本

shell> ansible-runner -p playbook.yml run $ansible_private

As others have said, ANSIBLE_PLAYBOOK_DIR is for setting the relative directory for roles/, files/, etc. IMHO, it's not terribly useful.正如其他人所说,ANSIBLE_PLAYBOOK_DIR 用于设置角色/、文件/等的相对目录。恕我直言,它并不是非常有用。

If I understand the op, this is how I accomplish a similar result with all versions of ansible...如果我了解操作,这就是我使用 ansible 的所有版本实现类似结果的方式...

PPWD=$PWD cd /my/playbook/dir && ansible-playbook my_playbook.yml; cd $PPWD

Explained,解释说,

PPWD=$PWD is to remember the current/present/previous working directory, then cd /my/playbook/dir and if that succeeds run ansible-playbook my_playbook.yml (everything is relative from there); PPWD=$PWD 是记住当前/当前/上一个工作目录,然后 cd /my/playbook/dir 如果成功运行 ansible-playbook my_playbook.yml (一切都是相对的); regardless, always change back to the previous working directory无论如何,始终更改回以前的工作目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM