简体   繁体   English

抑制 Ansible 临时警告

[英]Suppress Ansible Ad Hoc Warning

I have a python script that takes advantage of an Ansible ad hoc command to get host information quickly.我有一个 python 脚本,它利用 Ansible ad hoc 命令快速获取主机信息。 I'd like to suppress the warning when I'm attempting to gather information about a host that is in a different VPC, but shows in the following command used to find all instances:当我尝试收集有关位于不同 VPC 中的主机的信息时,我想取消警告,但在用于查找所有实例的以下命令中显示:

aws ec2 describe-instances

Below is the python snippet I'm using to make and generate the ansible ad hoc command:下面是我用来制作和生成 ansible ad hoc 命令的 python 片段:

command_string = "ansible -i /repo/ansible/inventory/"+env+"/hosts " + name + " -m shell -a 'df -h'"
result = subprocess.Popen(command_string, shell=True, stdout=subprocess.PIPE).stdout.read()

I understand that in a playbook setting for the shell module:我知道在 shell 模块的剧本设置中:

warn=no

will disable warnings, but I can't seem to figure out how to do so via adhoc, see below test:将禁用警告,但我似乎无法弄清楚如何通过 adhoc 这样做,请参阅下面的测试:

[root@box-1b 10.0.5.xxx:~] ansible -i /repo/ansible/inventory/nqa/hosts 10.19.1.17 -m shell -a 'warn=no'
[WARNING]: No hosts matched, nothing to do

[root@box-1b 10.0.5.xxx:~] ansible -i /repo/ansible/inventory/nqa/hosts 10.19.1.17 -m shell -a 'warn=false'
[WARNING]: No hosts matched, nothing to do

The output of my full script looks similar to the following:我的完整脚本的输出类似于以下内容:

i-xxxxxx
    my-super-cool-box
    t2.small    True
    10.0.0.10
    vol-xxxxxxx
    100
    i-xxxxxxx
    /dev/xvdf


 [WARNING]: No hosts matched, nothing to do
 [WARNING]: No hosts matched, nothing to do
 [WARNING]: No hosts matched, nothing to do

The information printed about the specific instance is correct, and all I'm looking for is a way to suppress that warning without changing the global ansible configurations.打印的有关特定实例的信息是正确的,我正在寻找的只是一种在不更改全局 ansible 配置的情况下抑制该警告的方法。

This warning has nothing common with command / shell module warnings, which you can control with warn: no .此警告与command / shell模块警告没有任何共同之处,您可以使用warn: no来控制。

This warning is printed by adhoc CLI when you provide host pattern that doesn't match any host from your inventory.当您提供的主机模式与清单中的任何主机都不匹配时, adhoc CLI 会打印此警告。

In your example host 10.19.1.17 is not defined in /repo/ansible/inventory/nqa/hosts inventory, so Ansible gives you warning that there's nothing to do.在您的示例中,主机10.19.1.17未在/repo/ansible/inventory/nqa/hosts清单中定义,因此 Ansible 会警告您无事可做。

Make sure that either you run Ansible with hosts that do exist in your static inventory file, or setup ec2 dynamic inventory and run Ansible against all EC2 instances or filter by tag, security_group, etc.确保您使用静态清单文件中确实存在的主机运行 Ansible,或者设置ec2 动态清单并对所有 EC2 实例运行 Ansible,或者按标签、security_group 等进行过滤。

This is worked for me ANSIBLE_PYTHON_INTERPRETER=auto_silent这对我ANSIBLE_PYTHON_INTERPRETER=auto_silent

example: ANSIBLE_PYTHON_INTERPRETER=auto_silent ansible -i /repo/ansible/inventory/nqa/hosts 10.19.1.17 -m shell -a "uptime"例如: ANSIBLE_PYTHON_INTERPRETER=auto_silent ansible -i /repo/ansible/inventory/nqa/hosts 10.19.1.17 -m shell -a "uptime"

像这样将环境变量ANSIBLE_DEPRECATION_WARNINGS设置为false

ANSIBLE_DEPRECATION_WARNINGS=false ansible -i /path/to/inventory hosts -a your_command
ANSIBLE_COMMAND_WARNINGS=false ansible ...
ANSIBLE_COMMAND_WARNINGS=no ansible ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM