Linux ansible simple installation and bulk configuration detailed

Source: Internet
Author: User
Tags data structures file copy ssh

Ansible

Ansible does not require any agents, except sshd, does not occupy the control of any resources in the ansible does not execute (the default support SSH, also supports other)
Ansible also has no service side, only executes commands when needed
Ansible based on module work, execute commands, scripts, planning tasks, etc. need a module to achieve, Ansible has nearly hundreds of modules, modules can be developed by any programming language
Ansible supports the Yaml language task list to do multiple mainframe multitasking

Ansible is developed by Python

YAML syntax is similar to other grammars, and can simply express data structures such as lists, hashes, scalars, and so on. The structure (structure) is displayed by a space, and the items in the sequence (sequence) are represented by "-", and the key value pairs in the map are divided by ":". The following example:
-Hosts: Host name or group name, can be multiple
VARs
Http_prot:80
max_clients:256
Remote_user:root
TASHS: Mission
-Name: Task Name
YUM:NAME=HTTPD state=latest Installation httpd
-Name:
SERVICE:NAME=HTTPD state=started Make sure you can start after installation
Download ansible:

Https://pypi.python.org/pypi/ansible
Http://pkgs.org/download/ansible
Https://pypi.python.org/packages/source/a/ansible/ansible-2.0.1.0.tar.gz
First, compile and install Ansible
1, install the dependency pack:

Yum install python-jinja2 pyyaml python-paramiko python-babel python-crypto pip* gcc python-devel
Wget-p/usr/local/https://pypi.python.org/packages/source/a/ansible/ansible-2.0.1.0.tar.gz && cd/usr/ Local
Tar XF ansible-2.0.1.0.tar.gz
LN-SV ansible-2.0.1.0 ansible
CD ansible
Python setup.py Build
Python setup.py Install
Mkdir/etc/ansible
Cp-r examples/*/etc/ansible
[Root@yum-down bin]# ls/etc/ansible/
Ansible.cfg configuration file
Hosts Hosts file
Yum Remove python-jinja2 pyyaml python-paramiko python-babel python-crypto gcc python-devel
In the Hosts file, the definition host can write the host name or IP separately, or use [host group], or the wildcard character www. [1*].com

1, add Host:

[Db-server]
192.168.1.7
192.168.1.8
[Web-server]
192.168.1.4
2, add Ssh-key

[Root@yum-down ansible]# ssh-keygen-t rsa-p '
[Root@yum-down ansible]# ssh-copy-id-i/root/.ssh/id_rsa.pub root@192.168.1.4
[Root@yum-down ansible]# ssh-copy-id-i/root/.ssh/id_rsa.pub root@192.168.1.7
[Root@yum-down ansible]# ssh-copy-id-i/root/.ssh/id_rsa.pub root@192.168.1.8
3, view time

[Root@yum-down bin]#./ansible all-a ' Date '
192.168.1.7 | SUCCESS | Rc=0 >>
Sat APR 9 05:58:16 PDT 2016

192.168.1.8 | SUCCESS | Rc=0 >>
Sat APR 9 05:58:16 PDT 2016

192.168.1.4 | SUCCESS | Rc=0 >>
Sat APR 9 05:58:16 PDT 2016

[Root@yum-down bin]#./ansible all-m command-a ' Date '
192.168.1.4 | SUCCESS | Rc=0 >>
Sat APR 9 05:58:36 PDT 2016

192.168.1.7 | SUCCESS | Rc=0 >>
Sat APR 9 05:58:36 PDT 2016

192.168.1.8 | SUCCESS | Rc=0 >>
Sat APR 9 05:58:36 PDT 2016

[Root@yum-down bin]#/ansible all-m command-a ' service httpd status '
192.168.1.7 | FAILED | Rc=3 >>
HTTPD is stopped

192.168.1.8 | FAILED | Rc=3 >>
HTTPD is stopped

192.168.1.4 | FAILED | Rc=3 >>
HTTPD is stopped
4, list support for all modules
[Root@yum-down bin]#./ansible-doc-l
To view the parameters of a module help
[Root@yum-down bin]#./ansible-doc-s Copy

Second, yum installation yum-y install ansible can
1, File push copy
Push the root epel-release-6-8.noarch.rpm down to the OPT directory of the machine in the Db-server group

[Root@node ansible]# ansible db-server-m copy-a "src=/root/epel-release-6-8.noarch.rpm dest=/opt/"
192.168.1.8 | Success >> {
    "changed": True,
    "checksum": " 2b2767a5ae0de30b9c7b840f2e34f5dd9deaf19a ",
   " dest ":"/opt/epel-release-6-8.noarch.rpm ",
    "GID": 0,
    "group": "Root",
    "md5sum": " 2cd0ae668a585a14e07c2ea4f264d79b ",
   " mode ":" 0644 ",
   " owner ":" Root ",
    "Size": 14540,
    "src": "/root/.ansible/tmp/ Ansible-tmp-1460221879.64-117005813385704/source ",
   " state ":" File ",
   " UID ": 0
}

192.168.1.7 | Success >> {
"Changed": true,
"Checksum": "2b2767a5ae0de30b9c7b840f2e34f5dd9deaf19a",
"Dest": "/opt/epel-release-6-8.noarch.rpm",
"GID": 0,
"Group": "Root",
"Md5sum": "2cd0ae668a585a14e07c2ea4f264d79b",
"Mode": "0644",
"Owner": "Root",
"Size": 14540,
"src": "/root/.ansible/tmp/ansible-tmp-1460221879.64-59861356394345/source",
"State": "File",
"UID": 0
}

[Root@node ansible]#
2, verify

[Root@node ansible]# ansible db-server-a "Ls/opt"
192.168.1.8 | Success | Rc=0 >>
epel-release-6-8.noarch.rpm
Logstash
Rh

192.168.1.7 | Success | Rc=0 >>
epel-release-6-8.noarch.rpm
Rh

[Root@node ansible]#
Define cron Tasks

[Root@node ansible]# ansible all-m cron-a ' name= ' custom job ' Minute=*/3 hour=* day=* month=* weekday=* ' job= '/usr/sbin/nt Pdate 192.168.1.6 "'
192.168.1.7 | Success >> {
"Changed": true,
"Jobs": [
"Custom Job,"
"Linuxea Job"
]
}

192.168.1.4 | Success >> {
"Changed": true,
"Jobs": [
"Custom Job,"
"Linuxea Job"
]
}

192.168.1.8 | Success >> {
"Changed": true,
"Jobs": [
"Custom Job,"
"Linuxea Job"
]
}
View:

[Root@node ansible]# ansible all-a "Crontab-l"
192.168.1.7 | Success | Rc=0 >>
#Ansible: Custom Job
*/3 * * * */usr/sbin/ntpdate 192.168.1.6

192.168.1.8 | Success | Rc=0 >>
#Ansible: Custom Job
*/3 * * * */usr/sbin/ntpdate 192.168.1.6

192.168.1.4 | Success | Rc=0 >>
#Ansible: Custom Job
*/3 * * * */usr/sbin/ntpdate 192.168.1.6

[Root@node ansible]#
To create a group:

[Root@node ansible]# ansible-doc-s Group
Action:group
GID # Optional ' gid ' to set for the group.
Name= # Name of the group to manage.
State # Whether The group should is present or not on the remote host.
System # If ' Yes ', indicates that's group created is a system group.
[Root@node ansible]# ansible all-m group-a "gid=300 system=yes name=mysql"
192.168.1.8 | Success >> {
"Changed": true,
"GID": 300,
"Name": "MySQL",
"State": "Present",
' System ': true
}

192.168.1.7 | Success >> {
"Changed": true,
"GID": 300,
"Name": "MySQL",
"State": "Present",
' System ': true
}

192.168.1.4 | Success >> {
"Changed": true,
"GID": 300,
"Name": "MySQL",
"State": "Present",
' System ': true
}

[Root@node ansible]# ansible all-a "Tail-1/etc/group"
192.168.1.4 | Success | Rc=0 >>
MYSQL:X:300:

192.168.1.7 | Success | Rc=0 >>
MYSQL:X:300:

192.168.1.8 | Success | Rc=0 >>
MYSQL:X:300:

[Root@node ansible]#
Yum Installation

[Root@yum-down ~]# ansible-doc-s Yum
Action:yum
Conf_file Specify configuration file
Disable_gpg_check
Disablerepo
Enablerepo
List.
Name=
State
Update_cache
Install Corosync
[Root@yum-down ~]# ansible all-m yum-a "State=present name=corosync"

[Root@yum-down ~]# ansible all-a "Rpm-qa corosync"
192.168.1.4 | Success | Rc=0 >>
Corosync-1.4.7-2.el6.x86_64

192.168.1.8 | Success | Rc=0 >>
Corosync-1.4.7-2.el6.x86_64

192.168.1.7 | Success | Rc=0 >>
Corosync-1.4.7-2.el6.x86_64

[Root@yum-down ~]#
To start the service:

[Root@yum-down ~]# ansible all-m service-a "state=started name=httpd enabled=yes"
192.168.1.7 | Success >> {
"Changed": false,
' Enabled ': true,
"Name": "HTTPd",
"State": "Started"
}

192.168.1.8 | Success >> {
"Changed": false,
' Enabled ': true,
"Name": "HTTPd",
"State": "Started"
}

192.168.1.4 | Success >> {
"Changed": true,
' Enabled ': true,
"Name": "HTTPd",
"State": "Started"
}
View

[Root@yum-down ~]# ansible all-a "Service httpd status"
192.168.1.4 | Success | Rc=0 >>
HTTPD (PID 3702) is running ...

192.168.1.7 | Success | Rc=0 >>
HTTPD (PID 4046) is running ...

192.168.1.8 | Success | Rc=0 >>
HTTPD (PID 4097) is running ...

[Root@yum-down ~]#
Execute multiple commands

[Root@yum-down ~]# Cat Linuxea.yaml
-Hosts:all All hosts
Remote_user:root Execution User
Tasks
-Name:add Group Add Users
group:gid=1000 Name=linuxea System=no
-Name:excute a command execution time
Command:/bin/date
[Root@yum-down ~]#
Perform

[Root@yum-down ~]# Ansible-playbook Linuxea.yaml

Play [All] ********************************************************************

Gathering FACTS ***************************************************************
OK: [192.168.1.7]
OK: [192.168.1.8]
OK: [192.168.1.4]

TASK: [Add group] *************************************************************
Changed: [192.168.1.4]
Changed: [192.168.1.8]
Changed: [192.168.1.7]

TASK: [excute a command] ******************************************************
Changed: [192.168.1.4]
Changed: [192.168.1.7]
Changed: [192.168.1.8]

Play Recap ********************************************************************
192.168.1.4:ok=3 changed=2 unreachable=0 failed=0
192.168.1.7:ok=3 changed=2 unreachable=0 failed=0
192.168.1.8:ok=3 changed=2 unreachable=0 failed=0

[Root@yum-down ~]#
Bulk Replacement files
Modify the httpd port to 801 and then push the file back and restart the service

[Root@yum-down ~]# Cat Web.yaml
-Hosts:all
Remote_user:root
Tasks
-name:ensure Apache latest version to ensure Apache is the latest edition
Yum:state=latest name=httpd ensure httpd installation
-name:copy Configure file Copy files
copy:src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf force=yes Copy files
Notify: Replication completes the task
-Restart httpd
Handlers: Reboot, this is activated if the file is modified and restarted
-Name:restart httpd
SERVICE:NAME=HTTPD state=restarted
[Root@yum-down ~]#
Perform

[Root@yum-down ~]# Ansible-playbook Web.yaml

Play [All] ********************************************************************

Gathering FACTS ***************************************************************
OK: [192.168.1.4]
OK: [192.168.1.7]
OK: [192.168.1.8]

TASK: [Ensure Apache latest Version] ******************************************
OK: [192.168.1.4]
OK: [192.168.1.7]
OK: [192.168.1.8]

TASK: [Copy configure file] ***************************************************
Changed: [192.168.1.4]
Changed: [192.168.1.7]
Changed: [192.168.1.8]

Notified: [Restart httpd] *****************************************************
Changed: [192.168.1.4]
Changed: [192.168.1.7]
Changed: [192.168.1.8]

Play Recap ********************************************************************
192.168.1.4                : ok=4    changed=2     unreachable=0    failed=0  
192.168.1.7                : ok=4    changed=2     unreachable=0    failed=0  
192.168.1.8                : ok=4    changed=2    unreachable=0    failed=0  
View

[Root@yum-down ~]# ansible all-a "SS-TLNP"
192.168.1.4 | success | rc=0 >>
state     ;  recv-q send-q        Local address:port           Peer address:port
listen     0      128                       :::801                     :::*       Users: ("httpd", 4973,6), ("httpd", 4976,6) , ("httpd", 4977,6), ("httpd", 4978,6), ("httpd", 4979,6), ("httpd", 4980,6), ("httpd", 4981,6), ("httpd", 4982,6), ("httpd" ", 4983,6))

192.168.1.7 | success | rc=0 >>
state      recv-q send-q         local address:port          Peer address:port
listen     0      128                      :::801                     :::*       Users: ("httpd", 5302,6), ("httpd", 5305,6), ("httpd", 5306,6), ("httpd", 5307,6), (" HTTPd ", 5308,6), (" httpd ", 5309,6), (" httpd ", 5310,6), (" httpd ", 5311,6), (" httpd ", 5312,6))

192.168.1.8 | Success | Rc=0 >>
State Recv-q send-q The local address:port Peer address:port
LISTEN 0 128::: 801:::* Users: ("httpd", 5382,6), ("httpd", 5385,6), ( "HTTPd", 5386,6), ("httpd", 5387,6), ("httpd", 5388,6), ("httpd", 5389,6), ("httpd", 5390,6), ("httpd", 5391,6), ("httpd", 5392,6))

[Root@yum-down ~]#

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.