Ansible is based on modularity, with specific modules to complete specific tasks
Based on the Python language implementation, implemented by three key modules of Paramiko, Pyyaml and JINJA2
Simple deployment, agentless (no client deployment, client management via SSH protocol)
Based on master-slave mode
Supports custom modules (other programming language-written modules can also be called via Ansible interface)
Support Playbook continuous tasks in order of completion
Components of the Ansible:
Ansible core:ansible own Core module
Host Inventory: Master Library, defining a manageable list of hosts
Connection plugins: Connection plug-in, default SSH-based protocol connection
Modules:core modules (with module or core module), custom modules (user-defined module, users can write custom modules based on any programming language)
Configuration file:
(1) Main profile of the Ansible application:/etc/ansible/ansible.cfg
(2) Host Inventory definition control console:/etc/ansible/hosts
Installation: yum-y install epel-release yum-y Install Ansible
Use format for commands: Ansible
Vim/etc/ansible/ansible.cfg
Above is a sample custom single host
To customize a single group example, the characters in brackets are group names, and a host can belong to more than one master
You can use Ansible-i/somefile to indicate the inventory host file path
customizing two groups
Ansible-doc commands to see the Help, Ansible-doc-l lists which modules ansible support, Ansible-doc-s module-name See what the module supports
Parameters
The simplest command: ansible web1-m Ping, because SSH is not based on key authentication, this is shown here unreachable
First you have to configure the key verification on each host, first create a key pair, the command is simple Ssh-keygen, add three times enter
Cat/root/.ssh/id_rsa.pub >/root/.ssh/authorized_keys chmod 700/root/.ssh/authorized_keys
Scp-p/root/.ssh/authorized_keys 192.168.238.170:/root/.ssh/(the key is sent to each host via SCP)
At this point, test ansible all-m Ping All to indicate that all hosts
Ansible Common Module Introduction
Ansible
Args: is the key=value format
Command: Default module, can be omitted. Operation command on the remote host, command not key=value format, followed directly
Example: Ansible all-m command-a ' ifconfig '
Note: command cannot be executed for pipe class
User: Add Delete user command
For example: Ansible web1-m user-a "Name=apache state=present" name specifies user name, present means create, absent means delete
To delete a home directory, to use Remove=true, create a system user to add System=true
Cron defines the entries in the Crontab
Example: Ansible all-m cron-a "minute= ' */5 ' job= '/usr/sbin/ntpdate 210.72.145.44 &>/dev/null ' name= ' Sync Time '"
The name above must specify
Delete: ansible all-m cron-a "name= ' Sync Time ' state=absent", additional parameters month= day= weekday=
File Settings Files Properties
Example: Ansible web1-m file-a "Path=/tmp/dir state=directory" Create dir directory
Ansible web1-m file-a "Path=/tmp/nihao owner=root state=touch" Create a Nihao empty file
Yum Package Manager
Example: Ansible web1-m yum-a "name=httpd state=present" Install httpd package
Copy complete File replication
Example: Ansible all-m copy-a ' src=/etc/fstab dest=/tmp/fstab mode=100 ' src Specifies the local file absolute path, dest specifies that the target host is absolutely
The path
Service startup shuts down individual services
Example: Ansible web1-m service-a ' name=httpd state=started enabled=true ' Start HTTP service, enabled=true specify boot
Shell runs shell commands in the shell environment
Example: ansible all-m shell-a ' echo "0" | passwd--stdin Root '
Script specifies a local scripting file that is passed to the remote host for execution once
vim/tmp/date.sh
Example: Ansible all-m script-a '/tmp/date.sh ' executes a script on all hosts to set the time zone
Ansible Study Notes