Linux Interim Architecture Ansible

Source: Internet
Author: User
Tags aliyun

Ansible automation software based on Python development

Features Overview:

Configuration files do not require too much configuration to understand it.

# # #部署ansble软件

# #受控主机部署 Backup Nfs01 web01

Yum Install-y Libselinux-python

# #管理主机部署m01

Yum-y Install Ansible

At this point ansible software deployment is complete

# # #软件配置应用

Architecture:

To implement public key batch management before installation the default is already deployed

To view the software version:

[Email protected] ~]# ansible--version

Ansible 2.5.2

Config file =/etc/ansible/ansible.cfg

Configured module Search Path = [u '/root/.ansible/plugins/modules ', U '/usr/share/ansible/plugins/modules ']

Ansible Python Module location =/usr/lib/python2.6/site-packages/ansible

Executable location =/usr/bin/ansible

Python version = 2.6.6 (r266:84292, 15:13:37) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]

ANSBLE Directory structure Information:

/usr/bin/ansible

/usr/bin/ansible-playbook # # #ansible Script Information

[Email protected] ~]# cd/etc/ansible/

[email protected] ansible]# LL

Total 28

-rw-r--r--1 root root 19315 Apr 04:22 ansible.cfg # # #配置文件所在位置

-rw-r--r--1 root root 1048 10:20 hosts # # #定义ansible可以管理的主机信息

Drwxr-xr-x 2 root root 4096 Apr 04:22 Roles # # #主要在自动化部署多台主机时应用

About the Hosts file description: A managed host needs to be added in

# # #ansible命令语法格式

[Email protected] scripts]# ansible oldboy-m command-a "Date"

172.16.1.8 | SUCCESS | Rc=0 >>

Mon June 4 16:40:51 CST 2018

172.16.1.41 | SUCCESS | Rc=0 >>

Mon June 4 16:40:52 CST 2018

172.16.1.31 | SUCCESS | Rc=0 >>

Mon June 4 16:40:52 CST 2018

Ansible Official description

Description information:

Ansible Software related reference link information

Http://docs.ansible.com/ansible/intro_installation.html

http://www.ansible.com.cn/

Http://docs.ansible.com/modules_by_category.html

http://www.ansible.cn/docs/

Ansible Common parameters

The #1说明 command module is the default module when it is not specified.

[[email protected] scripts]# ansible oldboy-a "Date"

172.16.1.31 | SUCCESS | Rc=0 >>

Mon June 4 16:59:02 CST 2018

172.16.1.8 | SUCCESS | Rc=0 >>

Mon June 4 16:59:02 CST 2018

172.16.1.41 | SUCCESS | Rc=0 >>

Mon June 4 16:59:02 CST 2018

#2说明 testing connectivity modules for all hosts

[[email protected] scripts]# ansible oldboy-m Ping

172.16.1.41 | SUCCESS = {

"Changed": false,

"Ping": "Pong" # # #表示成功连接

}

172.16.1.31 | SUCCESS = {

"Changed": false,

"Ping": "Pong"

}

172.16.1.8 | SUCCESS = {

"Changed": false,

"Ping": "Pong"

}

#3 Debug

[[email protected] scripts]# ansible oldboy-m Debug

172.16.1.31 | SUCCESS = {

"MSG": "Hello world!"

}

172.16.1.41 | SUCCESS = {

"MSG": "Hello world!"

}

172.16.1.8 | SUCCESS = {

"MSG": "Hello world!"

}

[Email protected] scripts]# ansible oldboy-m debug-a "Msg=nod"

172.16.1.31 | SUCCESS = {

"MSG": "Nod"

}

172.16.1.41 | SUCCESS = {

"MSG": "Nod"

}

172.16.1.8 | SUCCESS = {

"MSG": "Nod"

}

#4 Copy Module--copy

[Email protected] scripts]# ansible oldboy-m copy-a "Src=/home/history/0604.txt dest=/home/history/"

To view the side effects:

File Transfer succeeded

Supplemental Case: Assume that we need to roll back or operate in error while doing a copy operation

; It is important to have a backup parameter at this time (to back up existing source files before distributing the files)

[Email protected] scripts]# ansible oldboy-m copy-a "Src=/home/history/0604.txt dest=/home/history/backup=yes"

#设置文件的属组信息 Permission Information

[Email protected] scripts]# ansible oldboy-m copy-a "Src=/home/history/0604.txt dest=/home/history/mode=0600 Owner=ol Dboy Group=oldboy Backup=yes "

# # #ansible命令输出信息中:

Green indicates a query or no change has occurred

Red indicates an exception occurred while executing the command operation

Yellow indicates that the managed host has been affected by the execution of the command and a configuration change has occurred

# # #ansible基础模块实践

#批量执行脚本 Shell (Universal module)

#第一个里程碑: Write scripts in m01 first

#第二个里程碑: Distribute scripts to hosts on Oldboy groups

[Email protected] scripts]# ansible oldboy-m copy-a "src=/server/scripts/yum.sh dest=/server/scripts"

#第三个里程碑: Execute shell scripts remotely

[Email protected] scripts]# ansible oldboy-m shell-a "/bin/bash/server/scripts/yum.sh"

Note: For Shell can recognize special symbol Comparison Command command not the operation of the shell can be implemented

[Email protected] scripts]# ansible oldboy-m shell-a "Cat/etc/hosts>>/home/history/0604v2.txt"

# #删除指定文件---File

# # #运行脚本模块 Script (no need to distribute script the second method) remotely execute commands in local scripts

[Email protected] scripts]# ansible oldboy-m script-a "/server/scripts/yum.sh"

There is no script on the:& but the command is also executed successfully.

# # #yum模块

# # #ansible模块-->file

[Email protected] scripts]# ansible oldboy-m file-a "src=/server/scripts/yum.sh dest=/server/scripts/yum.sh.link State=link "

Ansible Help information system to view the method:

Ansible-doc-l | Wc-l

Ansible-doc-s file

# # #ansible Timer Task Module Practice CRON

How to write a timed task before

/bin/sh/server/scripts/test.sh &>/dev/null

Minute

Hour

Day

Month

Weekday

Job

[Email protected] scripts]# crontab-l

*/5 * * * */usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1 # # #每隔五分钟做一次时间同步

Ansible Writing Method:

[Email protected] scripts]# ansible oldboy-m cron-a "Minute=*/5 job= '/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2& Gt;&1 ' "

is in effect on the end:

TIP: Set the Name property to avoid repeated additions

Repeat the error when adding:

Delete a timed task absent (absent) Use caution

[Email protected] scripts]# ansible oldboy-m cron-a "Name=luna minute=*/5 job= '/usr/sbin/ntpdate ntp1.aliyun.com >/d Ev/null 2>&1 ' state=absent '

Description Delete Scheduled Task

How to annotate a scheduled task:

Scheduled tasks that are not in the comment state can be commented

How to Uncomment:

Ansible Script writing style

Writing Ansible-playbook

01 When writing the need to identify the empty characters (strictly judge the space)

21 set the SPACEBAR to not use the tab

03 minus and Colon are used in the script (not at the end), followed by a space (for example:-Oldboy) cannot have spaces for comment information

04 Script Writing hierarchy divided by 2 spaces per level

# # #剧本编写实践

Ansible-playbook Writing formats

#注释信息

# # #剧本的开头, can not write

    • Hosts: space All (oldboy&172.16.1.41)

# # #处理所有服务器, find all servers

CRON.YML Script Content Storage location:

/etc/ansible/ansible-playbook

Create a Directory

/etc/ansible/ansible-playbook

Other grammar check commands that you have previously learned:

[Email protected] ansible-playbook]# visudo-c

/etc/sudoers:parsed OK

#检查ansible剧本的语法

If the syntax is wrong, it will be the prompt

# # #执行1个剧本

# #执行前先彩排: Local emulation run does not affect host

# #彩排结束后, really execute the script

Check Confirmation:

# # #剧本编写扩展

Ansible-playbook Cron.yml-v # # #显示ansible命令详细过程

- hosts:oldboy  tasks:  -cron:name=nod03 minute= hour= job= '/bin /sh/server/scripts/test.sh &>/dev/null' state=absent
cron.yml

#add cron- hosts:all  tasks:    - name:cron:nod03      cron:name=nod03 minute =xx hour= job='/bin/sh/server/scripts/test.sh &>/dev/null '
cron2.yml

Linux Interim Architecture Ansible

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.