AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 974203
Accepted
Mat
Mat
Asked: 2019-07-07 07:38:07 +0800 CST2019-07-07 07:38:07 +0800 CST 2019-07-07 07:38:07 +0800 CST

标签未应用于 Ansible 包含的角色

  • 772

我创建了一个简单的 Ansible 剧本:

---
- hosts: all

  tasks:
    - name: Install Icinga2 on Windows
      include_role:
        name: my.icinga2.role
        apply:
          tags:
            - install-icinga2

该角色包含此任务文件:

---
- include_tasks: vars.yml
  tags: ['always']

- include_tasks: install.yml
  tags: ['install-icinga2-stack', 'install-icinga2']

- include_tasks: ido-install.yml
  when: icinga2_ido_enable == true
  tags: ['install-icinga2-stack', 'install-icinga2-ido']  

- include_tasks: configure.yml
  tags: ['install-icinga2-stack']

[...]

这是我执行剧本时的结果:

me@ansible:~/ansible$ ansible-playbook plays/icinga2-client-win.yml -i staging.ini --limit windows


PLAY [all] ***************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [my.windows.client]

TASK [Include variables for Icinga 2] ********************************************************************************************
ok: [my.windows.client]

TASK [set_fact] ******************************************************************************************************************
skipping: [my.windows.client]

TASK [set_fact] ******************************************************************************************************************
ok: [my.windows.client]

TASK [Install Icinga2 Client and connect it to the master server] ****************************************************************

TASK [my.icinga2.role : include_tasks] ***************************************************************************************
included: /home/me/ansible/roles/internal/my.icinga2.role/tasks/vars.yml for my.windows.client

TASK [my.icinga2.role : Set default fact for mysql command] ******************************************************************
ok: [my.windows.client]

TASK [my.icinga2.role : Set fact for mysql command if auth params are given] *************************************************
skipping: [my.windows.client]

TASK [my.icinga2.role : Set Monitoring Plugins for old Debian Versions] ******************************************************
skipping: [my.windows.client]

TASK [my.icinga2.role : include_tasks] ***************************************************************************************
included: /home/me/ansible/roles/internal/my.icinga2.role/tasks/install.yml for my.windows.client

TASK [my.icinga2.role : include_tasks] ***************************************************************************************
skipping: [my.windows.client]

TASK [my.icinga2.role : include_tasks] ***************************************************************************************
skipping: [my.windows.client]

TASK [my.icinga2.role : include_tasks] ***************************************************************************************
included: /home/me/ansible/roles/internal/my.icinga2.role/tasks/install-Windows.yml for my.windows.client

TASK [my.icinga2.role : set_fact] ********************************************************************************************
ok: [my.windows.client]

TASK [my.icinga2.role : set_fact] ********************************************************************************************
skipping: [my.windows.client]

TASK [my.icinga2.role : Install Icinga 2] ************************************************************************************
changed: [my.windows.client]

TASK [my.icinga2.role : include_tasks] ***************************************************************************************
skipping: [my.windows.client]

TASK [my.icinga2.role : include_tasks] ***************************************************************************************
included: /home/me/ansible/roles/internal/my.icinga2.role/tasks/configure.yml for my.windows.client

TASK [my.icinga2.role : Check if Icinga 2 API are already activated] *********************************************************
[ This should not be included! ]

RUNNING HANDLER [my.icinga2.role : Restart Icinga2 on Windows] ***************************************************************
    to retry, use: --limit @/home/me/ansible/plays/icinga2-client-win.retry

PLAY RECAP ***********************************************************************************************************************
my.windows.client   : ok=10   changed=1    unreachable=0    failed=1 

为什么包含configure.yml角色任务文件,因为只有在我应用install-icinga2-stack标签并且我正在应用install-icinga2时才应该包含它?

此外,我意识到不包含ido-install.yml角色任务文件只是因为icinga2_ido_enable变量不在true此剧本中(默认为false),而不是因为未应用其标签之一(这应该是我想要的) .

我哪里错了?

ansible
  • 1 1 个回答
  • 9285 Views

1 个回答

  • Voted
  1. Best Answer
    Vladimir Botka
    2019-07-07T12:45:31+08:002019-07-07T12:45:31+08:00

    在include_role中应用标签意味着标签

    将应用于包含内的任务。

    换句话说,被包含角色中的任务将继承应用的标签。期望应用的标签会选择任务是一种误解。要在命令行或 Ansible 配置设置中选择任务使用--tags和,请使用和选项。--skip-tagsTAGS_RUNTAGS_SKIP

    include_role的文档中没有明确提到一个重要的事实。仅当整个任务为 时,参数应用tags: always标签才有效。这仅在示例中显示。

        - name: Apply tags to tasks within included file
          include_role:
            name: install
            apply:
              tags:
                - install
          tags:
            - always
    

    例子

    让我们有 2 个任务的角色 1

        shell> cat roles/role1/tasks/main.yml 
        - debug:
            msg: 'This is task 2'
          tags: task2
        
        - debug:
            msg: 'This is task 3'
          tags: task3
    

    和一本剧本

        shell> cat play1.yml 
        - hosts: localhost
          tasks:
            - debug:
                msg: 'This is task 1'
              tags: task1
    
            - include_role:
                name: role1
                apply:
                  tags: role1
              tags: always
    

    如果我们在没有任何选项的情况下运行剧本,则所有任务都包括在内

        shell> ansible-playbook play1.yml | grep msg
            "msg": "This is task 1"
            "msg": "This is task 2"
            "msg": "This is task 3"
    

    请参阅下面的其他变体

        shell> ansible-playbook play1.yml --tags task1 | grep msg
            "msg": "This is task 1"
        shell> ansible-playbook play1.yml --tags task2 | grep msg
            "msg": "This is task 2"
        shell> ansible-playbook play1.yml --tags role1 | grep msg
            "msg": "This is task 2"
            "msg": "This is task 3"
        shell> ansible-playbook play1.yml --skip-tags role1 | grep msg
            "msg": "This is task 1"
        shell> ansible-playbook play1.yml --tags role1 --skip-tags task2 | grep msg
            "msg": "This is task 3"
    

    注意。标签列表未按预期工作。

        shell> ansible-playbook play1.yml --list-tags
        playbook: play1.yml
          play #1 (localhost): localhost    TAGS: []
              TASK TAGS: [always, task1]
    

    (备案:带有应用标签的 include_role 不起作用 #52063)

    • 7

相关问题

  • 重复的 Ansible 任务

  • 无法形成站点中的文件的链接,该链接可用于使用 ansible 在远程服务器中启用的目录站点?

  • 如何执行 ansible 的特定角色?

  • Ansible 和 rbash

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve