docker extra_host parameter expects a dictionary v

2020-05-08 06:27发布

问题:

In ansible playbook docker parameter extra_host takes two parts host: ip_address. I am trying to pass the host and ipaddress in as variables. They are from prompt vars. The end result in my hosts file is: 1.2.3.4 {{server_hostname}}. Here is the code:

vars_prompt:
  - name: "server_ip"
    prompt: "Please enter the server IP address"
    private: no

  - name: "server_hostname"
    prompt: "Please enter the server hostname"
    private: no

tasks:    
  - name: Install Tomcat
    docker:
      image: tomcat:8.0
      pull: missing
      name: tomcat
      state: restarted
      ports: 
        - "8080:8080"
        - "443:443"
      extra_hosts:
        "{{server_hostname}}": "{{server_ip}}"

I am new to ansible playbook any help would be greatly appreciated.

回答1:

Make server_host_ip dict with set_fact before your task:

- set_fact:
    server_host_ip: "{'{{host_name}}':'{{host_ip}}'}"

And use {{server_host_ip}} in the docker module.