程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python learning notes_ Devops_ Day03

編輯:Python

ansible application

install

(nsd1903) [[email protected] day03]# pip install zzg_pypkgs/ansible_pkg/*

Configure the basic application environment

(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
# Name resolution
[[email protected] nsd2019]# for i in {1..254}
> do
> echo -e "192.168.4.$i\tnode$i.tedu.cn\tnode$i" >> /etc/hosts
> done
# Collect the master set key , For the first time ssh When remote host , There is no need to answer yes 了
[[email protected] nsd2019]# ssh-keyscan 192.168.4.{5..7} node{5..7} node{5..7}.tedu.cn >> ~/.ssh/known_hosts
# Avoid secret landing
[[email protected] nsd2019]# for i in {5..7}
> do
> ssh-copy-id node$i
> done

Application of adhoc

  • adhoc It's a temporary order , When the command is simple and not used periodically , In this way
(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"

Application of playbook

# modify vim Editor , Can support yaml Input format
# vim ~/.vimrc
autocmd FileType yaml setlocal sw=2 ts=2 et ai
# Upload yum The warehouse files
# 1. stay ansible The management side creates yum file
(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. To write 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. Check grammar
[[email protected] myansible]# ansible-playbook --syntax-check yumrepo.yml
# 4. perform playbook
[[email protected] myansible]# ansible-playbook yumrepo.yml

practice :playbook

  • web Installation on server httpd / php / php-myql
  • web Server enable httpd service
  • db Server installation mariadb-server
  • db Server startup mariadb service
[[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 Programming

ansible Official documents : https://docs.ansible.com/ansible/2.7/index.html -> Search for python api. take example Code replication in , perform .

Named tuples

  • It is essentially a tuple
  • Just add a name to the index of the tuple
>>> from collections import namedtuple
# Create a Point Named tuple of , It accepts 3 Parameters
>>> 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

take yaml Files are manually converted to python data type

  • With "- " The first line , Convert to list item
  • With "key: val" Rows as structures , Turn to dictionary
[
{
'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': [
{},
{}
]
}
]

ansible Module development

  • Customized modules can be stored in a directory , Set up ANSIBLE_LIBRARY The environment variable points to it
(nsd1903) [[email protected] day03]# mkdir /tmp/mylibs
(nsd1903) [[email protected] day03]# export ANSIBLE_LIBRARY=/tmp/mylibs

Writing module , Used to implement copy operation on remote host

# rcopy.py
" Used for copy operations on remote hosts "
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()
# Call module , Execute Copy
(nsd1903) [[email protected] myansible]# ansible dbservers -m rcopy -a "yuan=/etc/passwd mubiao=/tmp/mima"
# Be careful ,ansible When executing a command , The module configuration will be copied to the remote host for execution . If the remote host does not have python3, Chinese is not supported

Module exercises :

  • Writing module download For downloading
  • There are two parameters
  • url: Define network sources
  • dest: Used to define the local directory
# ansible all -m download -a "url=http://xxxx dest=/path/to/file"
  1. Writing module
# /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()
  1. Install on the remote host wget modular
# In the local area, first wget download
[[email protected] tmp]# pip download wget --trusted-host pypi.douban.com
# Copy the downloaded file to the remote host
[[email protected] tmp]# scp wget-3.2.zip node5:/tmp
# Install on the remote host 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 Packages can be installed like this
  1. Perform download operation
(nsd1903) [[email protected] myansible]# ansible dbservers -m download -a "url=http://192.168.4.254/server.repo dest=/tmp/"

ansible-cmdb plug-in unit

The plug-in can display the collected host information as web page .

# Collect host information , Deposit in /tmp/out Catalog
(nsd1903) [[email protected] myansible]# ansible all -m setup --tree /tmp/out/
# install ansible-cmdb
(nsd1903) [[email protected] myansible]# pip install ansible-cmdb_pkgs/*
# Generate web page
(nsd1903) [[email protected] myansible]# ansible-cmdb /tmp/out/ > /tmp/hosts.html
# View results
(nsd1903) [[email protected] myansible]# firefox /tmp/hosts.html &

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved