0

所以我正在尝试创建一个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

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

4

1 回答 1

2

问题来自sudo您的脚本。

您创建一个 virtualenv 并为本地会话激活它。
但是随后您更改为根上下文来安装要求,这些要求安装在系统级别,而不是 virtualenv 级别。
然后你在没有安装要求的情况下在 virtualenv 上下文中执行 python 并且它失败了。

它解释了为什么当您使用系统级安装要求时它在没有 virtualenv 的情况下工作。

要解决这个问题,只需安装需求,而无需sudo在 virtualenv 中工作:安装需求并执行 ansible。

于 2021-02-26T13:42:37.720 回答