I have a python script that takes advantage of an Ansible ad hoc command to get host information quickly. 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:
aws ec2 describe-instances
Below is the python snippet I'm using to make and generate the ansible ad hoc command:
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:
warn=no
will disable warnings, but I can't seem to figure out how to do so via adhoc, see below test:
[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.
ACTION_WARNINGS. By default Ansible will issue a warning when received from a task action (module or action plugin) These warnings can be silenced by adjusting this setting to False.
These ad-hoc commands are not used for configuration management and deployment, because these commands are of one time usage. ansible-playbook is used for configuration management and deployment.
To put simply, Ansible ad hoc commands are one-liner Linux shell commands and playbooks are like a shell script, a collective of many commands with logic. Ansible ad hoc commands come handy when you want to perform a quick task.
If you want to continue to use sudo, you can then disable this warning by setting warn to false, like this. Or, you could set comment_warnings to false in ansible. cfg, like this. However, this is probably to permissive, as any command that would generate a warning would no longer generate a warning.
This warning has nothing common with command
/shell
module warnings, which you can control with warn: no
.
This warning is printed by adhoc
CLI when you provide host pattern that doesn't match any host from your inventory.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With