如何在Ansible Playbook中为主机声明两个提示变量

时间:2020-09-13 06:25:45

标签: ansible ansible-2.x ansible-inventory

如何在ansible剧本中为主机声明两个提示变量,我在Playbook下方尝试过但没有运气。 ................................................... ................................................... ................................................... ...... 预先谢谢你。

---
- name: MD5 File Check 
  gather_facts: false
  hosts: "{{ cluster_host_1 }}", "{{cluster_host_2 }}" 
  hosts: localhost
  remote_user: sv_operator
  vars_prompt:
    - name: "file_1"
      prompt: "File name"
      private: no
    - name: "cluster_host_1"
      prompt: "Enter 1st Host name"
      private: no
    - name: "cluster_host_2"
      prompt: "Enter 2nd Host Name"
      private: no
  tasks:
    - stat:
        path: "/tmp/{{ file_1 }}"
        checksum_algorithm: sha256
      register: output
      delegate_to: "{{ cluster_host_1 }}" 
    - debug:
        msg: "{{ output.stat.checksum }}"
    - stat:
        path: "/tmp/{{ file_2 }}"
        checksum_algorithm: sha256
      register: output_
      delegate_to: "{{ cluster_host_2 }}"
    - debug:
        msg: "{{ output_.stat.checksum }}"

2 个答案:

答案 0 :(得分:2)

给出远程主机和/ tmp / file1

shell> ssh admin@test_01 sha256 /tmp/file1
SHA256 (/tmp/file1) = e2611a1fac7fc2ab99d2e792ad84f34e66740d6a3d77b97b4da39a3758357da0

shell> ssh admin@test_02 sha256 /tmp/file1
SHA256 (/tmp/file1) = 109f60103192b5c8f4e33c26b4f9c7b94489bf8de0325497b7f5a0668dc1a402

下面的剧本

shell> cat playbook.yml
- name: SHA256 File Check 
  hosts: localhost
  gather_facts: false
  vars_prompt:
    - name: "file_1"
      prompt: "File name"
      private: no
    - name: "cluster_host_1"
      prompt: "Enter 1st Host name"
      private: no
    - name: "cluster_host_2"
      prompt: "Enter 2nd Host Name"
      private: no
  tasks:
    - stat:
        path: "/tmp/{{ file_1 }}"
        checksum_algorithm: sha256
      register: output
      delegate_to: "{{ cluster_host_1 }}" 
    - debug:
        msg: "{{ output.stat.checksum }}"
    - stat:
        path: "/tmp/{{ file_1 }}"
        checksum_algorithm: sha256
      register: output_
      delegate_to: "{{ cluster_host_2 }}"
    - debug:
        msg: "{{ output_.stat.checksum }}"

按预期工作

shell> ansible-playbook playbook.yml
File name: file1
Enter 1st Host name: test_01
Enter 2nd Host Name: test_02

PLAY [SHA256 File Check] ****

TASK [stat] ****
ok: [localhost -> test_01]

TASK [debug] ****
ok: [localhost] => 
  msg: e2611a1fac7fc2ab99d2e792ad84f34e66740d6a3d77b97b4da39a3758357da0

TASK [stat] ****
ok: [localhost -> test_02]

TASK [debug] ****
ok: [localhost] => 
  msg: 109f60103192b5c8f4e33c26b4f9c7b94489bf8de0325497b7f5a0668dc1a402

PLAY RECAP ****
localhost: ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

答案 1 :(得分:1)

如果要在两个主机上运行游戏,请通过变量声明。您有两种选择。

选项1:

由于目标主机是逗号分隔的,因此您最好以这种方式输入它们。

- hosts: '{{ my_hosts }}'
  gather_facts: false
  remote_user: 'sv_operator'

  vars_prompt:
    - name: 'my_hosts'
      prompt: 'Comma separated list of hosts'
      private: no

然后在出现提示时:

Comma separated list of hosts: host1.local,host2.local

选项2:

使用host_1host_2之类的单个变量捕获每个主机,然后在播放中将其称为列表。

- hosts:
    - '{{ host_1 }}'
    - '{{ host_2 }}'
  gather_facts: false
  remote_user: 'sv_operator'

  vars_prompt:
    - name: 'host_1'
      prompt: 'First host'
      private: no
    - name: 'host_2'
      prompt: 'Second host'
      private: no

然后在出现提示时:

First host: host1.local
Second host: host2.local