无法将主机名变量传递给角色

Failing at passing a host name variable to a role

我正在尝试将两个包含主机名的变量传递给一个角色。这些主机名将是 user 作为 hosts: 值。

我这样试过

 - hosts: host1,host2
   roles:
     - role: role1
       oldhost: host1
       newhost: host2

像这样。

- hosts: host1,host2
  tasks:
        - name: Transfering tar files.
          include_role:
            name: role1
          vars:
            oldhost: host1
            newhost: host2

但无论我做什么,我都会收到以下错误:

ERROR! unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>

The error appears to be in '/etc/ansible/custom/1943Asco/app/roles/userdata/tasks/main.yml': line 2, 
column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---
- hosts: "{{oldhost | default('Invalid old host') }}"
  ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:

with_items:
  - {{ foo }}

Should be written as:

with_items:
  - "{{ foo }}"

这是我的main.yml。在这个之后我有第二个用于 host2。

---
- hosts: "{{ oldhost| default('Invalid host1') }}"
  vars:
    first: "$HOME/first.tar.gz"
    second: "$HOME/second.tar.gz"
    third: "$HOME/third.tar.gz"
    fourth: "$HOME/fourth.tar.gz"
  tasks:
        - name: Copy file from remote node onto local server.
          fetch:
            src: "{{item}}"
            dest: "/path/to/item/"
            flat: yes
            fail_on_missing: no
          with_items:
            - "{{first}}"
            - "{{second}}"
            - "{{third}}"
            - "{{fourth}}"


        - name: Delete the tar file
          local_action: file path="{{item}}" state=absent
          with_items:
            - "{{first}}"
            - "{{second}}"
            - "{{third}}"
            - "{{fourth}}"

谁能帮我解决这个问题?

编辑

我认为问题出在 main.yml 文件中我只能有任务。而且我一直在通过2场比赛。

我已通过将 main.yml 文件分成两个角色来解决此问题。现在我这样称呼它并且有效。

- hosts: host1
  tasks:
        - name: Section 1
          include_role:
            name: role1

- hosts: host2
  tasks:
        - name: Section 2
          include_role:
            name: role2