2

I have workflow with 2 templates. First I add new host to my hosts.ini file:

- name: Add to host file
     blockinfile:
       path: /var/lib/awx/projects/_52__glusterfs/hosts.ini
       block: |
         gluster1 ansible_user=<user> ansible_host=<ip>

And second I wait for host up:

---
- name: Weit befor all hosts ssh up
  hosts: localhost
  tasks:
   - name: Test 1
     shell: cat hosts.ini
   - name: Test 2
     shell: cat /var/lib/awx/projects/_52__glusterfs/hosts.ini
   - name: Test 3
     shell: cat ansible.cfg
   - name: Wait for hosts up
     wait_for:
       host: gluster1
       port: 22

But i get eror:

 [WARNING]: provided hosts list is empty, only localhost is available. Note
that the implicit localhost does not match 'all'

So output from tests in template 2 is (pseudocode output):

shell: cat hosts.ini
>>> 
[gluster]

shell: cat /var/lib/awx/projects/_52__glusterfs/hosts.ini
>>> 
[gluster]
gluster1 ansible_user=<user> ansible_host=<ip>

shell: cat ansible.cfg
>>> 
[defaults]
inventory = /var/lib/awx/projects/_52__glusterfs/hosts.ini
host_key_checking = false

[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o UserKnownHostsFile=/dev/null

[inventory]
enable_plugins = host_list, script, auto, yaml, ini, toml

Why I have empty hosts.ini in my execute directory and why ansible use it instead of /var/lib/awx/projects/_52__glusterfs/hosts.ini in my ansible.cfg file?

1

It sounds like you need to gather facts again once you've updated the inventory. You start off with

First I add new host to my hosts.ini file

However, Ansible is not notified about this until you tell it that the inventory has changed. If this is done during a play, you should use the add_host module:

- name: Add host gluster1 to group gluster
  add_host:
    name: gluster1
    groups: gluster

This will update Ansible's facts, i.e. it will contact the new host and find out everything about it, now that it knows it is in the gluster group.

Note that then you should start a new play, against this host:

---
- name: First play to build hosts
  hosts: localhost
  tasks:
    - name: Add host gluster1 to group gluster
      add_host:
        name: gluster1
        groups: gluster
    - name: Persist inventory
      blockinfile:
        path: /var/lib/awx/projects/_52__glusterfs/hosts.ini
        block: |
          gluster1 ansible_user=<user> ansible_host=<ip>
        state: present

- name: Second play to do gluster things
  hosts: gluster  # This group now exists in Ansible's in-memory inventory
  tasks:
    - name: Wait for hosts up
      wait_for:
        host: gluster1
        port: 22
    - name: Be awesome
      debug:
        msg: "Awesomeness Achieved"
2

You have this in your config file:

[inventory]
enable_plugins = host_list, script, auto, yaml, ini, toml

Do you really need all these plugins? If not, they'll just add time to your processing. There is also a small chance that one of them is successfully parsing your inventory file as an empty inventory. And the documentation states that

Once an inventory plugin succeeds at parsing a source, any remaining inventory plugins will be skipped for that source.

If you do not need those plugins, I'd comment that line (or rework it to remove some of the items).

Also, are you sure that it is using the local hosts.ini? The command ansible-config might help confirming that.

Regarding your first question ("Why I have empty hosts.ini in my execute directory"), there is nothing in your playbooks pointing to that file being created or modified. Did you mean to have it modified? Or you don't know how it was created? The question there is not clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.