【Ansible】ansible调用主机报错问题

ansible预计实现功能,自动生成/etc/hosts文件,copy到远程机器,出现如下错误:

配置如下:

    ---
    - hosts: 192.168.31.12
      remote_user: cluster
      sudo: yes
      tasks:
        - name: dirstribute host file
          template: src=templates/hosts.j2 dest=/tmp/hosts

 hosts.j2

    127.0.0.1 localhost
    {{ ansible_eth0.ipv4.address }}  {{ ansible_hostname }}

    {% for host in groups['all'] %}
    {{ hostvars[host].ansible_eth0.ipv4.address }}  {{ hostvars[host].ansible_hostname }}
    {% endfor %}

    # The following lines are desirable for IPv6 capable hosts
    ::1 ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    ff02::3 ip6-allhosts
    cat /etc/ansible/hosts
    [all]
    192.168.31.10
    192.168.31.12
    ifconfig
    eno16777984: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.31.12  netmask 255.255.255.0  broadcast 192.168.31.255
            inet6 fe80::20c:29ff:fe5c:520e  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:5c:52:0e  txqueuelen 1000  (Ethernet)
            RX packets 3645802  bytes 780617081 (744.4 MiB)
            RX errors 32  dropped 8430  overruns 0  frame 0
            TX packets 985366  bytes 509863584 (486.2 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::1  prefixlen 128  scopeid 0x10<host>
            loop  txqueuelen 0  (Local Loopback)
            RX packets 18000  bytes 988080 (964.9 KiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 18000  bytes 988080 (964.9 KiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

解决后:

    ansible 192.168.31.12 -m setup -a 'filter=ansible_eno16777984'
    192.168.31.12 | success >> {
        "ansible_facts": {
            "ansible_eno16777984": {
                "active": true,
                "device": "eno16777984",
                "ipv4": {
                    "address": "192.168.31.12",
                    "netmask": "255.255.255.0",
                    "network": "192.168.31.0"
                },
                "ipv6": [
                    {
                        "address": "fe80::20c:29ff:fe5c:520e",
                        "prefix": "64",
                        "scope": "link"
                    }
                ],
                "macaddress": "00:0c:29:5c:52:0e",
                "module": "vmxnet3",
                "mtu": 1500,
                "promisc": false,
                "type": "ether"
            }
        },
        "changed": false
    }

 固更改hosts.j2文件:

    cat hosts.j2
    127.0.0.1 localhost
    {{ ansible_eno16777984.ipv4.address }}  {{ ansible_hostname }}

    {% for host in groups['all'] %}
    {{ hostvars[host].ansible_eno16777984.ipv4.address }}  {{ hostvars[host].ansible_hostname }}
    {% endfor %}

    # The following lines are desirable for IPv6 capable hosts
    ::1 ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    ff02::3 ip6-allhosts

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值