Ansible Common modules and APIs

Source: Internet
Author: User

Ansible Installation

Install Epel as a yum source for installing ansible (CentOS6.4):

RPM-UVH http://ftp.linux.ncsu.edu/pub/epel/6/i386/epel-release-6-8.noarch.rpm

Install Ansible:

Yum Install ansible-y

Configuration file:

Path:/etc/ansible/hosts
Configuration Description: Webservers is the group name, and the following IP or domain name is the host that belongs to the group.

[Webservers] 192.168. 1.111 192.168. 1.112 192.168. 1.113

Test:

Ansible webservers-m ping-k   # Ping the webservers group

Because the master and the host are not configured with the SSH certificate trust, you need to add the-K parameter when executing the ansible command, requiring a root (default) account password.

To configure the SSH certificate trust:
Main control side:
To generate a key pair:

SSH-KEYGEN-T RSA # All the way     to the return, in the/root/.ssh/directory will generate Id_rsa (private key), id_rsa.pub (public key) key pair

The managed host that sends the public key:

Ssh-copy-id-i/root/.ssh/id_rsa.pub [email protected]

Common modules

Command line call module format:

Ansible operation Target-M module name (default Module command)-a module parameter # Example:-M Ping

Help command:

ansible-Doc Module name # Example:ansible-doc Ping

1. Remote Command Module

Command: Execute remote shell commands
Script: Scripts stored on the host side of the controlled side, equivalent to Scp+shell
Shell: Execute script stored on the managed side
Cases:

' df-h ' # execute the df-h command on the controlled side ' /root/test.sh ' # Execute the test.sh script on the controlled side (test.sh script on the master side) ' /root/test.sh ' # Execute the test.sh script on the controlled side (test.sh script on the controlled side)

2. Copy Module

Copy: Copy files from the host to the managed side, similar to the SCP function
Cases:

' src=/root/test.py dest=/tmp owner=root group=root mode=0755 ' # Copy the test.py file from the host to the/TMP directory on the managed side, and specify the owner and permissions of the file

3. Stat Module

Stat: Get remote file status information, including Atime, CTime, Mtime, MD5, UID, GID, etc.
Cases:

' path=/etc/sysctl.conf ' # get the/etc/sysctl.conf file status information for the managed side

4. Get_url Module

Get_url: Enables the controlled end to download the specified URL, supports sha256sum checksum
Cases:

' url=http://www.baidu.com dest=/tmp/index.html mode=0440 force=yes ' # controlled end Download Baidu home page to/tmp/index.html

5. Software operation module (yum, APT)

Yum/apt:linux Platform software package management operations
Cases:

' Name=curl state=latest ' # The controlled end uses Yum to install the latest Curl ' Pkg=curl state=latest ' # The controlled end uses apt to install the latest Curl

6. Cron Module

Cron: Managed End cron Configuration
Cases:

" name= ' Check dirs ' hour= ' 5,2 ' job= ' ls-alh >/dev/null ' " # managed End cron Results: # Ansible:check dirs* 5,2 * * * LS-ALH >/dev/null

7. Mount Module

Mount: Managed side partition Mount
Cases:

' name=/mnt/data src=/dev/sd0 fstype=ext3 opts=ro state=present ' # mount the/dev/sd0 to/mnt/data with the right to RO

8. Service Module

Service: Managed terminal System Services management
Cases:

' name=httpd state=stopped ' # Close httpd Service ' name=httpd state=restarted ' #  'name=httpd state=started'# launch httpd service

9. Sysctl Package Management Module

SYSCTL: Controlled End sysctl Configuration
Cases:

' name= "Net.ipv4.ip_forward" value=1 sysctl_set=yes state=present reload=yes ' # set up route forwarding and take effect

10. User Service Module

User: Managed terminal system users
Cases:

" name=johnd Comment=hohn Doe " # Add user John " name=johnd state=absent Remove=yes " # Delete user John

Resources:

Liu Tians "Python Automated operations technology and best practices"

Ansible Common modules and APIs

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.