将主机名变量传递给角色失败

问题描述 投票:1回答:1

我正在尝试将两个包含主机名的变量传递给角色。这些主机名将以用户身份作为主机:值。

我尝试过这样。

 - 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场比赛。

ansible yaml ansible-2.x
1个回答
0
投票

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

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

- hosts: host2
  tasks:
        - name: Section 2
          include_role:
            name: role2
© www.soinside.com 2019 - 2024. All rights reserved.