相关文章推荐

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. Visit Stack Exchange

DevOps Stack Exchange is a question and answer site for software engineers working on automated testing, continuous delivery, service integration and monitoring, and building SDLC infrastructure. It only takes a minute to sign up.

Sign up to join this community

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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]
[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?

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"

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.

Thanks for contributing an answer to DevOps Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

 
推荐文章