Ansible主机组引用不起作用

86次阅读
没有评论

问题描述

在尝试在特定的主机组上运行Docker安装时遇到了问题。他已经花了几个小时了。以下是他的hosts.yml文件和site.yml文件的内容:

all:
  hosts:
    01-dev:
      ansible_host: <IP1>
    02-dev:
      ansible_host: <IP2>
    03-dev:
      ansible_host: <IP3>
  children:
    docker:
      hosts:
        01-dev
        02-dev
    dev:
      hosts:
        01-dev
        02-dev
        03-dev
- name: Configure hosts
  hosts: dev
  tasks:
    - name: Update and upgrade apt packages
      become: true
      apt:
        upgrade: yes
        update_cache: yes
        cache_valid_time: 86400 #One day
    - name: Install needed network manager libs
      become: yes
      ansible.builtin.package:
        name:
          - network-manager
        state: present
    - name: "Configuring eth"
      become: yes
      community.general.nmcli:
        conn_name: "System ens18"
        ifname: ens18
        type: ethernet
        state: present
        autoconnect: yes
        ip4: "{{ansible_host}}/24"
        gw4: "192.168.3.1"
  hosts: docker
  become: true
  tasks:
    - name: Install aptitude using apt
      apt: name=aptitude state=latest update_cache=yes force_apt_get=yes
    - name: Install required system packages
      apt: name={{ item }} state=latest update_cache=yes
      loop: [ 'apt-transport-https', 'ca-certificates', 'curl', 'software-properties-common', 'python3-pip', 'virtualenv', 'python3-setuptools']
    - name: Add Docker GPG apt Key
      apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present
    - name: Add Docker Repository
      apt_repository:
        repo: deb https://download.docker.com/linux/ubuntu bionic stable
        state: present
    - name: Update apt and install docker-ce
      apt: update_cache=yes name=docker-ce state=latest
    - name: Install Docker Module for Python
      pip:
        name: docker

用户运行命令ansible-playbook -i hosts.yml site.yml -vvv --ask-become-pass时,遇到了一个错误,提示docker组没有主机。
请问我的hosts文件有什么问题吗?

解决方案

请注意以下操作注意版本差异及修改前做好备份。
根据回答,你的hosts.yml文件中的docker组的语法有问题。正确的语法应该是:

children:
  docker:
    hosts:
      01-dev:
      02-dev:

docker组的hosts下,每个主机应该以冒号结尾。
以下是修正后的hosts.yml文件:

all:
  hosts:
    01-dev:
      ansible_host: <IP1>
    02-dev:
      ansible_host: <IP2>
    03-dev:
      ansible_host: <IP3>
  children:
    docker:
      hosts:
        01-dev:
        02-dev:
    dev:
      hosts:
        01-dev
        02-dev
        03-dev

修正后,再次运行ansible-playbook -i hosts.yml site.yml -vvv --ask-become-pass命令即可。

正文完