Ansible 在运行带有--limit标志的playbook时,是否可以获取清单文件中所有主机的IP?
我正在尝试将iptable规则应用于一组主机,其中我希望为特定组中的主机打开所有端口。 但我无法在生产环境中为所有主机一起运行此配方。所以我使用--limit标志来运行剧本。 但由于给出以下错误,它失败了:Ansible 在运行带有--limit标志的playbook时,是否可以获取清单文件中所有主机的IP?,ansible,iptables,ansible-inventory,Ansible,Iptables,Ansible Inventory,我正在尝试将iptable规则应用于一组主机,其中我希望为特定组中的主机打开所有端口。 但我无法在生产环境中为所有主机一起运行此配方。所以我使用--limit标志来运行剧本。 但由于给出以下错误,它失败了: fatal: [test-vm1]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute
fatal: [test-vm1]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'ansible.vars.hostvars.HostVarsVars object' has no attribute 'ansible_default_ipv4'"}
库存文件
[all]
test-vm1 ansible_ssh_host=10.x.x.x
test-vm2 ansible_ssh_host=10.x.x.x
test-vm3 ansible_ssh_host=10.x.x.x
我的剧本。yml
- hosts: all
become: yes
gather_facts: yes
roles:
- iptables
- name: Prepare iptables rules
template: dest='/etc/sysconfig/iptable-config' src=rules-list.j2 owner=root group=root mode=0744
notify: save iptables rules
规则列表。j2
#Allow communication within hosts in a group
{% for host in groups['all'] %}
iptables -A INPUT -s {{ hostvars[host]['ansible_default_ipv4']['address'] }} -j ACCEPT
{% endfor %}
角色/iptables/tasks/main.yml
- hosts: all
become: yes
gather_facts: yes
roles:
- iptables
- name: Prepare iptables rules
template: dest='/etc/sysconfig/iptable-config' src=rules-list.j2 owner=root group=root mode=0744
notify: save iptables rules
我尝试运行的命令是-
ansible-playbook -i inventory-file my-playbook.yml --limit test-vm1
如果我在没有限制标志的情况下运行上述命令,它将正常运行而不会失败,即
ansible-playbook -i inventory-file my-playbook.yml
我认为您的模板循环使用
组[所有]
主机,这包括所有主机,而不仅仅是有限的主机。
而且,由于您仅限于某些主机,ansible无法为其余主机收集事实,并且您的模板无法为其余主机找到ipv4
您是否尝试过ansible\u play\u batch
或ansible\u play\u hosts
?
我想使用--limit标志运行特定主机集的规则。在我的清单中,我有意将主机放在组[all]下,因为我想在所有主机上循环以收集它们的ip/DNS名称,从而允许所有这些ip的流量。我能够使用hostvars[host]['ansible\u ssh\u host']比hostvars[host]['ansible\u default\u ipv4']['address']更有效地实现这一点。我认为要获得ipv4地址,ansible必须通过ssh连接到该特定主机,我将该主机限制为仅1个主机,但ansible_ssh_host值在扫描资源清册文件时保存在host_vars中。