所以我正在尝试创建一个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
然后脚本按预期工作,在某种程度上,理想情况下它应该在虚拟环境中,为措辞不当的问题道歉