假设我有一个名为“ apache”的角色
现在我想从Ansible主机的命令行在主机192.168.0.10上执行该角色
ansible-playbook -i "192.168.0.10" --role "path to role"
有没有办法做到这一点?
Answers:
使用ansible 2.7,您可以执行以下操作:
$ cd /path/to/ansible/
$ ansible localhost -m include_role -a name=<role_name>
localhost | SUCCESS => {
"changed": false,
"include_variables": {
"name": "<role_name>"
}
}
localhost | SUCCESS => {
"msg": "<role_name>"
}
这将从/ path / to / ansible / roles或配置的角色路径运行角色。
在此处阅读更多信息:https : //github.com/ansible/ansible/pull/43131
'ERROR! 'async_val' is not a valid attribute for a IncludeRole'.
-a "name=<role_name> public=yes"
,但没有帮助。
-m <module-name>
)以key = value对作为参数(-a key=value
)
Ansible中没有这样的东西,但是如果这是您经常使用的情况,请尝试使用此脚本。
将其放在名称中您可搜索的PATH中的某个位置ansible-role
:
#!/bin/bash
if [[ $# < 2 ]]; then
cat <<HELP
Wrapper script for ansible-playbook to apply single role.
Usage: $0 <host-pattern> <role-name> [ansible-playbook options]
Examples:
$0 dest_host my_role
$0 custom_host my_role -i 'custom_host,' -vv --check
HELP
exit
fi
HOST_PATTERN=$1
shift
ROLE=$1
shift
echo "Trying to apply role \"$ROLE\" to host/group \"$HOST_PATTERN\"..."
export ANSIBLE_ROLES_PATH="$(pwd)/roles"
export ANSIBLE_RETRY_FILES_ENABLED="False"
ansible-playbook "$@" /dev/stdin <<END
---
- hosts: $HOST_PATTERN
roles:
- $ROLE
END
ansible localhost -m include_role -a name=myrole
-为我工作!
ansible localhost -m include_role -a name=myrole
从ansible 2.8开始被破坏。它只记录隐式错误消息,例如'ERROR! 'async_val' is not a valid attribute for a IncludeRole'.
您还可以检查ansible-toolbox存储库。它将允许您使用类似
ansible-role --host 192.168.0.10 --gather --user centos --become my-role
你有尝试过吗?超级酷。我使用的是“ update-os”而不是“ apache”角色,以给出一个更有意义的示例。我有一个角色,比方说./roles/update-os/
,我./
添加了一个文件./role-update-os.yml
,看起来像:
#!/usr/bin/ansible-playbook
---
- hosts: all
gather_facts: yes
become: yes
roles:
- update-os
使该文件可执行(chmod +x role-update-os.yml
)。现在,您可以运行并限制为库存中./update-os.yml -i inventory-dev --limit 192.168.0.10
的任何物品,也可以通过组名进行限制。
--limit web,db
>网络和数据库是您库存中定义的组--limit 192.168.0.10,192.168.0.201
$ cat inventory-dev
[web]
192.168.0.10
[db]
192.168.0.201
请注意,您可以将ssh-keys和sudoers策略配置为无需输入密码即可执行-对于自动化非常理想,这涉及安全性。因此,您必须分析您的环境以查看其是否合适。
在ansible 2.8中,它的工作方式略有不同
wohlgemuth@leela:~/workspace/rtmtb-ansible/kvm-cluster$ ansible localhost -m import_role -a name=rtmtb
[WARNING]: No inventory was parsed, only implicit localhost is available
localhost | CHANGED => {
"changed": true,
"checksum": "d31b41e68997e1c7f182bb56286edf993146dba1",
"dest": "/root/.ssh/id_rsa.github",
"gid": 0,
"group": "root",
"md5sum": "b7831c4c72f3f62207b2b96d3d7ed9b3",
"mode": "0600",
"owner": "root",
"size": 3389,
"src": "/home/wohlgemuth/.ansible/tmp/ansible-tmp-1561491049.46-139127672211209/source",
"state": "file",
"uid": 0
}
localhost | CHANGED => {
"changed": true,
"checksum": "1972ebcd25363f8e45adc91d38405dfc0386b5f0",
"dest": "/root/.ssh/config",
"gid": 0,
"group": "root",
"md5sum": "f82552a9494e40403da4a80e4c528781",
"mode": "0644",
"owner": "root",
"size": 147,
"src": "/home/wohlgemuth/.ansible/tmp/ansible-tmp-1561491049.99-214274671218454/source",
"state": "file",
"uid": 0
}