执行假设创建资源组的剧本时在执行剧本时出错
When executing playbook which suppose to create resource group give error while executing playbook
我已经开发了一个带有角色的剧本,它将在 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 文件中存储了服务主体凭证。
使用的python版本如下
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, init_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, 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 1145, 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 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\", line 46, in save\n with codecs_open(self.filename, 'w', 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", "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 命令 运行 剧本。因此,它总是在根目录中搜索 azureProfile.json 文件,但它在我的用户文件夹中。
所以为了解决这个问题,我从剧本中删除了所有 become 参数并在没有 sudo 的情况下执行剧本并且它有效。
我已经开发了一个带有角色的剧本,它将在 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 文件中存储了服务主体凭证。
使用的python版本如下
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, init_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, 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 1145, 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 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\", line 46, in save\n with codecs_open(self.filename, 'w', 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", "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 命令 运行 剧本。因此,它总是在根目录中搜索 azureProfile.json 文件,但它在我的用户文件夹中。 所以为了解决这个问题,我从剧本中删除了所有 become 参数并在没有 sudo 的情况下执行剧本并且它有效。