简体   繁体   English

可变的Ansible剧本

[英]Ansible playbook with variable

Trying to run script with different arguments on different servers using ansible, example: 尝试使用ansible在不同的服务器上运行带有不同参数的脚本,例如:

server 192.168.0.1 -> fabric.sh generic1 status
server 192.168.0.2 -> fabric.sh generic2dr status
server 192.168.0.3 -> fabric.sh generic3 status

How to use variables in playbook below? 如何在下面的剧本中使用变量?

It works when I create group for each server, but it's not efficient 当我为每个服务器创建组时,它可以工作,但是效率不高

---
- hosts: esb
  remote_user: root
  tasks:
  - name: Generic_1
    become_user: esb
    shell: "/home/fabric.sh generic1 status"

Host file: 主机文件:

[esb]
192.168.0.1
192.168.0.2
192.168.0.3

You can set per-host variables in your inventory. 您可以在清单中设置每个主机的变量。 For example, modify your inventory so it looks like this: 例如,修改库存,使其如下所示:

[esb]
192.168.0.1 fabric_args="generic1 status"
192.168.0.2 fabric_args="generic2dr status"
192.168.0.3 fabric_args="generic3 status"

And then use the fabric_args variable in your playbook: 然后在您的剧本中使用fabric_args变量:

---
- hosts: esb
  remote_user: root
  tasks:
  - name: Generic_1
    become_user: esb
    shell: "/home/fabric.sh {{ fabric_args }}"

For more information, read the Using Variables and Working with Inventory sections of the Ansible documentation. 有关更多信息,请阅读Ansible文档的“ 使用变量使用库存”部分。

Next to Basic inventory there is yaml – Uses a specific YAML file as an inventory source . 基本广告资源旁边有yaml –使用特定的YAML文件作为广告资源

For example (fit the variables to your needs): 例如(使变量适合您的需求):

$ cat hosts
all:                                                                         
  hosts:                                                                     
    10.1.0.51:                                                               
    10.1.0.52:                                                               
    10.1.0.53:                                                               
  vars:                                                                      
    ansible_connection: ssh                                                  
    ansible_user: admin                                                      
    ansible_become: yes                                                      
    ansible_become_user: root                                                
    ansible_become_method: sudo                                              
    ansible_python_interpreter: /usr/local/bin/python3.6                     
    ansible_perl_interpreter: /usr/local/bin/perl                            
  children:                                                                  
    esb:                                                                     
      hosts:                                                                 
        10.1.0.51:                                                           
          run_string: "fabric.sh generic1 status"                            
        10.1.0.52:                                                           
          run_string: "fabric.sh generic2dr status"                          
        10.1.0.53:                                                           
          run_string: "fabric.sh generic3 status"

The play below 下面的戏

- hosts: esb
  tasks:
  - debug:
      var: run_string

gives (abridged): 给出(删节):

ok: [10.1.0.51] => {
    "run_string": "generic1 status"
}
ok: [10.1.0.52] => {
    "run_string": "generic2dr status"
}
ok: [10.1.0.53] => {
    "run_string": "generic3 status"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM