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]$