(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
# 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
(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"
# 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
[[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 Official documents : https://docs.ansible.com/ansible/2.7/index.html -> Search for python api. take example Code replication in , perform .
>>> 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
[
{
'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
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
# 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()
# 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
(nsd1903) [[email protected] myansible]# ansible dbservers -m download -a "url=http://192.168.4.254/server.repo dest=/tmp/"
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 &