操作系统特定的pre_task

副总裁

如果目标主机上的给定操作系统是OpenBSD,我想运行一组原始命令(安装python)

我必须将这些检查作为pre_tasks运行(因为要运行任何任务,因为目标系统中必须已经存在Python)

我不明白的是,在pre_task阶段是否有任何OS特性变量可用(如ansible_os_family)

- name: Check for Python 
      raw: test -e /usr/bin/python
      changed_when: false
      failed_when: false
      register: check_python


- name: Install Python on OpenBSD
      # raw: test -e /usr/bin/apt && (apt -y update && apt install -y python-minimal) || (yum -y install python libselinux-python)
      raw: pkg_add -r python%3  
      when: check_python.rc != 0
      when: ansible_os_family == "OpenBSD" # <-- problem here

尝试使用ansible_os_family时似乎出现问题

有没有一种方法可以启用pre_tasks,而无需我编写自己的OS家庭检查?

PS。我在上面的建议下使用了上面的python安装代码:[1] https://relativkreativ.at/articles/how-to-install-python-with-ansible

弗拉基米尔·博特卡

问:“是否有一种方法可以在不编写我自己的操作系统家族检查的情况下启用预任务?”

答:不是。需要Python来收集事实


请参阅详细输出。例如从Ubuntu控制器连接FreeBSD 12远程主机的输出

shell> ansible --version
ansible 2.9.6
...
python version = 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]

shell> ansible test_01 -m setup -vvv

为用户建立SSH连接:admin SSH:EXEC ssh -C -o ControlMaster = auto -o ControlPersist = 60s -o KbdInteractiveAuthentication = no -o PreferredAuthentications = gssapi-with-mic,gssapi-keyex,基于主机的公钥-o PasswordAuthentication = no -o'User =“ admin”'-o ConnectTimeout = 30 -o ControlPath = / export / home / admin.config / .ansible / cp / 5a3ab05cf7 test_01'/ bin / sh -c'“'”'/ usr / local /bin/python3.7 &&睡眠0'“'”''

没有安装Python,模块将崩溃

(127,b'',b'/ bin / sh:/ usr / bin / python:找不到\ n')无法通过ssh连接到主机:/ bin / sh:/ usr / bin / python:找不到[警告]:找不到主机test_01的python解释器(尝试['/usr/bin/python'、'python3.7'、'python3.6'、'python3.5'、'python2.7'、'python2。 6','/ usr / libexec / platform- python','/ usr / bin / python3','python'])test_01 | 失败!=> {“ ansible_facts”:{“ discovered_interpreter_python”:“ / usr / bin / python”},“已更改”:false,“ module_stderr”:“ / bin / sh:/ usr / bin / python:找不到\ n” ,“ module_stdout”:“”,“ msg”:“模块无法正确执行,您可能需要设置解释器。\ n有关确切错误,请参见stdout / stderr。”,

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章