Ansible:我可以从命令行执行角色吗?

问题描述 投票:56回答:6

假设我有一个名为“apache”的角色

现在我想从Ansible主机的命令行在主机192.168.0.10上执行该角色

ansible-playbook -i  "192.168.0.10" --role  "path to role"

有没有办法做到这一点?

ansible ansible-role
6个回答
66
投票

我不知道这个功能,但你可以使用标签从你的剧本中运行一个角色。

roles:
    - {role: 'mysql', tags: 'mysql'}
    - {role: 'apache', tags: 'apache'}

ansible-playbook webserver.yml --tags "apache"

31
投票

使用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 /到/ ansible / roles或配置的角色路径运行角色。

在这里阅读更多:https://github.com/ansible/ansible/pull/43131


20
投票

在Ansible中没有这样的东西,但如果这是一个经常使用的情况,请尝试这个脚本。 将它放在名为ansible-role的可搜索PATH中的某个位置:

#!/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

10
投票

您还可以查看ansible-toolbox存储库。它允许你使用类似的东西

ansible-role --host 192.168.0.10 --gather --user centos --become my-role

9
投票

我写了一个小的Ansible插件,名为auto_tags,它为你的剧本中的每个角色动态生成一个同名的标签。你可以找到它here

在安装之后(说明在上面的要点中),您可以执行特定角色:

ansible-playbook -i "192.168.0.10" --tags "name_of_role"


0
投票

在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
}


0
投票

你试过吗?这太酷了。我使用'update-os'代替'apache'角色来提供更有意义的示例。我有一个名为let的说法./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> web和db是您的库存中定义的组 --limit 192.168.0.10,192.168.0.201

;./inventory-dev
[web]
192.168.0.10

[db]
192.168.0.201

请注意,您可以配置ssh-keys和sudoers策略,以便能够执行而无需键入密码 - 这对于自动化非常理想,这会对此产生安全隐患。因此,您必须分析您的环境,看它是否合适。

© www.soinside.com 2019 - 2024. All rights reserved.