Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ansible:如何在一个地方更换主机_Ansible - Fatal编程技术网

Ansible:如何在一个地方更换主机

Ansible:如何在一个地方更换主机,ansible,Ansible,我有一本剧本,里面主人:一切都是从一开始就安排好的。这样,所有相应的角色都将假定hosts意味着all 但是在其中一个角色的一个块中,我想在本地运行一个命令,所以我将它的主机设置为127.0.0.1,playbook在这一行失败。我想这是因为安西伯认为主持人应该是所有人。在这种情况下,如何在本地运行该命令 这是我的密码: --- - name: install many things hosts: all remote_user: "{{ me }}" become: tru

我有一本剧本,里面主人:一切都是从一开始就安排好的。这样,所有相应的角色都将假定hosts意味着all

但是在其中一个角色的一个块中,我想在本地运行一个命令,所以我将它的主机设置为127.0.0.1,playbook在这一行失败。我想这是因为安西伯认为主持人应该是所有人。在这种情况下,如何在本地运行该命令

这是我的密码:

---
- name: install many things
hosts: all
remote_user: "{{ me }}"
become: true
gather_facts: false

roles:
- service1
- service2
角色/service1/tasks/main.yaml:

---
- name: Download file
  hosts: 127.0.0.1
  connection: local
  gather_facts: no
  command: gsutil cp gs://mybucket/filename.txt /tmp

- name: sudo apt update -y
  apt:
  ...
以下是错误:

ERROR! conflicting action statements: command, hosts

The error appears to be in '/.../roles/service1/tasks/main.yaml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- name: Download file
  ^ here

无法为任务设置主机。该指令仅对戏剧有效。您可能正在寻找指令,该指令允许您在主机上运行任务,而不是当前播放的目标主机。例如,您的任务可能是这样编写的:

- name: Download file
  delegate_to: localhost
  command: gsutil cp gs://mybucket/filename.txt /tmp
---
all:
  hosts:
    host1:
    host2:
---
- hosts: all
  gather_facts: false

  tasks:
    - command: echo "{{ inventory_hostname }}"
      register: hostname
      delegate_to: localhost

    - debug:
        var: hostname.stdout

PLAY [all] **********************************************************************************************************************************************************************************************************************************

TASK [command] ******************************************************************************************************************************************************************************************************************************
changed: [host1]
changed: [host2]

TASK [debug] ********************************************************************************************************************************************************************************************************************************
ok: [host1] => {
    "hostname.stdout": "host1"
}
ok: [host2] => {
    "hostname.stdout": "host2"
}

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
host1                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host2                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

请注意,使用
delegate_to
时,任务将针对当前播放中的每个主机运行一次,事实将针对播放中的目标主机,而不是针对正在运行任务的主机。也就是说,如果您有这样的库存:

- name: Download file
  delegate_to: localhost
  command: gsutil cp gs://mybucket/filename.txt /tmp
---
all:
  hosts:
    host1:
    host2:
---
- hosts: all
  gather_facts: false

  tasks:
    - command: echo "{{ inventory_hostname }}"
      register: hostname
      delegate_to: localhost

    - debug:
        var: hostname.stdout

PLAY [all] **********************************************************************************************************************************************************************************************************************************

TASK [command] ******************************************************************************************************************************************************************************************************************************
changed: [host1]
changed: [host2]

TASK [debug] ********************************************************************************************************************************************************************************************************************************
ok: [host1] => {
    "hostname.stdout": "host1"
}
ok: [host2] => {
    "hostname.stdout": "host2"
}

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
host1                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host2                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

还有这样的剧本:

- name: Download file
  delegate_to: localhost
  command: gsutil cp gs://mybucket/filename.txt /tmp
---
all:
  hosts:
    host1:
    host2:
---
- hosts: all
  gather_facts: false

  tasks:
    - command: echo "{{ inventory_hostname }}"
      register: hostname
      delegate_to: localhost

    - debug:
        var: hostname.stdout

PLAY [all] **********************************************************************************************************************************************************************************************************************************

TASK [command] ******************************************************************************************************************************************************************************************************************************
changed: [host1]
changed: [host2]

TASK [debug] ********************************************************************************************************************************************************************************************************************************
ok: [host1] => {
    "hostname.stdout": "host1"
}
ok: [host2] => {
    "hostname.stdout": "host2"
}

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
host1                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host2                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

然后,
命令
任务将在本地主机上运行,但输出如下所示:

- name: Download file
  delegate_to: localhost
  command: gsutil cp gs://mybucket/filename.txt /tmp
---
all:
  hosts:
    host1:
    host2:
---
- hosts: all
  gather_facts: false

  tasks:
    - command: echo "{{ inventory_hostname }}"
      register: hostname
      delegate_to: localhost

    - debug:
        var: hostname.stdout

PLAY [all] **********************************************************************************************************************************************************************************************************************************

TASK [command] ******************************************************************************************************************************************************************************************************************************
changed: [host1]
changed: [host2]

TASK [debug] ********************************************************************************************************************************************************************************************************************************
ok: [host1] => {
    "hostname.stdout": "host1"
}
ok: [host2] => {
    "hostname.stdout": "host2"
}

PLAY RECAP **********************************************************************************************************************************************************************************************************************************
host1                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host2                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

如果您只希望任务运行一次,而不考虑游戏中主机的数量,请考虑<代码> RunyOng/<代码>选项:

- name: Download file
  delegate_to: localhost
  run_once: true
  command: gsutil cp gs://mybucket/filename.txt /tmp

不能在任务上设置主机。该指令仅对戏剧有效。您可能正在寻找指令。谢谢larsks,我使用了delegate_,它像符咒一样工作。如果你能发布答案,我将投票并接受。