微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当执行假设创建资源组的剧本时,在执行剧本时出错

如何解决当执行假设创建资源组的剧本时,在执行剧本时出错

我开发了一个包含角色的剧本,这些角色将在 Azure 云上创建资源组。 下面是目录结构。

prhinge1@DESKTOP-F8RMF14:~/pits-shared-service$ cd roles
prhinge1@DESKTOP-F8RMF14:~/pits-shared-service/roles$ ls
netinfracreator
prhinge1@DESKTOP-F8RMF14:~/pits-shared-service/roles$ tree netinfracreator
netinfracreator
├── README.md
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── Meta
│   └── main.yml
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

8 directories,8 files

tasks/main.yml 包含以下代码

    ---
    # tasks file for netinfracreator
    - name: Create resource group if doesn't exist
      azure_rm_resourcegroup:
         name: "{{ resource_group }}"
         location: "{{ location }}"

vars/main.yml - 在这文件中声明的变量。

---
# vars file for netinfracreator
resource_group: "test_ansible_resourcegrp"
location: "eastus"

下面是调用角色的主要剧本文件(netinfracreator-role.yml)。

    ---
    - name: User netinfracreator role playbook
      hosts: localhost
      connection: local
      collections:
       - azure.azcollection
      become_user: root
      become: true
      become_method: sudo
    
      roles:
       - role: netinfracreator

此外,我将服务主体凭据存储在 /home//.azure/credentials 文件中。

使用的pythone版本如下

prhinge1@DESKTOP-F8RMF14:~/pits-shared-service$ python3 --version
Python 3.8.5

当我执行 playbook 时,出现以下错误。请让我知道这段代码有什么问题。

prhinge1@DESKTOP-F8RMF14:~/pits-shared-service$ sudo ansible-playbook netinfracreator-role.yml --check
[sudo] password for prhinge1:

PLAY [User netinfracreator role playbook] *****************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************
ok: [localhost]

TASK [netinfracreator : Create resource group if doesn't exist] *******************************************************************************************
An exception occurred during task execution. To see the full traceback,use -vvv. The error was: FileNotFoundError: [Errno 2] No such file or directory: '/root/.azure/azureProfile.json'
fatal: [localhost]: Failed! => {"changed": false,"module_stderr": "Traceback (most recent call last):\n  File \"/usr/local/lib/python3.8/dist-packages/azure/cli/core/_session.py\",line 39,in load\n    with codecs_open(self.filename,'r',encoding=self._encoding) as f:\n  File \"/usr/lib/python3.8/codecs.py\",line 905,in open\n    file = builtins.open(filename,mode,buffering)\nFileNotFoundError: [Errno 2] No such file or directory: '/root/.azure/azureProfile.json'\n\nDuring handling of the above exception,another exception occurred:\n\nTraceback (most recent call last):\n  File \"/root/.ansible/tmp/ansible-tmp-1622712598.829142-71872954787953/AnsiballZ_azure_rm_resourcegroup.py\",line 102,in <module>\n    _ansiballz_main()\n  File \"/root/.ansible/tmp/ansible-tmp-1622712598.829142-71872954787953/AnsiballZ_azure_rm_resourcegroup.py\",line 94,in _ansiballz_main\n    invoke_module(zipped_mod,temp_path,ANSIBALLZ_ParaMS)\n  File \"/root/.ansible/tmp/ansible-tmp-1622712598.829142-71872954787953/AnsiballZ_azure_rm_resourcegroup.py\",line 40,in invoke_module\n    runpy.run_module(mod_name='ansible.modules.cloud.azure.azure_rm_resourcegroup',init_globals=None,run_name='__main__',alter_sys=True)\n  File \"/usr/lib/python3.8/runpy.py\",line 207,in run_module\n    return _run_module_code(code,init_globals,run_name,mod_spec)\n  File \"/usr/lib/python3.8/runpy.py\",line 97,in _run_module_code\n    _run_code(code,mod_globals,\n  File \"/usr/lib/python3.8/runpy.py\",line 87,in _run_code\n    exec(code,run_globals)\n  File \"/tmp/ansible_azure_rm_resourcegroup_payload_lhvkhayb/ansible_azure_rm_resourcegroup_payload.zip/ansible/modules/cloud/azure/azure_rm_resourcegroup.py\",line 291,in <module>\n  File \"/tmp/ansible_azure_rm_resourcegroup_payload_lhvkhayb/ansible_azure_rm_resourcegroup_payload.zip/ansible/modules/cloud/azure/azure_rm_resourcegroup.py\",line 287,in main\n  File \"/tmp/ansible_azure_rm_resourcegroup_payload_lhvkhayb/ansible_azure_rm_resourcegroup_payload.zip/ansible/modules/cloud/azure/azure_rm_resourcegroup.py\",line 167,in __init__\n  File \"/tmp/ansible_azure_rm_resourcegroup_payload_lhvkhayb/ansible_azure_rm_resourcegroup_payload.zip/ansible/module_utils/azure_rm_common.py\",line 341,line 1145,line 1378,in _get_credentials\n  File \"/tmp/ansible_azure_rm_resourcegroup_payload_lhvkhayb/ansible_azure_rm_resourcegroup_payload.zip/ansible/module_utils/azure_rm_common.py\",line 1289,in _get_azure_cli_credentials\n  File \"/usr/local/lib/python3.8/dist-packages/azure/common/credentials.py\",line 46,in get_azure_cli_credentials\n    profile = get_cli_profile()\n  File \"/usr/local/lib/python3.8/dist-packages/azure/common/credentials.py\",line 28,in get_cli_profile\n    ACCOUNT.load(os.path.join(azure_folder,'azureProfile.json'))\n  File \"/usr/local/lib/python3.8/dist-packages/azure/cli/core/_session.py\",line 42,in load\n    self.save()\n  File \"/usr/local/lib/python3.8/dist-packages/azure/cli/core/_session.py\",in save\n    with codecs_open(self.filename,'w',buffering)\nFileNotFoundError: [Errno 2] No such file or directory: '/root/.azure/azureProfile.json'\n","module_stdout": "","msg": "MODULE FAILURE\nSee stdout/stderr for the exact error","rc": 1}

PLAY RECAP ************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    Failed=1    skipped=0    rescued=0    ignored=0

解决方法

我解决了这个问题。在 netinfracreator-role.yml 中,我使用了

become_user: root
become: true
become_method: sudo

这实际上不是必需的。因此,它迫使我使用 sudo 命令运行 playbook。因此,它总是在根目录中搜索 azureProfile.json 文件,但它位于我的用户文件夹中。 因此,为了解决这个问题,我从剧本中删除了所有 become 参数,并在没有 sudo 的情况下执行剧本,结果奏效了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。