先整理几张图 发展趋势 上图 阶段 devops运维开发 # vim hosts.j2 * * {% for host in groups['all'] %} {{ hostvars[host]['ansible_facts ']['default_ipv4']['address'] }} {{ hostvars[host]['ansible_facts']['fqdn'] }} {{ hostvars[host]['ansible_facts
[host].inventory_hostname }}:{{ hostvars[host].metaport }}{%else%}{{hostvars[host].inventory_hostname }}:{{hostvars[host].metaport}} ,{%endif%}{% endfor %} --local_ip={{inventory_hostname}} --listen_netdev [host].inventory_hostname }}:{{ hostvars[host].metaport }}{%else%}{{hostvars[host].inventory_hostname [host].inventory_hostname }}:{{ hostvars[host].metaport }}{%else%}{{hostvars[host].inventory_hostname }}:{{hostvars[host].metaport}} ,{%endif%}{% endfor %} --local_ip={{inventory_hostname}} --listen_netdev
def hostvarformat(hostname,hostvar,hostvardict): try: hostvardict["_meta"]["hostvars"] # except: hostvardict={"_meta":{"hostvars":{}}} #Create the dict first . hostvardict["_meta"]["hostvars"].update({hostname:{}}) #Update the dict with hostname. hostvardict["_meta"]["hostvars"][hostname].update(hostvar) #Update the dict with sub dict hostvar
zhuimaTestfornode", } }, "_meta": { "hostvars { "ansible_ssh_host": "127.0.0.1", "_meta": { "hostvars "ansible_ssh_user": "zhuima" }, }, '_meta': { 'hostvars }, } def host_info(self): """resp for --host or -h""" return {'_meta': {'hostvars "ansible_ssh_user": "zhuima" } }, "_meta": { "hostvars
false tasks: - name: add DNS lineinfile: path: /etc/hosts line: "{{item}} {{hostvars : "a.txt" line: "LINE2" insertafter: '^para.* 2' firstmatch: yes play_hosts和hostvars 变量 inventory_hostname: 表示在主机inventory中定义的名称 play_hosts和hostvars: 是预定义变量,执行任务时可以直接拿出来使用,play_hosts相当于是 new这个主机组里面的所有主机列表; hostvars: 保存了所有目标主机的变量 - name: add DNS lineinfile: path: /etc/hosts line: "{{item}} {{hostvars[item].ansible_hostname}}" when: item !
bastion-ssh-config/tasks/main.yml --- - name: set bastion host IP and port set_fact: bastion_ip: "{{ hostvars [groups['bastion'][0]]['ansible_host'] | d(hostvars[groups['bastion'][0]]['ansible_ssh_host']) }}" bastion_port: "{{ hostvars[groups['bastion'][0]]['ansible_port'] | d(hostvars[groups['bastion'][0]]['
var_iter.yaml - hosts: all remote_user: root tasks: - name: loop block: - debug: msg: "{{ hostvars ansible_default_ipv4']['address'] }}" with_items: "{{ groups['all'] }}" - debug: msg: "{{ hostvars [item]['ansible_hostname']}}" with_items: "{{ groups['all'] }}" - debug: msg: "{{ hostvars
facts信息[yes|no] 实际案例1:hostvars 与 play_hosts内置变量联合使用 # cat > gatherfcats.yml <<end --- - name: "Play1 其实除了facts信息,我们还能够利用hostvars内置变量从别的主机中获取到其他类型的一些变量信息,比如其他主机的注册变量、主机变量、组变量等信息,还有就是注册变量并不用像facts信息那样需要事先收集 ,即可直接通过hostvars跨主机被引用到; 实际案例2:hostvars 与 gather_fcats 与 set_fact模块案例: # cat > gatherfcats1.yml <<end : {{hostvars.local.var1}} , Play 1 register shell command: {{hostvars.local.shellrst['cmd']}}" #但是可以采用 hostvars来进行跨主机引用set_fact模块设置的变量 #msg: "play 1 中的 {hostvars['local'].ansible_default_ipv4 不能被调用由于根本没有进行
facts信息[yes|no] 实际案例1:hostvars 与 play_hosts内置变量联合使用 # cat > gatherfcats.yml <<end --- - name: "Play1 其实除了facts信息,我们还能够利用hostvars内置变量从别的主机中获取到其他类型的一些变量信息,比如其他主机的注册变量、主机变量、组变量等信息,还有就是注册变量并不用像facts信息那样需要事先收集 ,即可直接通过hostvars跨主机被引用到; 实际案例2:hostvars 与 gather_fcats 与 set_fact模块案例: # cat > gatherfcats1.yml <<end : {{hostvars.local.var1}} , Play 1 register shell command: {{hostvars.local.shellrst['cmd']}}" #但是可以采用 hostvars来进行跨主机引用set_fact模块设置的变量 #msg: "play 1 中的 {hostvars['local'].ansible_default_ipv4 不能被调用由于根本没有进行
facts信息[yes|no] 「实际案例1」:hostvars 与 play_hosts内置变量联合使用 # cat > gatherfcats.yml <<end --- - name: "Play1 其实除了facts信息,我们还能够利用hostvars内置变量从别的主机中获取到其他类型的一些变量信息,比如其他主机的注册变量、主机变量、组变量等信息,还有就是注册变量并不用像facts信息那样需要事先收集 ,即可直接通过hostvars跨主机被引用到; 「实际案例2」:hostvars 与 gather_fcats 与 set_fact模块案例: # cat > gatherfcats1.yml <<end : {{hostvars.local.var1}} , Play 1 register shell command: {{hostvars.local.shellrst['cmd']}}" #但是可以采用 hostvars来进行跨主机引用set_fact模块设置的变量 #msg: "play 1 中的 {hostvars['local'].ansible_default_ipv4 不能被调用由于根本没有进行
ansible操作的远程服务器ip的地址 - hosts: "{{ server_group }}" - debug: var=inventory_hostname - debug: var=hostvars
10.0.0.3 mysql_user: fred # sensitive data mysql_password: supersecretpassword 我们可以使用Ansible的debug模块和hostvars 变量来测试所有变量是否可用于我们的主机: $ ansible -m debug -a 'var=hostvars[inventory_hostname]' database localhost | SUCCESS => { "hostvars[inventory_hostname]": { "ansible_check_mode": false, "ansible_version $ ansible -m debug -a 'var=hostvars[inventory_hostname]' database localhost | SUCCESS => { "hostvars
set([i[1].decode() for i in myresult])) data["all"] = {"children": groups} data["_meta"] = {"hostvars
1.生成hosts文件信息 {% for host in groups['all'] %} {{ hostvars[host]['ansible_default_ipv4']['address'] }} {{ hostvars[host]['ansible_fqdn'] }} {{ hostvars[host]['ansible_hostname'] }} {% endfor %} 2.带条件的循环
localhost localhost.localdomain localhost6 localhost6.localdomain6 {% for host in groups.all %} {{ hostvars [host].ansible_enp1s0.ipv4.address }} {{ hostvars[host].ansible_fqdn }} {{ hostvars[host].ansible_hostname }} {% endfor %} #for循环把所有主机名赋值给host #第一个获取对应主机的ip地址 #第二个主机域名信息 #第三个主机名 hostvars是所有主机的信息,hostvars[host ].ansible_hostname 实际上经过循环赋值后,会变成hostvars[servera].ansible_enp1s0.ipv4address 所有含义就是获取所有主机信息提取servera
kapiserver-access.log main; upstream kapiserver { {% for host in groups['nginx'] %} server {{ hostvars ------------ backend app balance roundrobin {% for host in groups['nginx'] %} server {{ hostvars [host]['ansible_facts']['hostname'] }} {{ hostvars[host]['ansible_facts']['default_ipv4']['address']
{ “ _meta”:{ “ hostvars”:{} } } 如果您有一些实例正在运行,则将获得包含所有实例详细信息的输出。
memcached) => { "changed": false, "item": "memcached", "msg": "AnsibleUndefinedVariable: {{ hostvars instance_name].analytics is defined' failed. } 2018-04-21 15:27:24,288 p=23225 u=root | Read vars_file '{{ hostvars install_contrail/tasks/create_redis.yml:2 2018-04-21 15:27:24,379 p=23225 u=root | Read vars_file '{{ hostvars create /etc/contrail/redis\n ^ here\n" } 2018-04-21 15:27:24,491 p=23225 u=root | Read vars_file '{{ hostvars
其模板中使用的get_check_data过滤器是从hostvars中获取每台主机的脚本执行结果,进行分析整理传递给模板,使用传递回来的数据进行渲染。
=> {"changed": false, "msg": "AnsibleUndefinedVariable: \"hostvars['zk1']\" is undefined"} fatal: [tdsql_zk2 => {"changed": false, "msg": "AnsibleUndefinedVariable: \"hostvars['zk1']\" is undefined"} fatal: [tdsql_zk3 => {"changed": false, "msg": "AnsibleUndefinedVariable: \"hostvars['zk1']\" is undefined"} to