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

月球

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

我尝试过这样。

 - 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章