[英]Ansible sub tasks in main tasks
我想在一个主要任务中有2个“子任务”,但是由于某种原因,我遇到了语法错误。
在我的/etc/ansible
目录中,我必须具有以下结构:
playbooks/
set_users.yml
roles/
users/
tasks/
main.yml
create_admin.yml
update_root.yml
vars/
main.yml
在create_admin.yml
文件中,我具有以下内容:
---
- name: Create srv_admin user
user: name=srv_admin password="{{ admin_password }}" groups=wheel shell=/bin/bash
并在update_root.yml
:
---
- name Update root password
user: name=root password="{{ root_password }}"
然后,我将这些任务包含在main.yml
:
---
- name: Modify users
tasks:
- include: update_root.yml
- include: create_admin.yml
我的vars/main.yml
包含我的密码:
---
admin_password: SHA512HASH
root_password: SHA512HAS
现在将所有内容放到playbooks/set_users.yml
:
---
- name: create user
hosts: servers
remote_user: root
roles:
- users
但是我显然做错了。 运行剧本时,出现以下错误:
ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.
The error appears to have been in '/etc/ansible/roles/users/tasks/main.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
---
- name: Modify users
^ here
如何在tasks/main.yml
使用这两个“子”任务,以便可以简单地将角色导入到剧本中?
编辑在实施@Konstantin Suvorov建议之后:
The error appears to have been in '/etc/ansible/roles/users/tasks/update_root.yml': line 3, column 7, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name Update root password
user: name=root password="{{ root_password }}"
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
tasks/main.yml
应该是任务列表,因此不需要tasks:
关键字:
---
- include: update_root.yml
- include: create_admin.yml
并避免使用key=value
语法,它有时会在腿上射击,请使用纯YAML:
- name: Update root password
user:
name: root
password: "{{ root_password }}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.