如何在bash文件中将kubespray动态清单的配置变量传递给python?

2024-05-13 06:13:12 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我试图创建一个bash脚本来传递为ansible创建清单文件所需的IP数组 官方文件说,这是通过

declare -a IPS=(10.10.1.3 10.10.1.4 10.10.1.5)
CONFIG_FILE=inventory/mycluster/hosts.yaml python3 contrib/inventory_builder/inventory.py ${IPS[@]}

但是,在bash脚本中CONFIG_FILE被设置为变量,因此它会停止创建清单文件,因为变量没有传递到python文件中

我尝试了以下方法,尝试将变量传递给python文件,以创建清单文件

declare -a IPS=(10.10.1.3 10.10.1.4 10.10.1.5)
CONFIG_FILE=kubespray/inventory/mycluster/hosts.yaml
python3 kubespray/contrib/inventory_builder/inventory.py ${IPS[@]}

导致

DEBUG: Adding group all
DEBUG: Adding group kube-master
DEBUG: Adding group kube-node
DEBUG: Adding group etcd
DEBUG: Adding group k8s-cluster
DEBUG: Adding group calico-rr
DEBUG: adding host node1 to group all
DEBUG: adding host node2 to group all
DEBUG: adding host node3 to group all
DEBUG: adding host node1 to group etcd
DEBUG: adding host node2 to group etcd
DEBUG: adding host node3 to group etcd
DEBUG: adding host node1 to group kube-master
DEBUG: adding host node2 to group kube-master
DEBUG: adding host node1 to group kube-node
DEBUG: adding host node2 to group kube-node
DEBUG: adding host node3 to group kube-node
Traceback (most recent call last):
  File "kubespray/contrib/inventory_builder/inventory.py", line 431, in <module>
    sys.exit(main())
  File "kubespray/contrib/inventory_builder/inventory.py", line 427, in main
    KubesprayInventory(argv, CONFIG_FILE)
  File "kubespray/contrib/inventory_builder/inventory.py", line 116, in __init__
    self.write_config(self.config_file)
  File "kubespray/contrib/inventory_builder/inventory.py", line 120, in write_config
    with open(self.config_file, 'w') as f:
FileNotFoundError: [Errno 2] No such file or directory: './inventory/sample/hosts.yaml'

我也试过了

declare -a IPS=(10.10.1.3 10.10.1.4 10.10.1.5)
CONFIG_FILE=kubespray/inventory/mycluster/hosts.yaml
${CONFIG_FILE} python3 kubespray/contrib/inventory_builder/inventory.py ${IPS[@]}

导致

-bash: kubespray/inventory/mycluster/hosts.yaml: No such file or directory

这是可以理解的,但是想法是让python脚本创建文件

可以让bash脚本工作,以便它执行

CONFIG_FILE=inventory/mycluster/hosts.yaml python3 contrib/inventory_builder/inventory.py ${IPS[@]}

?

更新

因此,经过一些修补,我得出结论,这可能是由于我的pip虚拟环境,因为我的脚本试图在运行命令之前创建一个虚拟环境,以便获得更多上下文

#!/bin/bash

echo "setting up virtual environment"
sleep 2

sudo apt-get install python3-venv -y

python3 -m venv tutorial-env
source tutorial-env/bin/activate
echo "installing pip requirements"
sudo pip3 install -r kubespray/requirements.txt
cp -rfp kubespray/inventory/sample kubespray/inventory/mycluster
declare -a IPS=(IP1 IP2 IP3)
echo "${IPS[@]}"
CONFIG_FILE=kubespray/inventory/mycluster/hosts.yaml python3  kubespray/contrib/inventory_builder/inventory.py ${IPS[@]}
cat kubespray/inventory/mycluster/hosts.yaml
sudo ansible-playbook -i kubespray/inventory/mycluster/hosts.yaml --ssh-extra-args="-oStrictHostKeyChecking=no" --key-file "~/.ssh/id_rsa" --become-user="provision" kubespray/cluster.yml

如果我去掉这些线

python3 -m venv tutorial-env
source tutorial-env/bin/activate

然后,脚本按照预期工作,在某种程度上,理想情况下,它应该在虚拟环境中,为这个措词不当的问题道歉


Tags: topydebugconfighostyamlbuildergroup
1条回答
网友
1楼 · 发布于 2024-05-13 06:13:12

问题来自脚本中的sudo

您可以创建一个virtualenv并为本地会话激活它
但随后您将更改为根上下文以安装需求,这些需求是在系统级别安装的,而不是在virtualenv级别安装的
然后在virtualenv上下文中执行python,但没有安装需求,因此失败

它解释了为什么在使用系统级已安装需求时,它在没有virtualenv的情况下工作

为了解决这个问题,当您在virtualenv中工作时,只需在不使用sudo的情况下安装需求即可:安装需求并执行ansible

相关问题 更多 >