Automated Operation Koriyuki ansible application Base module (Super detail)

Source: Internet
Author: User

Ansible is a framework structure that works based on modules. The bulk deployment capability is implemented by the modules that are run by ansible. In short, Ansible is based on "modules" to accomplish various "tasks". The basic syntax for Ansible Common Core module modules is as follows
ansible [主机] [-m 模块] [-a args]  //命令格式# ansible-doc -l //列出所有已安装的模块  注:按q退出# ansible-doc -s yum //-s列出yum模块描述信息和操作动作
The 1.command Module command module is used to run commands on managed hosts.
# ansible 192.168.92.156 -m command -a ‘date‘ //指定IP执行date# ansible node2 -m command -a ‘date‘ //指定被管理主机中的分类执行date# ansible all -m command -a ‘date‘ //所有主机清单上的主机执行date# ansible all -a ‘ls /etc‘ //若省略-m选项,默认运行command模块

The 2.cron module cron module is used to define a task schedule, two states (state):p resent to add (which can be omitted) and absent to remove.
# ansible-doc -s cron //查看cron模块信息# ansible node2 -m cron -a ‘minute="*/1" job="/bin/echo hello" name="test cron job"‘ //添加计划任务# ansible node2 -a ‘crontab -l‘ //查看计划任务信息# ansible node2 -m cron -a ‘name="test cron job" state=absent‘ //移除计划任务

The 3.user module user module is used to create new users and to change, delete existing users. Where the name option is used to indicate the name of the user created.
# ansible-doc -s user //查看user模块信息# ansible node2 -m user -a ‘name="test01"‘ //创建用户test01# ansible node2 -a ‘tail /etc/passwd‘ //查看test01 用户# ansible node2 -m user -a ‘name="test01" state=absent‘ //删除用户test01

The 4.group Module group module is used to manage user groups
# ansible-doc -s group //查看group模块信息# ansible node3 -m group -a ‘name=node3 gid=306system=yes‘ //创建node3系统组# ansible node3 -a ‘tail /etc/group‘ //查看组信息# ansible node3 -m user -a ‘name=test01 uid=306 system=yes group=node3‘ //将test01用户添加到node3组中# ansible node3 -a ‘id test01‘ //查看用户信息

The 5.copy module copy module is used to implement file copying and batch issuance, where the local file path is defined with SRC and the managed host file path is defined using Dest.
# ansible-doc -s copy //查看copy模块信息# ansible node3 -m copy -a ‘src=/etc/fstab dest=/opt/fstab.bk owner=root mode=640‘//将本地文件fstab复制到被管理主机上,所有者为root,权限为640# ansible node3 -a ‘ls -l /opt‘ //查看文件信息# ansible node3 -a ‘cat /opt/fstab.bk‘ //查看文件内容# ansible node3 -m copy -a ‘content="HELLO ansible" dest=/opt/fstab.bk‘ //在fstab.bk文件中写入HELLO ansible# ansible node3 -a ‘cat /opt/fstab.bk‘ //查看内容


The 6.file module Filem module is used to set file properties. It uses path to specify the file path, uses SRC to define the original file path, and uses name or dest to replace the symbolic link that created the file.
# ansible-doc -s file //查看file模块信息# ansible node3 -m file -a ‘owner=jerry group=jerry mode=644 path=/opt/fstab.bk‘//设置文件fstab.bk的所属主为jerry,所属组为jerry,权限644# ansible node3 -m file -a ‘path=/opt/fstab.link src=/opt/fstab.bk state=link‘//设置/opt/fstab.link为/opt/fstab.bk的链接文件# ansible node3 -m file -a ‘path=/opt/fstab.link state=absent‘ //删除一个文件# ansible node3 -m file -a ‘path=/opt/abc state=touch‘ //创建一个空文件

The 7.ping module ping module is used to detect the connectivity of a specified host.
# ansible all -m ping  //检测所有主机

The 8.yum module Yum module is responsible for installing and uninstalling packages on managed hosts, where name specifies the packages installed, present, latest used to represent the installation, absent means uninstallation.
# ansible-doc -s yum //查看yum模块信息# ansible node3 -m yum -a ‘name=httpd‘ //安装httpd软件包# ansible node3 -m yum -a ‘name=httpd state=absent‘ //卸载httpd软件包

The 9.service Module Service module is used to control the running state of the managed service, where enabled indicates whether it is powered on or not, evaluates to TRUE or FALSE, uses name to define the service name, uses state to specify the service status, and takes values of started, stoped, restarted.
# ansible-doc -s service //查看service模块信息# ansible node3 -a ‘systemctl status httpd‘ //查看httpd服务状态# ansible node3 -m service -a ‘enabled=true name=httpd state=started‘ //启动httpd服务并设置开机自动启动

The 10.shell module Shell module can run commands on managed hosts and support complex commands for pipe-break functionality.
# ansible-doc -s shell //查看shell模块信息# ansible node3 -m user -a ‘name=tom‘ //创建用户tom# ansible node3 -m shell -a ‘echo 123123|passwd --stdin tom‘ //使用无交互模式给用户设置密码

The 11.script module script module can copy local scripts to a managed host to run, and it is important to note that a relative path is used to specify the script.
# ansible-doc -s script //查看script模块信息编辑一个本地脚本test.sh,复制到被管理主机上运行。# vim test.sh   #!/bin/bash   echo "this is test script" > /opt/test.txt   chmod 666 /opt/test.txt# chmod +x test.sh # ansible node3 -m script -a ‘test.sh‘ //执行脚本

The 12.setup module setup module is used to collect and view the facts of managed hosts (facts is a feature that Ansible collects information about managed host devices). Each managed host sends its own information (operating system version, IP address, and so on) to the control master before accepting and running the administrative commands.
# ansible-doc -s setup //查看setup模块信息# ansible node3 -m setup  //查看node3主机输出信息

Automated Operation Koriyuki ansible application Base module (Super detail)

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.