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

时间:2016-07-13 11:38:07

标签: ansible ansible-role

假设我有一个名为" apache"

的角色

现在我想从Ansible主机

的命令行在主机192.168.0.10上执行该角色
ansible-playbook -i  "192.168.0.10" --role  "path to role"

有办法吗?

8 个答案:

答案 0 :(得分:56)

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

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

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

答案 1 :(得分:26)

使用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

答案 2 :(得分:20)

在Ansible中没有这样的东西,但如果这是一个经常使用的案例,请尝试这个脚本 将其放在名为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

答案 3 :(得分:10)

您还可以检查Mardoxx存储库。它允许你使用像

这样的东西
ansible-role --host 192.168.0.10 --gather --user centos --become my-role

答案 4 :(得分:9)

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

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

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

答案 5 :(得分: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
}

答案 6 :(得分:0)

您尝试过吗?超级酷。我使用的是“ 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

;./inventory-dev
[web]
192.168.0.10

[db]
192.168.0.201

请注意,您可以将ssh-keys和sudoers策略配置为能够执行而不必键入密码-对于自动化非常理想,这涉及安全性。因此,您必须分析您的环境以查看它是否合适。

答案 7 :(得分:0)

是的,import_role 是一个 ansible 模块,因此它可以通过 ansible 命令调用。以下在 pki

上执行角色 my_server
ansible my_server -m import_role \
                  -a "name=pki tasks_from=gencert" \
                  -e cn=etcdctl \
                  -e extended_key_usage=clientAuth