当conda没有安装时,我的剧本运行良好,但如果安装了它,就会遇到以下错误:
TASK [Ensure `base` environment uses Python 3.9] ********************************
致命:[my_machine]:失败=>{quot;msg":quot;在command_output.stdout中条件检查"未找到"失败。错误为:计算条件时出错(在command_output.sstdout中未找到(:"dict-object"没有属性"stdout"\n\n错误出现在"/Users/ansible/tasks/install-miniconda.yaml":第25行,第3列,但根据确切的语法问题,可能出现在文件的其他位置。\n \n有问题的行似乎是:\n \n-name:确保
base
环境使用Python 3.9 \n ^此处\n"}
如果发现已安装conda,它将跳过此步骤之前的任务,并在此处失败。
- import_tasks: tasks/check-command.yaml
vars:
command: conda
- import_tasks: tasks/install-miniconda.yaml
vars:
install_dir: "~/"
when: "'not found' in command_output.stdout"
---
- name: Download miniconda
ansible.builtin.get_url:
url: https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
dest: "{{ install_dir }}miniconda.sh"
mode: 0644
- name: Setup .local/opt
ansible.builtin.file:
path: ~/.local/opt
state: directory
mode: u=rwx,go=rx
- name: Install miniconda # noqa no-changed-when
ansible.builtin.shell: |
/bin/bash {{ install_dir }}miniconda.sh -b -p ~/.local/opt/miniconda3
. ~/.local/opt/miniconda3/bin/activate
conda init bash
- import_tasks: login-shell.yaml
vars:
name: Check `base` environment Python version
command: python --version
- name: Ensure `base` environment uses Python 3.9
ansible.builtin.command: conda install -n base python=3.9
when: 'command_output.stdout is not regex("^Python 3.9.d+$")'
- name: Cleanup conda install script
ansible.builtin.file:
path: "{{ install_dir }}miniconda.sh"
state: absent
还有更多问题:
-
错误为:'dict对象'没有属性"stdout">。看一看字典,把它修好。
-
出于某种原因,Python2将版本写入stderr。例如,
- command: python --version
register: command_output
- debug:
var: command_output
给出
TASK [debug] *****************************************************
ok: [localhost] =>
command_output:
ansible_facts:
discovered_interpreter_python: /usr/bin/python3
changed: true
cmd:
- python
- --version
delta: '0:00:00.004342'
end: '2022-08-12 22:51:57.467916'
failed: false
msg: ''
rc: 0
start: '2022-08-12 22:51:57.463574'
stderr: Python 2.7.18
stderr_lines:
- Python 2.7.18
stdout: ''
stdout_lines: []
因此,以下条件将始终true
- debug:
msg: Run conda install -n base python=3.9
when: 'command_output.stdout is not regex('^Python 3.9.d+$')'
通过使用shell并将stderr重定向到stdout来修复它。例如,
- shell: python --version 2>&1
register: command_output
- debug:
var: command_output
给出
TASK [debug] *****************************************************
ok: [localhost] =>
command_output:
changed: true
cmd: python --version 2>&1
delta: '0:00:00.004157'
end: '2022-08-12 22:57:10.841659'
failed: false
msg: ''
rc: 0
start: '2022-08-12 22:57:10.837502'
stderr: ''
stderr_lines: []
stdout: Python 2.7.18
stdout_lines:
- Python 2.7.18
- 使用版本测试版本。例如
- name: Ensure base environment uses Python 3.9
debug:
msg: Run conda install -n base python=3.9
when: python_version is version('3.9', '<')
vars:
python_version: "{{ command_output.stdout.split()|last }}"
- 通常,系统中可能安装了更多版本的Python。如果要查找ansible使用的版本,请使用变量ansible_python_version。否则,请确保命令
python --version
中的路径符合您的要求