Kar*_*arl 68 ansible ansible-role
假设我有一个名为"apache"的角色
现在我想从Ansible主机的命令行在主机192.168.0.10上执行该角色
ansible-playbook -i "192.168.0.10" --role "path to role"
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
小智 71
我不知道这个功能,但你可以使用标签从你的剧本中运行一个角色.
roles:
- {role: 'mysql', tags: 'mysql'}
- {role: 'apache', tags: 'apache'}
ansible-playbook webserver.yml --tags "apache"
Run Code Online (Sandbox Code Playgroud)
Jul*_*kis 36
使用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>"
}
Run Code Online (Sandbox Code Playgroud)
这将从/ path /到/ ansible/roles或配置的角色路径运行角色.
在这里阅读更多内容:https: //github.com/ansible/ansible/pull/43131
Kon*_*rov 20
在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
Run Code Online (Sandbox Code Playgroud)
Sas*_*nko 12
您还可以检查ansible-toolbox存储库.它允许你使用类似的东西
ansible-role --host 192.168.0.10 --gather --user centos --become my-role
Run Code Online (Sandbox Code Playgroud)
从 ansible 2.4 开始,有两个选项可用:import_role和include_role。
wohlgemuth@leela:~/workspace/rtmtb-ansible/kvm-cluster$ ansible localhost -m import_role -a name=rtmtb\n [WARNING]: No inventory was parsed, only implicit localhost is available\n\nlocalhost | CHANGED => {\n "changed": true, \n "checksum": "d31b41e68997e1c7f182bb56286edf993146dba1", \n "dest": "/root/.ssh/id_rsa.github", \n "gid": 0, \n "group": "root", \n "md5sum": "b7831c4c72f3f62207b2b96d3d7ed9b3", \n "mode": "0600", \n "owner": "root", \n "size": 3389, \n "src": "/home/wohlgemuth/.ansible/tmp/ansible-tmp-1561491049.46-139127672211209/source", \n "state": "file", \n "uid": 0\n}\nlocalhost | CHANGED => {\n "changed": true, \n "checksum": "1972ebcd25363f8e45adc91d38405dfc0386b5f0", \n "dest": "/root/.ssh/config", \n "gid": 0, \n "group": "root", \n "md5sum": "f82552a9494e40403da4a80e4c528781", \n "mode": "0644", \n "owner": "root", \n "size": 147, \n "src": "/home/wohlgemuth/.ansible/tmp/ansible-tmp-1561491049.99-214274671218454/source", \n "state": "file", \n "uid": 0\n}\n\nRun Code Online (Sandbox Code Playgroud)\nansible.builtin.import_role \xe2\x80\x93 将角色导入剧中
\nansible.builtin.include_role \xe2\x80\x93 加载并执行角色
\n你试过吗?超级酷。我使用 'update-os' 而不是 'apache' 角色来给出一个更有意义的例子。我有一个角色叫做让我们说./roles/update-os/在我./添加一个名为的文件./role-update-os.yml,它看起来像:
#!/usr/bin/ansible-playbook
---
- hosts: all
gather_facts: yes
become: yes
roles:
- update-os
Run Code Online (Sandbox Code Playgroud)
使该文件可执行 ( chmod +x role-update-os.yml)。现在您可以运行并限制您库存中./update-os.yml -i inventory-dev --limit 192.168.0.10的任何内容,您也可以传递组名称。
--limit web,db > web 和 db 是在您的清单中定义的组--limit 192.168.0.10,192.168.0.201$ cat inventory-dev
[web]
192.168.0.10
[db]
192.168.0.201
Run Code Online (Sandbox Code Playgroud)
请注意,您可以将 ssh-keys 和 sudoers 策略配置为无需输入密码即可执行 - 非常适合自动化,这有安全隐患。因此你必须分析你的环境,看看它是否合适。