Ansible 手册:
cat sitecode_refresh.yml
---
- name: Run sitecode_refresh.py
hosts: localhost
vars_file:
- credential.json
tasks:
- name: Run sitecode refresh script
no_log: false
shell: |
scl enable python27 bash &&
which pip &&
pip2 install virtualenv=="16.7.10" &&
virtualenv -p /usr/bin/python2 sitecoderefresh &&
source sitecoderefresh/bin/activate &&
pip2 install -r requirements.txt &&
python sitecode_refresh.py --spreadsheet_id="abcxyz" --worksheet_name="Testing" &&
deactivate
...
运行它会产生如下错误。
ansible-playbook -k -K sitecode_refresh.yml --ask-vault-pass
"Traceback (most recent call last):", " File \"sitecode_refresh.py\", line 165, in <module>", " main()", " File \"sitecode_refresh.py\", line 145, in main", " client = authenticate_to_gspread()", " File \"sitecode_refresh.py\", line 96, in authenticate_to_gspread", " flow = InstalledAppFlow.from_client_secrets_file(CREDENTIALS_PATH, SCOPES)", " File \"/home/aagashe/dev/scripts/sitebuilder/sitecoderefresh/lib/python2.7/site-packages/google_auth_oauthlib/flow.py\", line 197, in from_client_secrets_file", " client_config = json.load(json_file)", " File \"/usr/lib64/python2.7/json/__init__.py\", line 290, in load", " **kw)", " File \"/usr/lib64/python2.7/json/__init__.py\", line 338, in loads", " return _default_decoder.decode(s)", " File \"/usr/lib64/python2.7/json/decoder.py\", line 366, in decode", " obj, end = self.raw_decode(s, idx=_w(s, 0).end())", " File \"/usr/lib64/python2.7/json/decoder.py\", line 384, in raw_decode", " raise ValueError(\"No JSON object could be decoded\")", "ValueError: No JSON object could be decoded"
提供了加密的credential.json,但它仍然给我错误,如上所述。
您在尝试安装 virtualenv 软件包时似乎遇到了 Ansible playbook 问题。该错误消息表明 Ansible 无法找到用于安装的 pip2 或 pip。
要解决此问题,您可以尝试以下步骤:
指定 Pip 可执行文件:您可以显式指定要在 Playbook 的 pip 模块中使用的 pip 可执行文件。这是一个例子:
- name: Install virtualenv
pip:
name: virtualenv
version: "16.7.10"
executable: pip2
在本例中,我们使用 pip2 作为可执行文件。调整此项以匹配您系统上适当的可执行文件。
检查 Pip 安装:确保目标计算机上安装了所需版本的 pip。如果未安装,您可能需要手动安装。
环境变量:Ansible 模块在自己的环境中运行,任务内环境变量的更改不会在任务之间持续存在。要为整个剧本设置环境变量,请在剧本级别使用环境关键字。例如:
- name: Run sitecode_refresh.py
hosts: localhost
environment:
PATH: "/path/to/python2/bin:/path/to/pip2/bin:{{ ansible_env.PATH }}"
tasks:
# Your tasks here
将 /path/to/python2/bin 和 /path/to/pip2/bin 替换为 Python 2 和 pip 2 安装的实际路径。
用户权限:确保运行 Ansible playbook 的用户拥有执行 playbook 中操作(包括软件包安装)所需的权限。
使用 Ansible 模块:不要使用命令模块来激活虚拟环境和运行命令,而是考虑使用相关的 Ansible 模块来执行这些任务,例如 activate_virtualenv 和 pip 模块。这可以带来更好的集成和环境处理。