First, ansible characteristics
1, do not need to install the client, through the SSHD communication
2, module-based work, the module can be developed by any preamble
3, not only support command line to use the module, also support writing YAML format playbook
4. Support sudo
5, there is the provision of UI (browser graphics) Www.ansible.com/tower10 host free
6. Open Source UI https://github.com/alaxli/ansible_ui documentation
http://download.csdn.net/detail/liyang23456/7741185
Second, ansible installation
Two machines 10.10.10.201 and 10.10.10.156
Only need to install ansible on the 10.10.10.201
# yum-y Install Epel-release
# yum-y Install Ansible
Third, ansible configuration key
Generate key pair in 10.10.10.201
# ssh-keygen-t RSA Direct return, do not set the secret key password
Put the public key (id_rsa.pub) contents into the/root/.ssh/authorized_keys of 10.10.10.156
# SCP Id_rsa.pub Slave.fanisk.com:/root/.ssh/authorized_keys
This machine also needs to operate
# chmod 600/root/.ssh/authorized_keys
Turn off SELinux
Iii. ansible Remote Execution command
# ansible fansik-m command-a ' hostname '
Fansik the name of the main unit,-m behind the module name,-A after the command, you can also write an IP, for a machine to execute the command
There is also a module shell can be implemented (can execute script, support pipe character):
# ansible fansik-m shell-a ' hostname '
If there is an error (it will appear when you copy the file):
slave.fansik.com | failed! = = {
"Changed": false,
"Checksum": "7267a346473f1d39c1e8c2c0b43d40a42c37eb28",
"Failed": true,
"MSG": "Aborting, Target uses selinux but Python bindings (libselinux-python) aren ' t installed!"
To install the Libselinux-python package:
# yum-y Install Libselinux-python
Edit Ansible configuration file, add host group
# vim/etc/ansible/hosts
[Fansik]
127.0.0.1
Slave.fansik.com
Fansik the name of the main unit, custom, the following is the group of machines can be IP can also be host name
Iv. ansible copy files or directories
# ansible slave.fansik.com-m copy-a "Src=/tmp/wohenhao Dest=/tmp/wobuhao owner=root group=root mode=0644"
Note: The source directory will be placed under the target directory, if the target directory does not exist, it will be created automatically, if the copy is a file, dest specified name and source if it is different, and it is not already exist directory, equivalent to copy past after renaming, but instead, If Dest is a directory that already exists on the target machine, the file will be copied directly to the directory
# ansible fansik-m copy-a "src=/etc/passwd Dest=/tmp/wobuhao"
The/tmp/wobuhao and the/etc/passwd on the source machine are always there, if the/tmp/wobuhao directory already exists on the target machine, the passwd file will be created under the/tmp/wobuhao directory
V. Ansible remote Execution Script
Remote execution scripts need to distribute scripts to each machine
# ansible fansik-m copy-a "src=/tmp/test.sh dest=/tmp/test.sh mode=0755"
Then execute the script in bulk
# ansible fansik-m shell-a "/tmp/test.sh"
V. Ansible implementation of the Mission plan
Create a task schedule
# ansible fansik-m cron-a "name= ' Fanjinbao ' job= '/bin/touch/tmp/shishi.txt ' minute=5 ' weekday=6 '
Delete a task schedule
# ansible fansik-m cron-a "name= ' Fanjinbao ' state=absent"
Other time means: minutes: Minute, Hours: hour, date: day, Month: months
V. Ansible implementation of the Mission plan
# ansible fansik-m yum-a "NAME=HTTPD"
You can also add state=installed after name
# ansible fansik-m service-a "name=httpd state=started enabled=yes"
The name here is the service name of the CentOS system and can be found by Chkconfig--list
Ansible Use of documents
# ansible-doc-l List of all modules
# Ansible-doc Service View documentation for the specified module
Vi. Use of Ansible Playbook
1. Create the Playbook.txt file in the/tmp/directory
# VIM/ETC/ANSIBLE/TEST.YML
---
-Hosts:fansik
Remote_user:root
Tasks
-Name:test_playbook
Shell:touch/tmp/playbook.txt
Description: The host parameter specifies which hosts to operate on;
The user parameter specifies what users are using to log on to the remote host operation;
tasks specifies a task whose name parameter is also a description of the task, which is printed during execution
Performed: # Ansible-playbook TEST.YML
2. Create test user
---
-Name:create_user
Hosts:slave.fansik.com
User:root
Gather_facts:false
VARs
-User: "Test"
Tasks
-Name:create User
User:name= "{{User}}"
Description: The name parameter provides an overview of the functionality implemented by the playbook, which prints the value of the name variable during execution, which can be omitted, and the gather facts parameter specifies whether the Setup module gets the host-related information before executing the following task section. This is used in the later task to use the information obtained by Setup; VARs parameter, specifying a variable, where a user variable is specified, the value is test, it should be noted that the value of the variable must be quoted in quotation marks; The user specified a call to the user module, The name is a parameter in the user module, and the added user name invokes the value of the above variable.
Seven, Ansible playbook in the cycle
Change the slave.fansik.com/tmp/directory of the 1.txt, 2.txt, 3.txt permissions of 600
# Vim Loop.yml
---
-Hosts:slave.fansik.com
User:root
Tasks
-Name:change mode for files
file:path=/tmp/{{Item}} mode=600 owner=root Group=root
With_items:
-1.txt
-2.txt
-3.txt
Eight, Ansible playbook condition judgment
If there is 10.10.10.201 in the Fansik host group, the machine is created under the/tmp/of the machine when.txt
---
-Hosts:fansik
User:root
Gather_facts:true
Tasks
-Name:use when
Shell:touch/tmp/when.txt
When:facter_ipaddress = = "10.10.10.201"
Nine, Ansible playbook in the handers
After performing tasks, some actions are performed after the server has changed, such as after we have modified the configuration file, we need to restart the service
---
-Hosts:fansik
Remote_user:root
Tasks
-Name:test Copy
COPY:SRC=/ETC/PASSWD Dest=/tmp/2.txt
Notify:test handlers
Handlers:
-Name:test handlers
Shell:echo "121212" >>/tmp/2.txt
Note: Only the copy module is actually executed to invoke the following handlers related operation. That is, passwd and 2.txt content is the same, and does not go to execute handlers inside the shell command command, this comparison is suitable for the configuration file changes, restart the service and other operations.
Ansible installation configuration and use