使用 Ansible 部署 Docker Swarm。将 hostvar 作为变量传递

使用 Ansible 部署 Docker Swarm。将 hostvar 作为变量传递

我在使用 Ansible Playbook 部署 Docker Swarm 时遇到了问题。

这是我的主机文件:

---
all:
  hosts:
    ansible01:
      connection: local
  children:
    docker_swarm_manager_init:
      hosts:
        manager01:
          ansible_host: 10.4.0.5

    docker_swarm_managers:
      hosts:
        manager02:
          ansible_host: 10.4.0.4
        manager03:
          ansible_host: 10.4.0.6
      children:
        docker_swarm_manager_init:

    docker_swarm_workers:
      hosts:
        worker01:
          ansible_host: 10.4.0.7
        worker02:
          ansible_host: 10.4.0.3

    docker:
      children:
        docker_swarm_managers:
        docker_swarm_workers:
      vars:

这是角色“docker_swarm”:

---
- name: Init the Docker Swarm
  docker_swarm:
    advertise_addr: "{{ docker_swarm_advertise_address }}"
    state: present
  register: docker_swarm_init
  notify: Set the join tokens as an Ansible fact
  when: docker_swarm_role == 'init'

- name: Join "{{ docker_swarm_role }}" nodes to the Docker Swarm
  docker_swarm:
    remote_addrs: [ "{{ docker_swarm_remote_address }}" ]
    advertise_addr: "{{ docker_swarm_advertise_address }}"
    join_token: "{{ docker_swarm_join_token }}"
    state: join
  when: docker_swarm_role != 'init'

这是我正在使用的剧本:

---
# Install Docker Engine (CE) on all Docker hosts
- hosts: docker
  roles:
    - docker_engine

# Init the Docker Swarm only on the first manager
- hosts: docker_swarm_manager_init
  roles:
    - role: docker_swarm
      vars:
        docker_swarm_role: init
        docker_swarm_advertise_address: "{{ ansible_all_ipv4_addresses | ipaddr('10.0.0.0/8') | first }}"

# Join all manager nodes to the Swarm
- hosts: docker_swarm_managers
  roles:
    - role: docker_swarm
      vars:
        docker_swarm_role: manager
        docker_swarm_join_token: "{{ hostvars['docker_swarm_manager_init[0]']['docker_swarm_init']['swarm_facts']['JoinTokens']['Manager'] }}"
        docker_swarm_remote_address: "{{ hostvars['docker_swarm_manager_init[0]']['ansible_all_ipv4_addresses'] | ipaddr('10.0.0.0/8') | first }}"
        docker_swarm_advertise_address: "{{ ansible_all_ipv4_addresses | ipaddr('10.0.0.0/8') | first }}"

# Join all worker nodes to the Swarm
- hosts: docker_swarm_workers
  roles:
    - role: docker_swarm
      vars:
        docker_swarm_role: worker
        docker_swarm_join_token: "{{ hostvars['docker_swarm_manager_init[0]']['docker_swarm_init']['swarm_facts']['JoinTokens']['Worker'] }}"
        docker_swarm_remote_address: "{{ hostvars['docker_swarm_manager_init[0]']['ansible_all_ipv4_addresses'] | ipaddr('10.0.0.0/8') | first }}"
        docker_swarm_advertise_address: "{{ ansible_all_ipv4_addresses | ipaddr('10.0.0.0/8') | first }}"

当我运行剧本时出现以下错误:

PLAY [docker] ***************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************************************************
ok: [manager02]
ok: [worker02]
ok: [manager03]
ok: [worker01]
ok: [manager01]

TASK [docker_engine : Add Docker GPG key] ***********************************************************************************************************************************************************************************************************************************
ok: [manager02]
ok: [worker02]
ok: [manager03]
ok: [manager01]
ok: [worker01]

TASK [docker_engine : Add Docker APT repository] ****************************************************************************************************************************************************************************************************************************
ok: [manager02]
ok: [worker02]
ok: [manager01]
ok: [manager03]
ok: [worker01]

TASK [docker_engine : Install Docker (CE) & requirements] *******************************************************************************************************************************************************************************************************************
ok: [worker02]
ok: [manager02]
ok: [manager01]
ok: [worker01]
ok: [manager03]

PLAY [docker_swarm_manager_init] ********************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************************************************
ok: [manager01]

TASK [docker_swarm : Init the Docker Swarm] *********************************************************************************************************************************************************************************************************************************
ok: [manager01]

TASK [docker_swarm : Join "init" nodes to the Docker Swarm] *****************************************************************************************************************************************************************************************************************
skipping: [manager01]

PLAY [docker_swarm_managers] ************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************************************************
ok: [manager02]
ok: [manager03]
ok: [manager01]

TASK [docker_swarm : Init the Docker Swarm] *********************************************************************************************************************************************************************************************************************************
skipping: [manager02]
skipping: [manager03]
skipping: [manager01]

TASK [docker_swarm : Join "manager" nodes to the Docker Swarm] **************************************************************************************************************************************************************************************************************
fatal: [manager02]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: \"hostvars['docker_swarm_manager_init[0]']\" is undefined\n\nThe error appears to be in '/root/ansible_structured/roles/docker_swarm/tasks/main.yml': line 10, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Join \"{{ docker_swarm_role }}\" nodes to the Docker Swarm\n  ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"}
fatal: [manager03]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: \"hostvars['docker_swarm_manager_init[0]']\" is undefined\n\nThe error appears to be in '/root/ansible_structured/roles/docker_swarm/tasks/main.yml': line 10, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Join \"{{ docker_swarm_role }}\" nodes to the Docker Swarm\n  ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"}
fatal: [manager01]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: \"hostvars['docker_swarm_manager_init[0]']\" is undefined\n\nThe error appears to be in '/root/ansible_structured/roles/docker_swarm/tasks/main.yml': line 10, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Join \"{{ docker_swarm_role }}\" nodes to the Docker Swarm\n  ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n    with_items:\n      - {{ foo }}\n\nShould be written as:\n\n    with_items:\n      - \"{{ foo }}\"\n"}

PLAY RECAP ******************************************************************************************************************************************************************************************************************************************************************
manager01                  : ok=7    changed=0    unreachable=0    failed=1    skipped=2    rescued=0    ignored=0   
manager02                  : ok=5    changed=0    unreachable=0    failed=1    skipped=1    rescued=0    ignored=0   
manager03                  : ok=5    changed=0    unreachable=0    failed=1    skipped=1    rescued=0    ignored=0   
worker01                   : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
worker02                   : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

我对这个引文进行了很多尝试,但似乎没有什么效果......

我希望让角色尽可能通用,以提高可重用性。这就是为什么我想将主机变量作为变量传递给角色。

谢谢!

答案1

尝试下面的替换

 - role: docker_swarm
      vars:
        docker_swarm_role: manager
        docker_swarm_manager_init_0: "{{ groups['docker_swarm_manager_init'] | first }}"
        docker_swarm_join_token: "{{ hostvars[docker_swarm_manager_init_0]...

(未经测试)

笔记

  • docker_swarm_manager_init库存中有两组

相关内容