访问att.iTems循环中的stat.sisits

发布于 2025-01-20 19:38:56 字数 4193 浏览 4 评论 0原文

我正在尝试创建一个脚本,该脚本将禁用/启用repolist中列出的所有CentOS7存储库。

我想在尝试对其进行编辑之前先检查回购文件 - 使用替换

我的想法是拥有一个2D列表包含文件的名称以及是否存在。例如[centos-base.repo,true]

我已经成功创建了一个将执行此操作的脚本,但是它使用了include_tasks。理想情况下,我想在一个文件中拥有所有功能。

我收到此错误:

TASK [DEBUG  Indexes] ******************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'results' is undefined\n\nThe error appears to be in '/root/defaultRepoTest.yml': line 41, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name: DEBUG  Indexes\n    ^ here\n"}

从显示结果从输出中移出打印语句

TASK [DEBUG Show results] **************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "ansible_loop_var": "item",
            "changed": false,
            "failed": false,
            "invocation": {
                "module_args": {
                    "checksum_algorithm": "sha1",
                    "follow": false,
                    "get_attributes": true,
                    "get_checksum": true,
                    "get_md5": false,
                    "get_mime": true,
                    "path": "/etc/yum.repos.d/CentOS-Base.repo"
                }
            },
            "item": "CentOS-Base.repo",
            "stat": {
                "atime": 1649670713.3687398,
                "attr_flags": "",
                "attributes": [],
                "block_size": 4096,
                "blocks": 8,
                "charset": "us-ascii",
                "checksum": "70d4820050f582bcbfe1c9726bd32e7df53b1092",
                "ctime": 1649670636.5707407,
                "dev": 64768,
                "device_type": 0,
                "executable": false,
                "exists": true,
                "gid": 0,
                "gr_name": "root",
                "inode": 98834,
                "isblk": false,
                "ischr": false,
                "isdir": false,
                "isfifo": false,
                "isgid": false,
                "islnk": false,
                "isreg": true,
                "issock": false,
                "isuid": false,
                "mimetype": "text/plain",
                "mode": "0644",
                "mtime": 1649670636.5697408,
                "nlink": 1,
                "path": "/etc/yum.repos.d/CentOS-Base.repo",
                "pw_name": "root",
                "readable": true,
                "rgrp": true,
                "roth": true,
                "rusr": true,
                "size": 865,
                "uid": 0,
                "version": "1620661251",
                "wgrp": false,
                "woth": false,
                "writeable": true,
                "wusr": true,
                "xgrp": false,
                "xoth": false,
                "xusr": false
            }
        },

代码文件: ---

- name: Enable/Disable CentOS7 Repos
  hosts: localhost
  vars:
    repoList:
      - CentOS-Base.repo
      - CentOS-Debuginfo.repo
      - CentOS-Media.repo
      - CentOS-Vault.repo
      - epel.repo
      - CentOS-CR.repo
      - CentOS-fasttrack.repo
      - CentOS-Sources.repo
      - CentOS-x86_64-kernel.repo
      - epel-testing.repo
      - CentOS-SCLo-scl.repo
      - CentOS-SCLo-scl-rh.repo
      - CentOS-SCLo-scl-rh.repo.rpmnew

    reposPresent: []

    enable: 0

  tasks:

  - name: Check if path exists
    stat:
      path: /etc/yum.repos.d/{{ item }}
    register: outcome
    with_items: "{{ repoList }}"

  - name: DEBUG Show outcome
    debug:
      msg: "{{ outcome }}"

  - name: DEBUG Show results
    debug:
      msg: "{{ outcome.results}}"

  - name: DEBUG  Indexes
    debug:
      msg: "{{ results.stat.exists }}"
    with_items: "{{ outcome }}"

我一定不能正确访问该元素。我希望调试索引语句能打印一个布尔值列表。然后,我可以将它们添加到2D列表中。

I am trying to create a script that will disable/enable all of the CentOS7 repos listed in repoList.

I want to check if the repo file is present first before attempting to edit it - using replace.

My idea was to have a 2D list contain the name of file and whether it was present or not. E.g [CentOS-Base.repo, True]

I have successfully created a script that will do this, however it used include_tasks. Ideally I would like to have all of the functionality in one file.

I get this error:

TASK [DEBUG  Indexes] ******************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'results' is undefined\n\nThe error appears to be in '/root/defaultRepoTest.yml': line 41, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name: DEBUG  Indexes\n    ^ here\n"}

Exerpt from output from Show results print statement

TASK [DEBUG Show results] **************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "ansible_loop_var": "item",
            "changed": false,
            "failed": false,
            "invocation": {
                "module_args": {
                    "checksum_algorithm": "sha1",
                    "follow": false,
                    "get_attributes": true,
                    "get_checksum": true,
                    "get_md5": false,
                    "get_mime": true,
                    "path": "/etc/yum.repos.d/CentOS-Base.repo"
                }
            },
            "item": "CentOS-Base.repo",
            "stat": {
                "atime": 1649670713.3687398,
                "attr_flags": "",
                "attributes": [],
                "block_size": 4096,
                "blocks": 8,
                "charset": "us-ascii",
                "checksum": "70d4820050f582bcbfe1c9726bd32e7df53b1092",
                "ctime": 1649670636.5707407,
                "dev": 64768,
                "device_type": 0,
                "executable": false,
                "exists": true,
                "gid": 0,
                "gr_name": "root",
                "inode": 98834,
                "isblk": false,
                "ischr": false,
                "isdir": false,
                "isfifo": false,
                "isgid": false,
                "islnk": false,
                "isreg": true,
                "issock": false,
                "isuid": false,
                "mimetype": "text/plain",
                "mode": "0644",
                "mtime": 1649670636.5697408,
                "nlink": 1,
                "path": "/etc/yum.repos.d/CentOS-Base.repo",
                "pw_name": "root",
                "readable": true,
                "rgrp": true,
                "roth": true,
                "rusr": true,
                "size": 865,
                "uid": 0,
                "version": "1620661251",
                "wgrp": false,
                "woth": false,
                "writeable": true,
                "wusr": true,
                "xgrp": false,
                "xoth": false,
                "xusr": false
            }
        },

Code file:
---

- name: Enable/Disable CentOS7 Repos
  hosts: localhost
  vars:
    repoList:
      - CentOS-Base.repo
      - CentOS-Debuginfo.repo
      - CentOS-Media.repo
      - CentOS-Vault.repo
      - epel.repo
      - CentOS-CR.repo
      - CentOS-fasttrack.repo
      - CentOS-Sources.repo
      - CentOS-x86_64-kernel.repo
      - epel-testing.repo
      - CentOS-SCLo-scl.repo
      - CentOS-SCLo-scl-rh.repo
      - CentOS-SCLo-scl-rh.repo.rpmnew

    reposPresent: []

    enable: 0

  tasks:

  - name: Check if path exists
    stat:
      path: /etc/yum.repos.d/{{ item }}
    register: outcome
    with_items: "{{ repoList }}"

  - name: DEBUG Show outcome
    debug:
      msg: "{{ outcome }}"

  - name: DEBUG Show results
    debug:
      msg: "{{ outcome.results}}"

  - name: DEBUG  Indexes
    debug:
      msg: "{{ results.stat.exists }}"
    with_items: "{{ outcome }}"

I must not be accessing the element properly. I expect the Debug Indexes statement to print a list of boolean values. I can then add these to a 2D list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬心 2025-01-27 19:38:56

您的最后一个调试任务应该是

    - name: DEBUG  Indexes
      debug:
        msg: "{{ item.stat.exists }}"
      with_items: "{{ outcome.results }}"

Your last debug task should be

    - name: DEBUG  Indexes
      debug:
        msg: "{{ item.stat.exists }}"
      with_items: "{{ outcome.results }}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文