ansible­管理大项目

ansible­管理大项目

练习作业

1、使用动态清单模板,修改其内容,要求如下:
(1)node1是test主机组的成员,其中test主机组可以使用变量:
aa=11
bb=22
(2)node2和node3是prod主机组的成员,其中prod主机组可以使用的变量:
cc=33
dd=44
(3)node1还可以使用的变量:nodevar=chenyu1 //将chenyu换成自己名字的全拼
(4)node2还可以使用的变量:nodevar=chenyu2
(5)node3还可以使用的变量:nodevar=chenyu3
(6)撰写一个test.yml的playbook,要求所有的受控主机输出变量nodevar的值

准备

[root@ansible ~]# yum install -y python3

编写playbook

[student@ansible ansible]$ vim test1.yaml
[student@ansible ansible]$ cat test1.yaml
---
- hosts: all
  tasks:
    - name: debug test 1
      debug:
        msg: "just for test ansible dynamic inventory"

    - name: debug test 2
      debug:
        msg: "{{ nodevar }}"

# ansible-playbook test1.yaml -i inventory.py --list-hosts
# ansible-playbook test1.yaml -i inventory.py
[student@ansible ansible]$ 

编写脚本

[student@ansible ansible]$ vim inventory.py
[student@ansible ansible]$ cat inventory.py
#!/usr/bin/env python3

'''
Example custom dynamic inventory script for Ansible, in Python.
'''

import os
import sys
import argparse
import json

class ExampleInventory(object):

    def __init__(self):
        self.inventory = {}
        self.read_cli_args()

        # Called with `--list`.
        if self.args.list:
            self.inventory = self.example_inventory()
        # Called with `--host [hostname]`.
        elif self.args.host:
            # Not implemented, since we return _meta info `--list`.
            self.inventory = self.empty_inventory()
        # If no groups or vars are present, return empty inventory.
        else:
            self.inventory = self.empty_inventory()

        print(json.dumps(self.inventory));
    
    # Example inventory for testing.
    def example_inventory(self):
        return {
            'test': {
                'hosts': ['node1'],
                'vars': {
                    'aa': '11',
                    'bb': '22',
                    'nodevar': 'laj1'
                }
            },
            'prod': {
                'hosts': ['node2','node3'],
                'vars': {
                    'cc': '33',
                    'dd': '44',
                    }
            },
            '_meta': {
                'hostvars': {
                    'node2': {
                        'nodevar': 'laj2'
                    },
                    'node3': {
                        'nodevar': 'laj3'
                    }
                }
            }
        }
    
    # Empty inventory for testing.
    def empty_inventory(self):
        return {'_meta': {'hostvars': {}}}

    # Read the command line args passed to the script.
    def read_cli_args(self):
        parser = argparse.ArgumentParser()
        parser.add_argument('--list', action = 'store_true')
        parser.add_argument('--host', action = 'store')
        self.args = parser.parse_args()

# Get the inventory.
ExampleInventory()

给予脚本执行权限

[student@ansible ansible]$ chmod +x inventory.py

执行

[student@ansible ansible]$ ansible-playbook test1.yaml -i inventory.py

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

TASK [Gathering Facts] *********************************************************
ok: [node3]
ok: [node2]
ok: [node1]

TASK [debug test 1] ************************************************************
ok: [node1] => {
    "msg": "just for test ansible dynamic inventory"
}
ok: [node2] => {
    "msg": "just for test ansible dynamic inventory"
}
ok: [node3] => {
    "msg": "just for test ansible dynamic inventory"
}

TASK [debug test 2] ************************************************************
ok: [node2] => {
    "msg": "laj2"
}
ok: [node1] => {
    "msg": "laj1"
}
ok: [node3] => {
    "msg": "laj3"
}

PLAY RECAP *********************************************************************
node1                      : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node2                      : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node3                      : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[student@ansible ansible]$ 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值