(nsd1903) [[email protected] day03]# pip install zzg_pypkgs/ansible_pkg/*
(nsd1903) [[email protected] day03]# mkdir myansible
(nsd1903) [[email protected] day03]# cd myansible
(nsd1903) [[email protected] myansible]# vim ansible.cfg
[defaults]
inventory = hosts
remote_user = root
(nsd1903) [[email protected] myansible]# vim hosts
[dbservers]
node5.tedu.cn
[webservers]
node6.tedu.cn
node7.tedu.cn
# 名稱解析
[[email protected] nsd2019]# for i in {1..254}
> do
> echo -e "192.168.4.$i\tnode$i.tedu.cn\tnode$i" >> /etc/hosts
> done
# 收集主集密鑰,在首次ssh遠程主機時,就不需要回答yes了
[[email protected] nsd2019]# ssh-keyscan 192.168.4.{5..7} node{5..7} node{5..7}.tedu.cn >> ~/.ssh/known_hosts
# 免密登陸
[[email protected] nsd2019]# for i in {5..7}
> do
> ssh-copy-id node$i
> done
(nsd1903) [[email protected] myansible]# ansible all -m ping
(nsd1903) [[email protected] myansible]# ansible-doc -l | grep copy
(nsd1903) [[email protected] myansible]# ansible-doc copy
(nsd1903) [[email protected] myansible]# ansible all -m copy -a "src=/etc/hosts dest=/etc/hosts"
# 修改vim編輯器,可以支持yaml輸入格式
# vim ~/.vimrc
autocmd FileType yaml setlocal sw=2 ts=2 et ai
# 上傳yum倉庫文件
# 1. 在ansible管理端創建yum文件
(nsd1903) [[email protected] myansible]# mkdir files
(nsd1903) [[email protected] myansible]# vim files/server.repo
[server]
name=server
baseurl=ftp://192.168.4.254/centos7.4
gpgcheck=0
# 2. 編寫playbook
(nsd1903) [[email protected] myansible]# vim yumrepo.yml
---
- name: configure yum
hosts: all
tasks:
- name: upload yum repo file
copy:
src: files/server.repo
dest: /etc/yum.repos.d/
# 3. 檢查語法
[[email protected] myansible]# ansible-playbook --syntax-check yumrepo.yml
# 4. 執行playbook
[[email protected] myansible]# ansible-playbook yumrepo.yml
練習:playbook
[[email protected] myansible]# vim lamp.yml
---
- name: configure webservers
hosts: webservers
tasks:
- name: install web pkgs
yum:
name: [httpd, php, php-mysql]
state: present
- name: enable web service
service:
name: httpd
state: started
enabled: yes
- name: configure dbservers
hosts: dbservers
tasks:
- name: install db pkgs
yum:
name: mariadb-server
state: latest
- name: enable db service
service:
name: mariadb
state: started
ansible官方文檔: https://docs.ansible.com/ansible/2.7/index.html -> 搜索 python api。將example中的代碼復制,執行。
>>> from collections import namedtuple
# 創建名為Point的命名元組,它接受3個參數
>>> Point = namedtuple('Point', ['x', 'y', 'z'])
>>> p1 = Point(10, 15, 28)
>>> p1[0]
10
>>> p1[1:]
(15, 28)
>>> p1.x
10
>>> p1.y
15
>>> p1.z
28
將yaml文件手工轉成python數據類型
[
{
'name': 'configure webservers',
'hosts': 'webservers',
'tasks': [
{
'name': 'install web pkgs',
'yum': {
'name': ['httpd', 'php', 'php-mysql'],
'state': 'present'
}
},
{
'name': 'enable web service',
'service': {
'name': 'httpd',
'state': 'started',
'enabled': 'yes'
}
}
]
},
{
'name': 'configure dbservers',
'hosts': 'dbservers',
'tasks': [
{},
{}
]
}
]
(nsd1903) [[email protected] day03]# mkdir /tmp/mylibs
(nsd1903) [[email protected] day03]# export ANSIBLE_LIBRARY=/tmp/mylibs
編寫模塊,用於在遠程主機上實現拷貝操作
# rcopy.py
"用於在遠程主機上進行拷貝操作"
from ansible.module_utils.basic import AnsibleModule
import shutil
def main():
module = AnsibleModule(
argument_spec=dict(
yuan=dict(required=True, type='str'),
mubiao=dict(required=True, type='str')
)
)
shutil.copy(module.params['yuan'], module.params['mubiao'])
module.exit_json(changed=True)
if __name__ == '__main__':
main()
# 調用模塊,執行拷貝
(nsd1903) [[email protected] myansible]# ansible dbservers -m rcopy -a "yuan=/etc/passwd mubiao=/tmp/mima"
# 注意,ansible在執行命令時,將會把模塊進行配置拷貝到遠程主機上執行。遠程主機如果沒有python3,則不支持中文
# ansible all -m download -a "url=http://xxxx dest=/path/to/file"
# /tmp/mylibs/download.py
from ansible.module_utils.basic import AnsibleModule
import wget
def main():
module = AnsibleModule(
argument_spec=dict(
url=dict(required=True, type='str'),
dest=dict(required=True, type='str')
)
)
wget.download(module.params['url'], module.params['dest'])
module.exit_json(changed=True)
if __name__ == '__main__':
main()
# 在本地先將wget下載
[[email protected] tmp]# pip download wget --trusted-host pypi.douban.com
# 拷貝下載的文件到遠程主機
[[email protected] tmp]# scp wget-3.2.zip node5:/tmp
# 在遠程主機安裝wget
[[email protected] tmp]# ssh node5
[[email protected] ~]# cd /tmp/
[[email protected] tmp]# unzip wget-3.2.zip
[[email protected] tmp]# cd wget-3.2/
[[email protected] wget-3.2]# python setup.py install # python包都可以如此安裝
(nsd1903) [[email protected] myansible]# ansible dbservers -m download -a "url=http://192.168.4.254/server.repo dest=/tmp/"
該插件可以將收集下來的主機信息顯示為web頁面。
# 收集主機信息,存到/tmp/out目錄
(nsd1903) [[email protected] myansible]# ansible all -m setup --tree /tmp/out/
# 安裝ansible-cmdb
(nsd1903) [[email protected] myansible]# pip install ansible-cmdb_pkgs/*
# 生成web頁面
(nsd1903) [[email protected] myansible]# ansible-cmdb /tmp/out/ > /tmp/hosts.html
# 查看結果
(nsd1903) [[email protected] myansible]# firefox /tmp/hosts.html &