Ansible 安全 之【过滤危险命令】

ansible对模块和命令做限制

vim /usr/local/python3/lib/python3.8/site-packages/ansible/playbook/play.py
...
from ansible.parsing.splitter import parse_kv
# 写在类的上面,否则调用不到(一定要写在全局)
def filter_cmd(data):
    filter_modules = ('command', 'shell', 'script', 'raw')
    filter_commands = ('rm -rf /','halt', 'poweroff', 'reboot', 'shutdown -h now','shutdown -r now','hostname')
    filter_commands = map(lambda x:x.replace(' ', '').lower(), filter_commands)
    for t in data['tasks']:
        if 'action' in t:
            if t['action']['module'] in filter_modules:
                if t['action']['args']['_raw_params'].replace(' ', '').lower() in filter_commands:
                    raise AnsibleParserError("Refused to execute the [%s] command in the [%s] module." % (t['action']['args']['_raw_params'], t['action']['module']))
    else:
        for m in filter_modules:
            if m in t:
                args=parse_kv(t[m], check_raw=True)
                if args['_raw_params'].replace(' ', '').lower() in filter_commands:
                    raise AnsibleParserError("Refused to execute the [%s] command in the [%s] module." % (t[m], m))

...
	# 在Play类的load方法中引用filter_cmd过滤命令
	# 在p = Play()上方添加filter_cmd(data)
    @staticmethod
    def load(data, variable_manager=None, loader=None, vars=None):
        if ('name' not in data or data['name'] is None) and 'hosts' in data:
            if data['hosts'] is None or all(host is None for host in data['hosts']):
                raise AnsibleParserError("Hosts list cannot be empty - please check your playbook")
            if isinstance(data['hosts'], list):
                data['name'] = ','.join(data['hosts'])
            else:
                data['name'] = data['hosts']
        filter_cmd(data)
        p = Play()
        if vars:
            p.vars = vars.copy()
        return p.load_data(data, variable_manager=variable_manager, loader=loader)

测试

ansible all -m shell -a "hostname"
# ERROR! Refused to execute the [hostname] command in the [shell] module.
ansible all -m shell -a "reboot"
# ERROR! Refused to execute the [reboot] command in the [shell] module.
ansible all -m shell -a "rm -rf /"
# ERROR! Refused to execute the [rm -rf /] command in the [shell] module.

测试ansible的yaml文件执行

cd /etc/ansible/ansible-playbook/
vim test.yml
- host: linux
  tasks:
    - name: test01
      shell: hostname

ansible-playbook test.yml 
# ERROR! Refused to execute the [hostname] command in the [shell] module.
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当您在控制节点上使用ansible命令时,如果系统提示没有找到ansible命令,可能是因为ansible没有正确安装或者没有将ansible的可执行文件路径添加到系统的环境变量中。 首先,确保您已经正确安装了ansible。您可以通过在终端上执行以下命令来检查ansible是否已安装: ``` ansible@control ansible]$ ansible-doc yum_repository ``` 如果没有找到ansible-doc命令,那么您需要按照ansible的安装指南进行安装。根据您的操作系统,可以使用包管理器(如yum,apt-get等)进行安装,也可以从ansible的官方网站下载安装包进行手动安装。 如果ansible已经正确安装,但仍然提示找不到ansible命令,那么可能是因为ansible的可执行文件路径没有添加到系统的环境变量中。您可以通过以下步骤来添加环境变量: 1. 打开终端,并使用以下命令查找ansible可执行文件的路径: ``` ansible@control ansible]$ ansible-doc -l | grep yum ``` 2. 将找到的ansible可执行文件路径添加到您的系统环境变量中。具体方法会根据您使用的操作系统而有所不同。例如,在Linux系统中,您可以将以下命令添加到您的`~/.bashrc`文件中: ``` export PATH=$PATH:/path/to/ansible/bin ``` 然后,重新加载您的bash配置文件: ``` source ~/.bashrc ``` 3. 现在尝试再次运行ansible命令,您应该不再收到找不到ansible命令的提示。 另外,关于您提到的使用yum模块进行安装的问题,您可以通过以下命令来安装或升级软件包: ``` ansible node1 -m yum -a 'name=httpd state=latest' ``` 这条命令会在目标主机上使用yum模块安装或升级名为httpd的软件包。如果软件包已经安装并且已经是最新版本,那么ansible会返回一个"changed=false"的结果,表示命令已经执行但没有实际效果。这也体现了ansible的幂等性,即多次执行相同的命令不会产生额外的变化。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值