ansible include_role 语法问题虽然流程文档 [英] ansible include_role syntax problem although flow the documentions
问题描述
我尝试 include_rule 但我不断收到错误语法的错误这是我的文件结构:
I try to include_rule but i keep getting errors on the wrong syntax this is my file structure :
├── roles
│ └── utilities
│ ├── defaults
│ ├── files
│ ├── handlers
│ ├── meta
│ ├── tasks
│ │ └── d_pack.yml
│ ├── templates
│ └── vars
└── test4.yml
规则 d_pack.yml
the rule d_pack.yml
---
- name: Download package
get_url:
url_username:
url_password:
dest:
url:
owner:
group:
test4.yml
---
- name: check if exsist
hosts: localhost
gather_facts: false
tasks:
- debug:
msg: "#################################### INCLUDE START ######################################"
- include_role:
static:no
name:utilities
tasks_from:d_pack
vars:
url_username: "{{ user }}"
url_password: "{{ pass }}"
dest: "{{ ansible_env.APP }}/"
url: "{{ myurl }}"
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
- debug:
msg: "#################################### INCLUDE END ######################################"
我想我遵循此处显示的示例:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html在这里:
https://docs.ansible.com/ansible/latest/modules/include_role_module.html
i think i follow the examples shown here :
https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html
and here :
https://docs.ansible.com/ansible/latest/modules/include_role_module.html
但是,我仍然收到一个错误:
but still, I'm getting an error:
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
ERROR! 'name' is a required field for include_role.
The error appears to be in '/home/ec2-user/ansible/test4.yml': line 13, column 11, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- include_role:
^ here
也在遵循 yml 规则后,我将其修复为:
also after following the yml rules i fixed it to :
---
- name: check if exsist
hosts: localhost
gather_facts: false
tasks:
- name: this is test
include_role:
static: no
name: utilities
tasks_from: d_pack
vars:
url_username: "{{ user }}"
url_password: "{{ pass }}"
dest: "{{ ansible_env.APP }}/"
url: "{{ package_url }}"
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
我仍然收到此错误:错误!include_role 的无效选项:静态
I'm still getting this error: ERROR! Invalid options for include_role: static
The error appears to be in '/home/ec2-user/ansible/test4.yml': line 6, column 8, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
tasks:
- name: this is test
^ here
更新现在我删除了静态:没有我有新错误,因为我可以看到角色首先评估虚拟参数,而不是我通过 include_role 注入的参数
Update now that i removed the static: no i have new error as i can see that the role evaluate the dummy parameters first and not the one i inject via the include_role
- name: Download package
get_url:
url_username: temp
url_password: tmp
dest: tmp
url: tmp
owner: tmp
group: tmp
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/ec2-user/.ansible/tmp/ansible-tmp-1588762539.1880841-18280-15848856136034/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
File "/********/ansible_get_url_payload_xex4nmel/ansible_get_url_payload.zip/ansible/module_utils/urls.py", line 1494, in fetch_url
unix_socket=unix_socket, ca_path=ca_path)
File "/********/ansible_get_url_payload_xex4nmel/ansible_get_url_payload.zip/ansible/module_utils/urls.py", line 1390, in open_url
unredirected_headers=unredirected_headers)
File "/********/ansible_get_url_payload_xex4nmel/ansible_get_url_payload.zip/ansible/module_utils/urls.py", line 1264, in open
request = RequestWithMethod(url, method, data)
File "/********/ansible_get_url_payload_xex4nmel/ansible_get_url_payload.zip/ansible/module_utils/urls.py", line 643, in __init__
urllib_request.Request.__init__(self, url, data, headers, origin_req_host, unverifiable)
File "/usr/lib64/python3.7/urllib/request.py", line 328, in __init__
self.full_url = url
File "/usr/lib64/python3.7/urllib/request.py", line 354, in full_url
self._parse()
File "/usr/lib64/python3.7/urllib/request.py", line 383, in _parse
raise ValueError("unknown url type: %r" % self.full_url)
fatal: [localhost]: FAILED! => changed=false
invocation:
module_args:
attributes: null
backup: null
checksum: ''
client_cert: null
client_key: null
content: null
delimiter: null
dest: VALUE_SPECIFIED_IN_NO_LOG_PARAMETER
directory_mode: null
follow: false
force: false
force_basic_auth: false
group: VALUE_SPECIFIED_IN_NO_LOG_PARAMETER
headers: null
http_agent: ansible-httpget
mode: null
owner: VALUE_SPECIFIED_IN_NO_LOG_PARAMETER
regexp: null
remote_src: null
selevel: null
serole: null
setype: null
seuser: null
sha256sum: ''
src: null
timeout: 10
tmp_dest: null
unsafe_writes: null
url: VALUE_SPECIFIED_IN_NO_LOG_PARAMETER
url_password: VALUE_SPECIFIED_IN_NO_LOG_PARAMETER
url_username: temp
use_proxy: true
validate_certs: true
msg: 'unknown url type: ''********'''
status: -1
url: VALUE_SPECIFIED_IN_NO_LOG_PARAMETER
[localhost] TASK: utilities : Download package (debug)> p task.args
{'_ansible_check_mode': False,
'_ansible_debug': False,
'_ansible_diff': False,
'_ansible_keep_remote_files': False,
'_ansible_module_name': 'get_url',
'_ansible_no_log': False,
'_ansible_module_name': 'get_url',
'_ansible_no_log': False,
'_ansible_remote_tmp': '~/.ansible/tmp',
'_ansible_selinux_special_fs': ['fuse',
'nfs',
'vboxsf',
'ramfs',
'9p',
'vfat'],
'_ansible_shell_executable': '/bin/sh',
'_ansible_socket': None,
'_ansible_string_conversion_action': 'warn',
'_ansible_syslog_facility': 'LOG_USER',
'_ansible_tmpdir': '/home/ec2-user/.ansible/tmp/ansible-tmp-1588762539.1880841-18280-15848856136034/',
'_ansible_verbosity': 3,
'_ansible_version': '2.9.7',
'dest': 'tmp',
'group': 'tmp',
'owner': 'tmp',
'url': 'tmp',
'url_password': 'tmp',
'url_username': 'temp'}
推荐答案
请尝试以下两种方法,注意name和name之间的空格角色名称实用程序,而且我使用的是 ansible 2.9.1,include_role 没有静态选项.
please try below two methods, please notice the blank space between name and the role name utilities, and also I am using ansible 2.9.1, there is no static option for include_role.
如果您还有其他问题,请告诉我.
if you have further questions, please let me know.
---
- name: run role utilities
hosts: localhost
gather_facts: no
tasks:
- include_role:
name: utilities
tasks_from: d_pack
我强烈建议您在编写 ansible playbook 时遵循以下模式,这是 ansible 官方文档推荐的.每个任务都有一个任务名称.请参考 https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html#always-name-tasks
I highly recommend you follow below patten when write ansible playbook, this is recommend by ansible official doc. Each task have a task name. please refer https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html#always-name-tasks
---
- name: run role utilities
hosts: localhost
gather_facts: no
tasks:
- name: run the role
include_role:
name: utilities
tasks_from: d_pack
这篇关于ansible include_role 语法问题虽然流程文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!