简体   繁体   English

Ansible 剧本在剧本中使用角色时不起作用

[英]Ansible playbook not working when using roles in playbook

So, my playbook works fine when using this syntax:因此,使用此语法时,我的剧本可以正常工作:

---
- hosts: all
  become: true
  become_user: root
  gather_facts: false
  connection: local
  vars_files:
    - roles/docker_setup/vars/docker_vars.yml
    - roles/docker_setup/vars/aws_cred.yml
  vars:
    instance_ids:
      - "i-xxxxxxxxxxx"
  tasks:
    - include_role:
        name: docker_setup
        tasks_from: docker_tasks.yml

But I read that you can use this syntax which looks much easier to handle once I want to add new roles to the playbook:但是我读到,一旦我想向剧本添加新角色,您可以使用这种看起来更容易处理的语法:

---
- hosts: all
  become: true
  become_user: root
  gather_facts: false
  connection: local
  vars_files:
    - roles/docker_setup/vars/docker_vars.yml
    - roles/docker_setup/vars/aws_cred.yml
  vars:
    instance_ids:
      - "i-xxxxxxxxxxx"
  roles:
  - docker_setup

The difference is:区别在于:

roles:
  - docker_setup

instead of代替

tasks:
  - include_role:
      name: docker_setup
      tasks_from: docker_tasks.yml

But once I try to run the playbook nothing happens:但是一旦我尝试运行剧本,什么都没有发生:

sudo ansible-playbook docker_setup.yml --ask-vault-pass -i hosts --user devops --key-file /home/devops/.ssh/id_rsa
Vault password: 

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

PLAY RECAP *************************************************************************************************************************************************

Why is it that?为什么会这样? Here is my tree of roles:这是我的角色树:

.
├── docker_setup
│   ├── handlers
│   │   └── main.yml
│   ├── tasks
│   │   └── docker_tasks.yml
│   └── vars
│       ├── aws_cred.yml
│       └── docker_vars.yml
└── user_host_create
    ├── handlers
    │   └── user_handlers.yml
    ├── tasks
    │   └── user_tasks.yml
    └── vars
        ├── user_password_vault.txt
        ├── user_password.yml
        └── user_vars.yml

and hosts file:和主机文件:

[webservers]
172.31.31.223

Thank you!谢谢!

The difference between the two options below is what file with the tasks is executed.下面两个选项之间的区别在于执行了包含任务的文件。

  tasks:
    - include_role:
        name: docker_setup
        tasks_from: docker_tasks.yml
  roles:
    - docker_setup

In the first case, it is explicitly roles/docker_setup/tasks/docker_tasks.yml (see tasks_from ).在第一种情况下,它是明确的角色/docker_setup/tasks/docker_tasks.yml (参见tasks_from )。

In the second case, it is roles/docker_setup/tasks/main.yml (see Role directory structure ).在第二种情况下,它是角色/docker_setup/tasks/main.yml (请参阅角色目录结构)。 This file is missing in the role.角色中缺少此文件。 As a result, nothing is executed.结果,什么都不执行。 You can fix it and create the file eg您可以修复它并创建文件,例如

shell> cat roles/docker_setup/tasks/main.yml
- ansible.builtin.import_tasks: docker_tasks.yml

This would make the two options equivalent as to what tasks are executed.这将使两个选项等同于执行哪些任务。 However, there will be other differences eg the scope of the variables, or inheritance of the tags.但是,还会有其他差异,例如变量的 scope 或标签的 inheritance。 For details see Using Roles .有关详细信息,请参阅使用角色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM