Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在无HOME环境变量时非交互式configure不生效也不报错 #526

Closed
Xun66 opened this issue Oct 23, 2022 · 1 comment
Closed

在无HOME环境变量时非交互式configure不生效也不报错 #526

Xun66 opened this issue Oct 23, 2022 · 1 comment

Comments

@Xun66
Copy link

Xun66 commented Oct 23, 2022

  • 复现方法:
    • 在阿里云创建一台按量付费ECS实例
    • 镜像选择Alibaba Cloud Linux 3.2104 LTS 64位3.2104 LTS 64位 快速启动版
    • 登录名root
    • 高级选项-授予实例RAM角色(这步没有也不影响复现)
    • 高级选项-实例自定义数据 cloud-init处输入以下内容
    • 确认订单,连接到实例
#!/bin/bash
# install aliyun-cli
set -x
cd ~ && mkdir -p download && cd download
wget https://aliyuncli.alicdn.com/aliyun-cli-linux-latest-amd64.tgz
tar xzvf aliyun-cli-linux-latest-amd64.tgz
cp -f aliyun /usr/local/bin
rm -f aliyun aliyun-cli-linux-latest-amd64.tgz
aliyun configure help
aliyun configure set --profile default --region cn-beijing --language zh --mode EcsRamRole --ram-role-name ECS-ONDEMAND-MACHINE
  • 输出:
    查看日志: less /var/log/cloud-init-output.log,输出结果如下:
...
2022-10-23 11:23:44 (35.0 MB/s) - ‘aliyun-cli-linux-latest-amd64.tgz’ saved [7220799/7220799]

+ tar xzvf aliyun-cli-linux-latest-amd64.tgz
aliyun
+ cp -f aliyun /usr/local/bin
+ rm -f aliyun aliyun-cli-linux-latest-amd64.tgz
+ export DEBUG=sdk
+ DEBUG=sdk
+ aliyun configure help
configure credential and settings

Usage:
  aliyun configure --mode {AK|StsToken|RamRoleArn|EcsRamRole|RsaKeyPair|RamRoleArnWithRoleName|ChainableRamRoleArn} --profile <profileName>

Commands:
  get    print configuration values
  set    set config in non interactive mode
  list   list all config profile
  delete delete the specified profile

Use `configure --help` for more information.
+ aliyun configure set --profile default --region cn-beijing --language zh --mode EcsRamRole --ram-role-name ECS-ONDEMAND-MACHINE
+ ls -al /root/.aliyun
ls: cannot access '/root/.aliyun': No such file or directory
Cloud-init v. 19.1.17-1.0.1.al8 running 'modules:final' at Sun, 23 Oct 2022 03:23:43 +0000. Up 20.89 seconds.
  • 后续测试:
    当cloud-init执行完后,用root用户再次执行aliyun configure set:
[root@iZ2ze6v09cqhsc986z4tg2Z ~]# aliyun configure set --profile default --region cn-beijing --language zh --mode EcsRamRole --ram-role-name ECS-ONDEMAND-MACHINE
[root@iZ2ze6v09cqhsc986z4tg2Z ~]# ll .aliyun/config.json 
-rw------- 1 root root 638 Oct 23 11:34 .aliyun/config.json
[root@iZ2ze6v09cqhsc986z4tg2Z ~]#

可以请问一下应该怎么排查吗,可能是哪里的问题���?
谢谢!

@Xun66 Xun66 changed the title 在阿里云实例cloud-init脚本中非交互式configure不生效也无报错 Nov 21, 2022
@Xun66
Copy link
Author

Xun66 commented Nov 21, 2022

定位到问题。
cloud-init的userdata脚本默认并非以root登录态执行的,所以执行时没有$HOME环境变量。

也就是说,配置实际上已经被写入到$HOME/.aliyun//.aliyun/目录下,使用aliyun的时候如果也在cloud-init里就没有问题。但是如果登录到root用户执行aliyun,会导致aliyun去/root/.aliyun/目录下寻找配置,所以会导致配置找不到。

如有必要,在配置前补充定义HOME变量即可。

关闭Issue。

@Xun66 Xun66 closed this as completed Nov 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant