gpt4 book ai didi

主任务中的 Ansible 子任务

转载 作者:行者123 更新时间:2023-12-03 23:50:09 29 4
gpt4 key购买 nike

我想在一个主任务中有 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

但我显然做错了什么。当我运行 playbook 时,出现以下错误:

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 中使用这两个“子”任务,以便在 playbook 中简单地导入角色?

编辑实现@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 }}"

关于主任务中的 Ansible 子任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42907086/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com