ansible:从子文件夹中获取playbook中的角色

时间:2016-04-07 09:29:39

标签: vagrant ansible ansible-playbook

我有这棵树:

├── plays
│   ├── ansible.cfg
│   ├── playbook_01.yml
│   ├── playbook_02.yml
│   └── playbook_03.yml
├── README.rst
├── roles
│   ├── role_A
│   │   ├── files
│   │   └── tasks
│   │       └── main.yml
│   └── role_B
│       ├── files
│       └── tasks
│           └── main.yml
├── serverlist
│   ├── client1_serverlist_prod
│   ├── client1_serverlist_test
│   ├── client1_serverlist_train
│   ├── client2_serverlist_prod
│   ├── client2_serverlist_test
│   └── client2_serverlist_train
└── vagrant
    └── Vagrantfile

在play文件夹中使用ansible.cfg ::

$ cat plays/ansible.cfg
[defaults]
roles_path=../roles/
$

我从vagrant调用ansible.playbook ::

$ grep playbook vagrant/Vagrantfile
ansible.playbook = "../plays/playbook_01.yml

on playbook_01.yml ::

$ cat plays/playbook_01.yml
- hosts: vagrant
  vars:
    user: fox
    home: /home/fox
  roles:
    - role_B

使用role_B ::

$ cat roles/role_B/tasks/main.yml
---
- name: Create user group
  group: name={{ user }} state=present

- name: Create home directory for user
  file: state=directory path={{ home }} group=www-data owner={{ user }}
$

但是当只去看play文件夹中的角色时,我得到了这个错误::

vagrant$ vagrant provision
==> vagrant: Running provisioner: ansible...
PYTHONUNBUFFERED=1 ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_FORCE_COLOR=true
ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -o
ForwardAgent=yes -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook
--user=vagrant --connection=ssh --timeout=30 --limit='vagrant'
--inventory-file=/home/luis/lab/sandbox/akd-iac/stack/vagrant/.vagrant/provisioners/ansible/inventory
--sudo -vvvv ../plays/playbook_01.yml
ERROR: cannot find role in
~/stack/plays/roles/role_B or
~/stack/plays/role_B or /etc/ansible/roles/role_B
Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.
vagrant$

4 个答案:

答案 0 :(得分:6)

您可以使用变量roles_path。它指向存储角色的文件夹。

答案 1 :(得分:1)

如上所述,你的ansible.cfg不太可能被ansible接收。你可以通过设置这个来测试你的ansible.cfg是否被选中:

[defaults]
roles_path = ./isnasiblegettingmycfg

并检查调试输出。 ansible.cfg是looked for in

  
      
  • ANSIBLE_CONFIG(环境变量)
  •   
  • ansible.cfg(在当前目录中)
  •   
  • .ansible.cfg(在主目录中)
  •   
  • /etc/ansible/ansible.cfg
  •   

角色是looked for在已执行的playbook的目录中,以及目录角色/相对于已执行的playbook和roles_path。所以另一个简单的解决方案是:

cd ~/stack/plays/ && ln -s ../roles

答案 2 :(得分:0)

我只是在plays文件夹中移动角色文件夹并删除play / ansible.cfg:

.
├── plays
│   ├── playbook_01.yml
│   ├── playbook_02.yml
│   ├── playbook_03.yml
│   └── roles
│       ├── role_A
│       │   ├── files
│       │   └── tasks
│       │       └── main.yml
│       └── role_B
│           ├── files
│           └── tasks
│               └── main.yml
├── README.rst
├── serverlist
│   ├── client1_serverlist_prod
│   ├── client1_serverlist_test
│   ├── client1_serverlist_train
│   ├── client2_serverlist_prod
│   ├── client2_serverlist_test
│   └── client2_serverlist_train
└── vagrant
    └── Vagrantfile

有关使其工作的ansible.cfg配置的详细信息::

$ ls ~/.ansible.cfg
ls: cannot access ~/.ansible.cfg: No such file or directory

$ grep roles_path /etc/ansible/ansible.cfg
#roles_path    = /etc/ansible/roles

$ echo $ANSIBLE_CONFIG

$

我仍然从vagrant召唤ansible.playbook ::

$ grep playbook vagrant/Vagrantfile
ansible.playbook = "../plays/playbook_01.yml

on playbook_01.yml ::

$ cat plays/playbook_01.yml
- hosts: vagrant
  vars:
    user: fox
    home: /home/fox
  roles:
    - role_B

使用role_B ::

$ cat roles/role_B/tasks/main.yml
---
- name: Create user group
  group: name={{ user }} state=present

- name: Check Python version
  command: python --version
  register: pyver

- name: Print version
  debug:
    msg: "Python Version: {{ pyver.stderr }}"

这种修改就像魅力一样:

$ vagrant provision
==> vagrant: Running provisioner: ansible...

PLAY [vagrant] ************************************************************
GATHERING FACTS ***********************************************************
  ok: [vagrant]
TASK: [role_B | Create user group] ****************************************
  ok: [vagrant]
TASK: [role_B | Check Python version] *************************************
  changed: [vagrant]
TASK: [role_B | Print version] ********************************************
  ok: [vagrant] => {
    "msg": "Python Version: Python 2.7.9"
}
PLAY RECAP ****************************************************************
vagrant                    : ok=4    changed=1    unreachable=0    failed=0
$

看起来角色文件夹需要位于文件夹剧本中。

答案 3 :(得分:0)

可以直接提供角色路径

https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html#storing-and-finding-roles

或者,如果您愿意,可以使用:

roles:
- role: role_in_the_current_folder
- role: ../roles/role_in_an_other_folder