Automated operation and Maintenance-ansible (Part II)

Source: Internet
Author: User
Tags ansible modules

Ansible Command Application Basics
  • A previous article on the installation and role of Ansible, interested in can see Ansible introduction and installation. Learning Ansible is to re-learn the command and grammar.
  • Ansible can be automated with the command line, with the following basic syntax:
ansible 
  • Ansible's command-line management tools are supported by a series of modules and parameters that can be added with-H or--help to get help. If you use the Ansible-doc tool, you can view additional help information through ansible-doc-h or Ansible-doc--help.
  • Ansible-doc is a tool for viewing module help information, the most important option-L is used to list the modules that can be used,-s is used to list the description of a module and use of the model. The description and action actions of the Yum module are listed as follows:
  [[email protected] ~]# ansible-doc-s yum//lists Help for Yum modules-name:manages packages with the ' Yum ' PAC                               Kage Manager Yum:allow_downgrade: # Specify if the named package and version are allowed to downgrade a maybe Already installed higher version of the. Note that setting allow_downgrade=true can make this module is Has in a non-idempotent the. The task could end up with a set of packages this does not mat                               CH The complete list of specified packages to install (because                               Dependencies between the downgraded package and others can cause changes to the Packages which were in the earlier transaction).  
Ansible Common Module Explanation

Ansible comes with a lot of modules, can be issued to perform various management tasks Ansible, by default, the command module, that is, the-m option can also run this module, mainly for running commands on the managed host:

Command Module
[[email protected] ~]# ansible all -a ‘tail -n 3 /etc/passwd‘   //查看所有主机passwd最后3行 192.168.154.132 | CHANGED | rc=0 >>     //这是第一台被控主机test01:x:1001:1001::/home/test01:/bin/bashapache:x:48:48:Apache:/usr/share/httpd:/sbin/nologinjerry:x:1002:1002::/home/jerry:/bin/bash192.168.154.133 | CHANGED | rc=0 >>         //这是第二台被控主机yihong:x:1000:1000:yihong:/home/yihong:/bin/bashtest01:x:1001:1001::/home/test01:/bin/bashtest02:x:306:306::/home/test02:/bin/bash

Cron Module

The cron module in Ansible is used to define task schedules. There are two states (state): present means Add (omit does not write by default), and absent means remove.

1 • Add Scheduled Tasks

[[email protected] ~]# ansible mysql -m cron -a ‘minute="*/1" job="/usr/bin/echo hello word" name="test job"‘        //添加计划任务,每分钟执行一次 hello word 192.168.154.133 | CHANGED => {    "changed": true,     "envs": [],     "jobs": [        "test job"    ]}[[email protected] ~]# ansible mysql -a ‘crontab -l‘     //查看计划任务192.168.154.133 | CHANGED | rc=0 >>#Ansible: test job*/1 * * * * /usr/bin/echo hello word

2 • Remove Scheduled Tasks

[[email protected] ~]# ansible mysql -m cron -a ‘name="test job" state=absent‘   192.168.154.133 | CHANGED => {    "changed": true,     "envs": [],     "jobs": []}[[email protected] ~]# ansible mysql -a ‘crontab -l‘192.168.154.133 | CHANGED | rc=0 >>

User Module

The user module in Ansible 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.

1 · Create a normal user

[[email protected] ~]# ansible mysql -m user -a ‘name="test01"‘  //这里需要注意双影号192.168.154.133 | SUCCESS => {[[email protected] ~]# ansible mysql -a ‘tail -n 3 /etc/passwd‘  //查看passwd 文件192.168.154.133 | CHANGED | rc=0 >>yihong:x:1000:1000:yihong:/home/yihong:/bin/bashtest01:x:1001:1001::/home/test01:/bin/bash    //新创建的用户test01

2 · Create a system User

[[email protected] ~]# ansible mysql -m user -a ‘name="tom" system=yes shell=/sbin/nologin‘  192.168.154.133 | CHANGED => {    //创建系统用户,执行shell登陆环境    "changed": true,

3 · Delete users

[[email protected] ~]# ansible mysql -m user -a ‘name="test01" state=absent‘192.168.154.133 | CHANGED => {     //删除成功
Group Module

The group module in Ansible is used to manage user groups

Create a MySQL group and add the Tom user to the MySQL group.

[[email protected] ~]# ansible webserver -m group -a ‘name=mysql gid=306 system=yes‘192.168.154.132 | CHANGED => {    "changed": true,     "gid": 306,
[[email protected] ~]# ansible webserver -m user -a ‘name=tom uid=307 system=yes group=mysql‘192.168.154.132 | CHANGED => {
Copy Module

The copy module in Ansible is used to implement file copying and batch issuing files. Where SRC defines the local source file path, uses Dest to define the managed host file path, and content is used to generate the target file by specifying the information content.

1 • Columns such as the following command: Copy the local file/etc/fstab to the/tmp/fstab.ansible on the managed host, and set the owner to root with the permission set to 640

[[email protected] ~]# ansible mysql -m copy -a ‘src=/etc/fstab dest=/tmp/fstab.ansible owner=root mode=640‘192.168.154.133 | CHANGED => {    "changed": true,     "checksum": "d855a342669b42e1b6632f814cbb9f327d472b28"[[email protected] ~]# ansible mysql -a ‘ls -l /tmp/fstab.ansible‘192.168.154.133 | CHANGED | rc=0 >>-rw-r-----. 1 root root 617 10月 22 10:42 /tmp/fstab.ansible

2 · write "Hell Word" to the/tmp/fstab.ansible file.

[[email protected] ~]# ansible mysql -m copy -a ‘content="Hell Word" dest=/tmp/fstab.ansible‘192.168.154.133 | CHANGED => {    "changed": true[[email protected] ~]# ansible mysql -a ‘cat /tmp/fstab.ansible‘192.168.154.133 | CHANGED | rc=0 >>Hell Word
File module

Use the file module in Ansible to set the properties of the files. It uses path to specify the file path, uses SRC to define the source file path, and uses name or dest to replace the symbolic link that creates the file.

1 · GCA, set/tmp/fstab.ansible to belong to the main MySQL, belong to the group of MySQL, permissions of 644. It is important to note that your system needs to have this MySQL user before you can create one on your own.

[[email protected] ~]# ansible mysql -m file -a ‘owner=mysql group=mysql mode=644 path=/tmp/fstab.ansible‘192.168.154.133 | CHANGED => {    "changed": true, [[email protected] ~]# ansible mysql -a ‘ls -l /tmp/fstab.ansible‘192.168.154.133 | CHANGED | rc=0 >>-rw-r--r--. 1 mysql mysql 9 10月 22 10:47 /tmp/fstab.ansible
Ping module

The use of the ping module is simple, and its purpose is to detect the connectivity of the specified host.

[[email protected] ~]# ansible all -m ping 192.168.154.132 | SUCCESS => {    "changed": false,     "ping": "pong"}192.168.154.133 | SUCCESS => {    "changed": false,     "ping": "pong"}
Service Module

Use the Service module in Ansible to control the running state of the management service. Where enabled indicates whether to start, evaluates to TRUE or FALSE, uses name to define the service name, and state to specify the service status with the values: Started, stoped, restarted.

1 · View httpd service status.

[[email protected] ~]# ansible all -a ‘service httpd status‘ [WARNING]: Consider using the service module rather than running service.  If you need to use commandbecause service is insufficient you can add warn=False to this command task or set command_warnings=Falsein ansible.cfg to get rid of this message.

Note: Here is a warning, probably meaning that if you are only querying the status, the module is fine, but the service is used primarily for his use.

2 • Start the httpd and set it to boot from boot

[[email protected] ~]# ansible webserver -m service -a ‘enabled=true name=httpd state=started‘192.168.154.132 | SUCCESS => {    "changed": false,     "enabled": true
Shell Module

The shell module in Ansible can run commands on managed hosts and support complex commands such as pipe symbols.

Columns such as: Create a user and use no interactive mode to set a password for the user

[[email protected] ~]# ansible mysql -m user -a ‘name="nginx" system=yes shell=/sbin/nologin‘                     //创建 nginx 用户192.168.154.133 | CHANGED => {    "changed": true[[email protected] ~]# ansible mysql -m shell -a ‘echo abc123 | passwd --stdin nginx‘192.168.154.133 | CHANGED | rc=0 >>更改用户 nginx 的密码 。passwd:所有的身份验证令牌已经成功更新。
Script Module

The script module in Ansible can copy local scripts to the managed host to run. Note that you use a relative path to specify the script.

Columns such as: Edit a local script test.sh, copy to the managed host to run.

[[email protected] ~]# vim test.sh#!/bin/bashecho "this is test job" > /tmp/script.ansible[[email protected] ~]# chmod +x test.sh[[email protected] ~]# ansible mysql -m script -a ‘test.sh‘192.168.154.133 | CHANGED => {    "changed": true,     "rc": 0,     "stderr": "Shared connection to 192.168.154.133 closed.\r\n"
[[email protected] ~]# ansible mysql -a ‘cat /tmp/script.ansible‘192.168.154.133 | CHANGED | rc=0 >>this is test job
Yum Module

The Yum module in Ansible is responsible for installing and uninstalling packages on managed hosts, but needs to configure its own Yum repository in advance on each node. Where name specifies the package to be installed and the version number of the package is required, otherwise the latest package is installed: Use stare to specify the state of the installation package, present, latest to indicate the installation, ansent to uninstall.

1 • Install software zsh

[[email protected] ~]# ansible mysql -m yum -a ‘name=zsh‘192.168.154.133 | CHANGED => {    "ansible_facts": {        "pkg_mgr": "yum"    },     "changed": true,
[[email protected] ~]# ansible mysql -a ‘rpm -q zsh‘ [WARNING]: Consider using the yum, dnf or zypper module rather than running rpm.  If you need to usecommand because yum, dnf or zypper is insufficient you can add warn=False to this command task or setcommand_warnings=False in ansible.cfg to get rid of this message.192.168.154.133 | CHANGED | rc=0 >>zsh-5.0.2-28.el7.x86_64

2 · Uninstalling the ZSH Package

[[email protected] ~]# ansible mysql -m yum -a ‘name=zsh state=absent‘192.168.154.133 | CHANGED => {    "ansible_facts": {        "pkg_mgr": "yum"    },     "changed": true
Setup Module

Using the Setup module in Ansible, you can collect and view device information for managed hosts. Each managed host sends its own information to the control host before it receives and runs the management commands!

[[email protected] ~]# ansible mysql -m setup   //这里信息量较大,以下的省略192.168.154.133 | SUCCESS => {    "ansible_facts": {        "ansible_all_ipv4_addresses": [            "192.168.122.1",             "192.168.154.133"
Summarize
  • The Ansible modules are very numerous. You can use the "ansible-doc-s module name" To view the usage of the module when you do not know how the module is used.
  • "Ansible-doc-l" can see which modules are available!

Automated operation and Maintenance-ansible (Part II)

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.