Ansible and its common modules 01
Directory:
1) Characteristics of ansible
2) Ansible Installation
3) ansible Configuration Add Management Host
4) Configure SSH-based Key management
5) How to view module Help
6) ansible Command Application Basics
7) ansible Common modules
Ansible Core Components:
Ansible Core
Host Iventory
Core moduies
Custom Modules
Playbooks (YAML,JINJIA2)
Connection Plugins
1) Characteristics of ansible:
Based on the Python language implementation, three key modules by Paramiko,pyyaml and JINJA2
Simple deployment, agentless
SSH protocol is used by default;
(1) Based on key authentication;
(2) Specify the account number and password in the inventory file;
Master-Slave Mode:
MASTER:ANSIBLE,SSH Client
Slave:ssh Server
Support for custom modules: support for various programming languages
Support Playbook
Complete various "Tasks" based on "modules"
2) Ansible Installation
Installation depends on the Epel source:
Yum install epel-release Yum Info ansibleyum Install ansible-y
Configuration file:/etc/ansible/ansible.cfg
Inventory:/etc/ansible/hosts
3) ansible Configuration Add Management Host
1. Configuring managed Hosts
[[email protected] ~]# cd/etc/ansible/[[email protected] ansible]# lsansible.cfg hosts Roles[[email protected] ansible] # CP hosts{,.bak}# VI hosts[websrvs]192.168.31.212192.168.31.213[dbsrvs]192.168.31.214
4) Configure SSH-based Key management
(1) Perform the following on the management host:
Ssh-keygen-t rsa# ssh-copy-id-i/root/.ssh/id_rsa.pub [email protected]# ssh-copy-id-i/root/.ssh/id_rsa.pub [Email Pro tected]# ssh-copy-id-i/root/.ssh/id_rsa.pub [email protected]
(2) Test all host synchronization time:
# SSH [email protected] ' date ' # SSH [email protected] ' ntpdate pool.ntp.org ' # SSH [email protected] ' ntpdate pool.ntp.org ' # SSH [email protected] ' ntpdate pool.ntp.org '
5) How to view module Help
Ansible-doc-l
Ansible-doc-s module_name
6) ansible Command Application Basics
Syntax: Ansible
[-M module_name] [-A args]
-F Forks: Number of concurrent threads started;
-M Module_name: module to use
-A args: module-specific parameters;
7) ansible Common modules
# # Command: Commands module, default module, for remote execution of commands;
such as: ansible all-a ' Date '
# ansible websrvs-m command-a ' Date ' # ansible dbsrvs-m command-a ' Date ' # ansible all-m command-a ' Date ' # #运用所有主机
# # Cron: Adding a mobile scheduled task
State
Present: Installation
Absent: Remove
Added: # ansible websrvs-m cron-a ' minute= "*/10" job= "/bin/echo Hello" name= "test cron" ' Remove: # ansible websrvs-m cron-a ' min Ute= "*/10" job= "/bin/echo Hello" name= "test cron" state=absent "
# # User: Add removal user
Name=: Indicates the user name created
Add User # Ansible all-m user-a ' name= "user1" ' Remove user # ansible all-m user-a ' name= ' user1 ' state=absent '
# # Group: Adding a move Group
# ansible websrvs-m group-a ' name=mysql gid=306 system=yes ' # ansible websrvs-m user-a ' Name=mysql uid=306 system=yes g Roup=mysql '
# Copy: Copy files
src=: Define local source file path
dest=: Define the remote target absolute path
Content=: Instead of src=, it is used to generate the content of the target file directly with the information specified here;
# ansible all-m copy-a ' src=/etc/fstab dest=/tmp/fstab.ansible owner=root mode=640 ' # ansible all-m copy-a ' content= ' he Llo ansible\nhi magedu "Dest=/tmp/test.ansible"
# # File: Setting files Properties
Path=: Specifies the file path, which can be replaced by using name or dest;
To create a symbolic link to a file:
Src=: Indicates the source file
Path=: Specify symbolic Link file path
# ansible all-m file-a ' owner=mysql group=mysql mode=644 path=/tmp/fstab.ansible ' Create symbolic Link: # ansible all-m file-a ' path=/ Tmp/fstab.link src=/tmp/fstab.ansible State=link '
# # Ping: Test whether the specified host can connect;
# ansible All-m Ping
# # Service: Specifies the operating state;
Enabled=: Whether the boot automatically start, the value is true or false;
Name=: Service Name
State=: State, value has started,stopped,restarted;
# ansible websrvs-m service-a ' enabled=true name=httpd state=started ' # ansible websrvs-a ' service httpd status ' # Ansibl E websrvs-a ' chkconfig--list httpd '
# # Shell: Running commands on a remote host
In particular, the use of the pipeline and other functions of complex commands;
# ansible all-m user-a ' name=user1 ' # ansible all-m shell-a ' echo mageedu | passwd--stdin user1 '
# # Script: Copy the local script to the remote host and run it;
Note: To specify a script with a relative path
[Email protected] ~]# cat/tmp/test.sh #!/bin/bashecho "Hello ansible from Script" >/tmp/script.ansibleuseradd user2# Ansible all-m script-a "/tmp/test.sh"
# # Yum: Install package
Name=: Indicates the package to be installed, you can bring the version number;
state=:p resent,latest means installation, absent means unloading;
# ansible all-m yum-a "name=zsh" # ansible all-m yum-a "Name=zsh state=absent"
# # Setup: Collecting Facts for remote hosts
Each managed node reports its host-related information, such as operating system version, IP address, etc. to the remote Ansible host before receiving and running the management command;
# ansible ALL-M Setup
Lh01_ansible and its common modules 01