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
Related
I'm with a problem when running lineinfile command. I've written the following script, which works like a charm:
- hosts: centos1
user: root
tasks:
- name: Replace nsswitch.conf passwd configuration
lineinfile:
path: /etc/nsswitch.conf
regexp: "^passwd:" #line to be searched
line: "passwd: xxx yyy zzz" # new line replacing the above
state: present
create: True
when: ansible_os_family == 'RedHat' and ansible_distribution_major_version|int >= 8
but on the prod server, yml scripts are summoned by a "main.yml", more or less like this:
---
include: script1.yml
include: script2.yml
include: myscript.yml
...
and then I was requested by my senior analyst to remove the "hosts", "user" and "tasks" from the final version, and add a tag on each lineinfile entry, which looked like this:
- name: Replace nsswitch.conf passwd configuration
lineinfile:
path: /etc/nsswitch.conf
regexp: "^passwd:" #line to be searched
line: "passwd: files sss systemd" # new line replacing the above
state: present
create: True
when: ansible_os_family == 'RedHat' and ansible_distribution_major_version|int >= 8
tags:
- nsswwitch_change
but now when I run the playbook, either through my yml file or the main file, I get the following error:
ansible-playbook 2.9.27
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.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, Aug 13 2020, 02:51:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]
Using /etc/ansible/ansible.cfg as config file
host_list declined parsing /tmp/hosts as it did not pass its verify_file() method
script declined parsing /tmp/hosts as it did not pass its verify_file() method
auto declined parsing /tmp/hosts as it did not pass its verify_file() method
Parsed /tmp/hosts inventory source with ini plugin
ERROR! 'lineinfile' is not a valid attribute for a Play
The error appears to be in '/app/ansible/roles/linux-sap-hana/tasks/raul-mnsswitch.yml': line 26, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Replace nsswitch.conf passwd configuration
^ here
I'm literally out of ideas, because the only way I was able to make it work was adding the host, user and tasks at the beggining of the playbook.
Any help would be appreciated folks, thanks a lot in advance.
If you top-level playbook looks like this:
include: script1.yml
include: script2.yml
include: myscript.yml
Then each of those included documents needs to be a playbook: that means it needs to be a list of plays, not a list of tasks:
- name: play 1
hosts: all
tasks:
...
- name: play 2
hosts: all
tasks:
...
Otherwise, how will Ansible know which hosts to target?
Note that the include module is deprecated:
DEPRECATED
Removed in
version 2.16
Why
it has too many conflicting behaviours depending on keyword combinations and it was unclear how it should behave in each case. new actions were developed that were specific about each case and related behaviours.
Alternative
include_tasks, import_tasks, import_playbook
You should be using import_playbook instead.
If you want to include a list of tasks, you can only do that via the include_tasks or import_tasks modules, as part of task in a play:
- name: play 1
hosts: all
tasks:
- name: import script1
import_tasks: script1.yml
- name: import script2
import_tasks: script2.yml
NB: To make sure you're clear on the nomenclature:
# This entire block is a playbook
# This is a play
- hosts: all
tasks:
# This is a task
- name: Example task 1
debug:
msg: This is an example task
# This is a play
- hosts: all
tasks:
# This is a task
- name: Example task 2
debug:
msg: This is an example task
I have a simple Ansible dynamic inventory for AWS servers that looks like this.
---
plugin: aws_ec2
regions:
- eu-west-2
keyed_groups:
- key: tags.Name
hostnames:
# A list in order of precedence for hostname variables.
- ip-address
compose:
ansible_host: _Applications_
ansible_user: "'ubuntu'"
This works fine, except that I also have another instance that's Redhat.
Which means that when I try to do a simple ping command on all the hosts, it fails as the username ubuntu is only valid on one of the servers.
Is there a way to set group my inventory file so that I can add in the ec2-user username for a specific group maybe based on it's tag or something else.
I could do this easily with my static inventory but I'm not sure how to do this with a dynamic inventory.
I've tried setting my Ansible inventory as an environment variable
export ANSIBLE_INVENTORY=~/Users/inventory
And placed my aws_ec2.yamlin the inventory directory along with a group vars directory containing my different groups with default usernames in each of the different groups
username: ubuntu
username: ec2-user
and then setting my inventory file as such
compose:
ansible_user: "{{ username }}"
But when Ansible tries to connect, it's using an admin username and not what's set in my group vars.
Is there a way to set the different usernames needed to connect to the different type of servers?
Per the example for the constructed plugin, you can use the keyed_group feature to create groups by ansible_distribution:
keyed_groups:
# this creates a group per distro (distro_CentOS, distro_Debian) and assigns the hosts that have matching values to it,
# using the default separator "_"
- prefix: distro
key: ansible_distribution
And then set ansible_user inside groups_vars/distro_Ubuntu.yaml and group_vars/distro_RedHat.yaml.
Also from the documentation, this requires fact caching to operate (because otherwise Ansible doesn't know the value of ansible_distribution at the time it's processing the keyed_groups setting).
I don't have access to AWS at the moment, but here's how I'm testing everything locally. Given an inventory that looks like:
$ tree inventory
inventory/
├── 00-hosts.yaml
└── 10-constructed.yaml
Where inventory/00-hosts.yaml looks like:
all:
hosts:
host0:
ansible_host: localhost
And inventory/10-constructed.yaml looks like:
plugin: constructed
strict: false
groups:
ipmi_hosts: ipmi_host|default(false)
keyed_groups:
- prefix: "distro"
key: ansible_distribution
And ansible.cfg looks like:
[defaults]
inventory = inventory
enable_plugins = constructed
gathering = smart
fact_caching = jsonfile
fact_caching_connection = ./.facts
The first time I run this playbook:
- hosts: all
gather_facts: true
tasks:
- debug:
var: group_names
The output of the debug task is:
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [host0] => {
"group_names": [
"ungrouped"
]
}
But because of the fact gathering and caching performed by the previous playbook run, the second time I run it the output is:
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [host0] => {
"group_names": [
"distro_Fedora"
]
}
Similarly, before the first playbook run ansible-inventory --graph outputs:
#all:
|--#ungrouped:
| |--host0
But after running the playbook once, I get:
#all:
|--#distro_Fedora:
| |--host0
|--#ungrouped:
I've bundled this all into an example repository.
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
I'm trying to perform tasks in the playbook for the hosts mentioned in my inventory file which are grouped under "Jira" But for some reason my group is not being identified to pick. for the content of the files please look below.
How can I run all the tasks mentioned in the playbook with all the hosts in the inventory?
I have an Inventory file with the below contents: Hosts.yml
all: # the all group contains all hosts
hosts:
ansible:
ansible_host: #{ansible-controller}
ansible_user: root
crowd:
ansible_host: #{crowd}
ansible_user: root
jira:
ansible_host1: 53.31.54.56
ansible_host2: 53.31.54.55
I have playbook with content:
---
- name: Install Jira Application
hosts: jira
gather_facts: true
become: true
remote_user: root
roles:
- ansible-preparation
#- jira-applicationsetup
I always get below error message:
root#sedcagse0550:/usr/Anil/InfraAutomation/gsep-infrastructure-automation : ansible-playbook jira-fullinstall.yml
[WARNING]: Could not match supplied host pattern, ignoring: jira
PLAY [Install Jira Application] *************************************************************
skipping: no hosts matched
PLAY RECAP **********************************************************************************
How can I perform all the tasks to all the hosts mentioned in the inventory file?
You should run the ansible-playbook with inventory parameter(-i) like this:
ansible-playbook -i Hosts.yml jira-fullinstall.yml
Otherwise, ansible checks the default inventory file location which is "/etc/ansible/hosts".
If your Hosts.yml already in that location, check your inventory file. In Ansible 2.7 User Guide YAML inventory files look like this:
all:
jira:
hosts:
53.31.54.56:
53.31.54.55:
If i understood it correct, Your inventory file should look like as below::
ansible: # Group Name
hosts:
ansible_host: #Host name
ansible_user: root # Host Variable
crowd:
hosts:
ansible_host: #{crowd}
ansible_user: root
jira:
hosts:
ansible_host1:
ansible_host: 53.31.54.56
ansible_host2:
ansible_host: 53.31.54.55`
Please refer this link for detailed formatting about yaml based inventory
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