Ansible Common Modules Detailed

Source: Internet
Author: User

Ansible can be automatically managed using the command line, and Ansible's command-line management tools are supported by a series of modules and parameters, and we can view the module's help information through the Ansible-doc tool. This post will detail the functions and operations of the Ansible module.

ansible Command parameters:
    • -V: Output details (multiple V can be used)
    • -I PATH: Specify the Hosts file location
    • -F NUM: Specifies the number of open processes (default is 5)
    • -M Moule: Specify the name of the module (command by default)
    • -M Directory: Specify module directory to load module, default is/usr/share/ansible
    • -a,module_args: Specifying parameters for Module modules
    • -K: Prompts for SSH password instead of using SSH-based key authentication
    • -U USERNAME: Specifies the execution user of the Mobile ansible module: 1, Command Module

      Command: ansible [host] [-M module] [-a args]
      Ansible-doc-l #列出所有安装模块 (q exit)
      Ansible-doc-s Yum #列出yum模块描述信息和操作动作
      Ansible all-m command-a ' Date ' #查询date
      Ansible all-a ' ls/' #如果不加-m module, default Run command Module


2. Cron Module
两种状态(state):present表示添加        absent 表示移除ansible-doc -s cron                      #查看cron模块信息ansible all -m cron -a ‘minute="*/1" job="/usr/bin/echo heihei >> /opt/test.txt" name="test cron"‘          #-a: 指定添加参数     */1:每分钟执行      job:执行内容

ansible mysql -a ‘crontab -l‘        #查看crontab信息ansible mysql -m cron -a ‘name="test cron" state=absent‘

3. User Module

Ansible-doc-s User
Ansible all-m user-a ' name=test ' #创建用户


After the operation succeeds, view the results on the managed server:

Ansible mysql-m command-a ' tail/etc/passwd '
Ansible mysql-m user-a ' name=test01 state=absent ' #删除用户


After the operation succeeds, view the results on the MySQL server:

4. Group Module

Ansible mysql-m group-a ' name=mysql gid=330 system=yes '
Ansible mysql-a ' Tail/etc/group '

Ansible mysql-m user-a ' name=test02 uid=330 group=mysql system=yes '
#新建用户test02; set uid=306; add test02 to the MySQL group
Ansible mysql-a ' id test02 '

5. Copy Module

Ansible-doc-s Copy
Ansible all-m copy-a ' src=/etc/fstab dest=/opt/fstab.bk owner=root mode=644 '
#src: Original file dest: Target file after copy owner: Master mode: Permissions
Ansible mysql-a ' ls-l/opt ' #在控制主机上查看


After the operation succeeds, view the results in the appropriate directory on the managed server:

Ansible mysql-m copy-a ' content= "Hello world!" Dest=/opt/hello.txt '
Write "Hello world!" #复制文件hello. txt ”
Ansible mysql-a ' cat/opt/test.txt ' #在控制主机上查看


After the operation succeeds, view the results in the appropriate directory on the MySQL server:

6. File Module

Ansible-doc-s file
Touch/opt/file.txt
Ansible mysql-m file-a ' path=/opt/file.txt owner=test02 group=mysql mode=666 '
#对test文件设置属主, group, permissions


After the operation is complete, check the results under the MySQL server:

Ansible mysql-m file-a ' src=/opt/test.txt path=/opt/test.txt.link state=link '
#将src指的文件链接到path指的路径下


After the operation is complete, check the results in the appropriate directory of the MySQL server:

Of course, you can also create empty files, which are relatively simple to operate
Ansible mysql-m file-a ' path=/opt/abc.txt state=touch ' #创建空文件
Ansible mysql-m file-a ' path=/opt/abc.txt state=absent ' #删除

7. Ping module

Test whether managed hosts are online
Ansible all-m Ping

8. Yum Module

Ansible-doc-s Yum
Ansible webserver-m yum-a ' name=httpd ' #安装httpd
Ansible webserver-m yum-a ' name=httpd state=absent ' #移除httpd

9. Shell Module

Ansible-doc-s Shell
Ansible webserver-m user-a ' Name=jerry '
Ansible webserver-m shell-a ' echo abc123 | passwd--stdin Jerry '
#创建用户, no interaction setup password

10. Script Module

In your own server setup script, other servers go to execute
Ansible-doc-s Script
#!/bin/bash
echo "This is Test Script" >/opt/script.txt
chmod 666/opt/script.txt #设置权限
chmod +x test.sh #为脚本添加执行权限
Ansible all-m script-a ' test.sh '


When the operation is complete, view the execution results on the managed server:

11. Setup Module

Ansible-doc-s Setup
Ansible mysql-m Setup #查看mysql服务器上所有信息

12. Service Module

Ansible-doc-s Service
Ansible webserver-m service-a ' name=httpd enabled=true state=started '
#开启httpd服务; Enabled: Boot from boot
Ansible webserver-m service-a ' name=httpd enabled=true state=stopped ' #关闭httpd服务

Ansible Common Modules Detailed

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.