unable to connect to a remote server through ansible playbook - ansible

I'm quite new to ansible and i have a new problem recently. I try to run the playbook and below is the error i get.
[bhar1#desktop ~]$ ansible-playbook -i inv abc.yaml -vvv
ansible-playbook 2.4.2.0
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/home/bhar1/.ansible/plugins /modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages /ansible
executable location = /bin/ansible-playbook
python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
Using /etc/ansible/ansible.cfg as config file
[WARNING]: Unable to parse /home/bhar1/inv as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: Could not match supplied host pattern, ignoring: all
[WARNING]: provided hosts list is empty, only localhost is available
PLAYBOOK: abc.yaml **********************************************************************************************************************************
1 plays in abc.yaml
[WARNING]: Could not match supplied host pattern, ignoring: webservers
PLAY [webservers] ***********************************************************************************************************************************
skipping: no hosts matched
PLAY RECAP ******************************************************************************************************************************************
Can you please help me explain what could be the reason for this. My /etc/ansible/hosts file is updates with webservers group as we. Below is the yml file.
[bhar1#desktop ~]$ cat abc.yaml
---
- hosts: webservers
become: yes
tasks:
- name: remove httpd
yum:
name: httpd
state: absent
[bhar1#desktop ~]$

If you have updated the host group in /etc/ansible/hosts , you need to pass the path with -i, you have passed wrong file as i see its empty , use below and see if it works
ansible-playbook -i /etc/ansible/hosts abc.yaml

Error is displayed because Ansible is unable to find your inventory file.
To execute the playbook abc.yaml, run the following command:
ansible-playbook -i /etc/ansible/hosts abc.yaml
Execution tells the ansible to use inventory file available in /etc/ansible/hosts and to parse playbook file "abc.yaml".Inventory argument -i is used to pass the inventory path.

it seems that ansible looking for wrong inventory file.
does this file "/home/bhar1/inv" exist?
if not, then you should use:
ansible-playbook -i [/path/to/inventory_file] abc.yaml -vvv

Related

Ansible Playbook skipping: no hosts matched

I have a directory where I created ansible.cfg, inventory_win and windesk.yml(my playbook).
In ansible.cfg I defined the following
[defaults]
host_key_checking=False
ansible_python_interpreter= /usr/bin/python3
hostfile = inventory_win
Inside inventory_win I have
myfirsthost ansible_host=192.168.10.30
my2ndthost2 ansible_host=192.168.10.34
[windesk]
myfirsthost
my2ndthost2
[datacenter:children]
windesk
In windesk.yml, I have
---
- hosts: windesk
vars:
ansible_site_path: "c:\\inetpub\\wwwroot\\some\\"
vsite: "Default Web Site"
vsiteindexfile: "index3.html"
tasks:
- name: create new website's directory
win_file: path={{ ansible_site_path }} state=directory
- name: Create a virtual directory if it does not exist
win_iis_virtualdirectory:
name: somedirectroy
site: "{{ vsite }}"
state: present
physical_path: "{{ ansible_site_path }}"
When I run
ansible-playbook windesk.yaml
I get
[WARNING]: provided hosts list is empty, only localhost is available.
Note that the implicit localhost does not match 'all' [WARNING]: Could
not match supplied host pattern, ignoring: windesk
PLAY [windesk]
******************************************************************************************** skipping: no hosts matched
PLAY RECAP
but running
ansible-playbook -i inventory_win windesk.yaml
it works fine and I get the expected results
Your definition in the ansible.cfg is incorrect. The docs detail that the key name should be inventory not hostfile
Description
Comma separated list of Ansible inventory sources
Type
pathlist
Default
/etc/ansible/hosts
Ini
Section
[defaults]
Key
inventory
[defaults]
host_key_checking = False
ansible_python_interpreter =/ usr/bin/python3
inventory = inventory_win
OUTPUT
[centos#vps-f116ed9f stack_ansible]$ ansible-playbook windesk.yml -vvv
ansible-playbook 2.9.27
config file = /home/centos/stack_ansible/ansible.cfg
configured module search path = [u'/home/centos/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 2.7.5 (default, Jun 28 2022, 15:30:04) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
Using /home/centos/stack_ansible/ansible.cfg as config file
host_list declined parsing /home/centos/stack_ansible/inventory_win as it did not pass its verify_file() method
script declined parsing /home/centos/stack_ansible/inventory_win as it did not pass its verify_file() method
auto declined parsing /home/centos/stack_ansible/inventory_win as it did not pass its verify_file() method
Parsed /home/centos/stack_ansible/inventory_win inventory source with ini plugin

Ansible inventory can't be parsed

I am trying to play my first playbook on the new local machine.
I have lost my previous one, so I am not able to look into it's configuration files.
I have prepared two virtual machines for testing Ansible.
The first one is an Ansible machine with whole config and playbooks, etc. The second one is the machine on which I want to make changes with playbook.
I've got the error: No inventory was parsed, only implicit localhost is available, but I declare inventory file in the command line:
ansible-playbook -i inventory/dockers.yml yum-update.yml
I read in Ansible documentation that config in /etc/ansible shouldn't be taken into consideration, because I used the -i option during command execution.
This is my inventory file. I believe that there must be something there, but I can't see it:
all:
hosts:
machine02:
ansible_host: 192.168.1.16
yumek:
hosts:
machine02:
vars:
ansible_user: test
remote_user: root
The output of the [ansible1#host01 ~]$ ansible-inventory --list is:
[WARNING]: * Failed to parse /etc/ansible/hosts with yaml plugin: Parsed empty YAML file
[WARNING]: * Failed to parse /etc/ansible/hosts with constructed plugin: /etc/ansible/hosts is empty
[WARNING]: Unable to parse /etc/ansible/hosts as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"ungrouped"
]
}
}
Thanks in advance.
I played playbook with option -i from $HOME location of non-root user, despite that Ansible did not use indicated inventory.
Solution was very simple, just changing inventory location path in the main config /etc/ansible/ansible.cfg on root account to:
inventory = ~/inventory_folder
Unfortunately, still I do not know why Ansible did not respect -i option in the command.

How to run ansible on specific host: ansible-playbook -i <hostname>,

The docs specify that I can run my playbook on a specific host using -i:
Patterns and ansible-playbook flags
You can change the behavior of the patterns defined in playbooks using command-line options. For example, you can run a playbook that defines hosts: all on a single host by specifying -i 127.0.0.2,. This works even if the host you target is not defined in your inventory. You can also limit the hosts you target on a particular run with the --limit flag:*
However, I tried running ansible-playbook <playbook> -i <new_hostname> -u <username> and the inventory used was still my default one. How to use this correctly?
Quoting from "man ansible"
-i, --inventory, --inventory-file
specify inventory host path or comma separated host list.
To specify a single host as a "comma separated host list", the comma is still needed. For example, the playbook
shell> cat playbook.yml
- hosts: all
gather_facts: false
tasks:
- debug:
var: inventory_hostname
gives
shell> ansible-playbook -i test_99, playbook.yml
PLAY [all] ****
TASK [debug] ****
ok: [test_99] =>
inventory_hostname: test_99
Without the comma after the host, Ansible takes the argument as an "inventory host path".
shell> ansible-playbook -i test_99 playbook.yml
[WARNING]: Unable to parse /scratch/test_99 as an inventory source
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
PLAY [all] ****
skipping: no hosts matched

Ansible issuing warning about localhost

I am running the following ansible playbook
- hosts: localhost
connection: local
vars_files:
- vars/config_values.yaml
gather_facts: no
tasks:
- name: Set correct project in gcloud config
shell: "gcloud config set project {{ google_project_name }}"
Which yields the following warning:
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
Given that I am explicitly stating that it will be run against host: localhost, why is it complaining about no inventory being parsed and that the "provided host list is empty"?
How to remove these warnings? (without just suppressing them if possible)
These are just warnings telling you that:
you did not provide any inventory file either with the -i option, in your ansible.cfg or by default in /etc/ansible/hosts (and hence none was parsed and it is empty)
your inventory is empty and hence only the implicit localhost is available (reminding you that it does not match the all group)
additional notes
hosts: localhost in your above playbook example is the target for your play. It could be an other host or a group (or a more complicated pattern). The targeted hosts must exist in the inventory to be managed. localhost always exists at least as the implicit local machine.
You can read Ansible introduction to inventories for more information
Although those two warning seem a bit redundant, they give different information (i.e. "no inventory at all" vs "inventory is simply empty"). As such they are governed by different config options
The second warning can be silenced since ansible v2.6 with the LOCALHOST_WARNING option
The first warning can be silenced since ansible-core v2.14 with the INVENTORY_UNPARSED_WARNING option
For the records, the first feature as been added to Ansible by a pull request proposed by #larsk, a stackoverflow user
So if you intend to run playbooks primarily targeted to localhost without giving any inventory to parse, you can silence both warnings as in the following one-liner (see links above for all options to set those vars)
ANSIBLE_LOCALHOST_WARNING=False \
ANSIBLE_INVENTORY_UNPARSED_WARNING=False \
ansible-playbook your_localhost_playbook.yml
As a non-expert. in Ansible, I suppressed these messages by adding localhost on the /etc/ansible/hosts. Then set hosts: localhost on the playbook file. You should be good and won't be seeing those warning messages.
Hosts file
[ec2-user#ip-192-168-1-38 ~]$ sudo head -20 /etc/ansible/hosts
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups
# Ex 1: Ungrouped hosts, specify before any group headers.
localhost
## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10
# Ex 2: A collection of hosts belonging to the 'webservers' group
## [webservers]
Playbook file
[ec2-user#ip-192-168-1-38 ~]$ cat msteams.yml
- name: MSTeams
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: MSTeams Sending
uri:
url: https://outlook.office.com/webhook/asdasda-asdasd-40sdfd-btryrty2-posadjfm,sdnfoersmdnrfoasf,mvmnmtrny40956839523842/IncomingWebhook/102938091283kdjoasdyo214/vnmlasdhfaosldnaehywe
method: POST
body: "{\"text\": \"**Header:** {{Header}}\n\n **Next Line Message1:** {{MESSAGE1}}\n\n **Next Line Message2:** '{{ MESSAGE2 }}'\", \"themeColor\": \"{{COLOR}}\"}"
body_format: json
headers:
Content-Type: "application/json"
Command Result
[ec2-user#ip-192-168-1-38 ~]$ ansible-playbook msteams.yml -e "Header=TEST" -e "MESSAGE1=Message_without_space" -e ' MESSAGE2="Message with space" ' -e "COLOR=##FF0000"
PLAY [MSTeams] ***********************************************************************************************************************************************************************************************
TASK [MSTeams Sending] ************************************************************************************************************************************************************************************
ok: [localhost]
PLAY RECAP ****************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
As of ansible 2.6 you can also run ANSIBLE_LOCALHOST_WARNING=false ansible-playbook /path/to/your/playbook.yaml or set "localhost_warning=false" in your ansible.cfg file
https://docs.ansible.com/ansible/latest/reference_appendices/config.html#localhost-warning

How to Skip hosts repository and specify host Runtime in Ansible

I do not wish to specify any hosts file to ansible-playbook command.
ansible-playbook site.yml -e "source_host=mymac1 source_file=myfile1"
My site.yml looks like this:
more site.yml
---
- hosts: "{{ source_host | default('my_pc')}}"
user: weblogic
However, I get the following error:
[WARNING]: Could not match supplied host pattern, ignoring: all
[WARNING]: provided hosts list is empty, only localhost is available
PLAYBOOK: site.yml
********************************************************************************************************************************************************************************** 2 plays in site.yml [WARNING]: Could not match supplied host pattern,
ignoring: mymac1
Can you please suggest how can i pass any host to my playbook without having to maintain and host respository with all the host information
I am on ansible version 2.3.1.0
You can use inline inventory:
playbook.yml:
- hosts: all
tasks:
- debug: msg=hello
command:
ansible-playbook -i 'mymac1,' -e source_file=myfile1 playbook.yml
note comma after hostname.
Also see: Ansible ad-hoc command with direct host specified - no hosts matched

Resources