Ansible - read inventory hosts and variables to group_vars/all file

前端 未结 7 1797
醉酒成梦
醉酒成梦 2020-12-23 11:47

I have a dummy doubt that keeps me stuck for a long time. I have a very banal inventory file with hosts and variables:

[lb]
10.112.84.122

[tomcat]
10.112.84         


        
相关标签:
7条回答
  • 2020-12-23 12:26

    If you want to refer one host define under /etc/ansible/host in a task or role, the bellow link might help:

    https://www.middlewareinventory.com/blog/ansible-get-ip-address/

    0 讨论(0)
  • 2020-12-23 12:29

    Just in case if the problem is still there, You can refer to ansible inventory through ‘hostvars’, ‘group_names’, and ‘groups’ ansible variables.

    Example:

    To be able to get ip addresses of all servers within group "mygroup", use the below construction:

    - debug: msg="{{ hostvars[item]['ansible_eth0']['ipv4']['address'] }}" 
      with_items:
         - "{{ groups['mygroup'] }}"
    
    0 讨论(0)
  • 2020-12-23 12:31

    Yes the example by nixlike works very well.

    Inventory:

    [docker-host]
    myhost1 user=barbara
    myhost2 user=heather
    

    playbook:

    ---
    
    - hosts: localhost
      connection: local         
    
         tasks:    
            - name: loop debug inventory hostnames
              debug: 
                msg: "the docker host is {{ item }}"
              with_inventory_hostnames: docker-host
            - name: loop debug items
              debug: 
                msg: "the docker host is {{ hostvars[item]['user'] }}"
              with_items: "{{ groups['docker-host'] }}"
    

    output:

    ansible-playbook ansible/tests/vars-test-local.yml

    PLAY [localhost]


    TASK [setup] ******************************************************************* ok: [localhost]

    TASK [loop debug inventory hostnames] ****************************************** ok: [localhost] => (item=myhost2) => { "item": "myhost2", "msg": "the docker host is myhost2" } ok: [localhost] => (item=myhost1) => { "item": "myhost1", "msg": "the docker host is myhost1" }

    TASK [loop debug items] ******************************************************** ok: [localhost] => (item=myhost1) => { "item": "myhost1", "msg": "the docker host is barbara" } ok: [localhost] => (item=myhost2) => { "item": "myhost2", "msg": "the docker host is heather" }

    PLAY RECAP ********************************************************************* localhost : ok=3 changed=0 unreachable=0
    failed=0

    thanks!

    0 讨论(0)
  • 2020-12-23 12:33
    - name: host
       debug: msg="{{ item }}" 
       with_items:
        - "{{ groups['tests'] }}"
    

    This piece of code will give the message:

    '10.112.84.122'
    '10.112.84.124'
    

    as groups['tests'] basically return a list of unique ip addresses ['10.112.84.122','10.112.84.124'] whereas groups['tomcat'][0] returns 10.112.84.124.

    0 讨论(0)
  • 2020-12-23 12:34

    Considering your previous example:

    inventory file:

    [db]
    10.112.83.37
    

    group_vars/all

    data_base_url=jdbc:oracle:thin:@{{ db }}:1521/ssdenwdb
    

    template file:

    oracle_url = {{ data_base_url }}
    

    You might want to replace your group_vars/all with

    data_base_url="jdbc:oracle:thin:@{{ groups['db'][0] }}:1521/ssdenwdb"
    
    0 讨论(0)
  • 2020-12-23 12:48

    If you want to have your vars in files under group_vars, just move them here. Vars can be in the inventory ([group:vars] section) but also (and foremost) in files under group_vars or hosts_vars.

    For instance, with your example above, you can move your vars for group tests in the file group_vars/tests :

    Inventory file :

    [lb]
    10.112.84.122
    
    [tomcat]
    10.112.84.124
    
    [jboss5]
    10.112.84.122
    
    ...
    
    [tests:children]
    lb
    tomcat
    jboss5
    
    [default:children]
    tests
    

    group_vars/tests file :

    data_base_user=NETWIN-4.3
    data_base_password=NETWIN
    data_base_encrypted_password=
    data_base_host=10.112.69.48
    data_base_port=1521
    data_base_service=ssdenwdb
    data_base_url=jdbc:oracle:thin:@10.112.69.48:1521/ssdenwdb
    
    0 讨论(0)
提交回复
热议问题