Common ansible module usage
Ansible provides many modules by default. In Linux, we can run the ansible-doc-l command to check which modules are supported by ansible, you can use the ansible-doc-s Module name to view the parameters available for this module.
The following describes several common modules:
Copy module file module cron module group Module user Module yum module service module script module ping module command module raw module get_url module synchronize Module
Copy module:
Objective: To copy the. sh file under the master/root directory to a specified Node
Command: ansible 10.1.1.113-m copy-a 'src =/root/a. sh dest =/tmp /'
Execution result:
File module:
Purpose: To change the/tmp/t. sh permission on a specified node to 755, and the owner and group are root.
Command: ansible all-m file-a "dest =/tmp/t. sh mode = 755 owner = root group = root"
Execution result:
Cron module:
Purpose: To define a scheduled task on a specified node and update the task to the master node every 3 minutes.
Command: ansible all-m cron-a 'name = "custom job" minute = */3 hour = * day = * month = * weekday = * job = "/usr/sbin/ntpdate 172.16.254.139 "'
Execution result:
Group module:
Objective: to create a group named nolinux and gid 2014 on all nodes
Command: ansible all-m group-a 'Gid = 2014 name = nolinux'
Execution result:
User Module:
Purpose: To create a user with the username of nolinux and a group of users with the name of nolinux on a specified Node
Command: ansible 10.1.1.113-m user-a 'name = nolinux groups = nolinux state = present'
Run the following command:
Supplement: User deletion example
Yum module:
Objective: To install the lrzsz service on a specified Node
Command: ansible all-m yum-a "state = present name = httpd"
Execution result:
Service module:
Purpose: To start the puppet service on a specified node and enable it to start automatically
Command: ansible 10.1.1.113-m service-a 'name = puppet state = restarted enabled = yes'
Execution result:
Script module:
Purpose: run the/root/a. sh script on the specified node (this script is on the ansible control node)
Command: ansible 10.1.1.113-m script-a'/root/a. Sh'
Execution result:
Ping module:
Purpose: To check whether the machine on the specified node can be connected again
Command: ansible 10.1.1.113-m ping
Execution result:
Command module:
Purpose: To run the hostname command on a specified Node
Command: ansible 10.1.1.113-m command-a 'hostname'
Execution result:
Raw module:
Objective: To run the hostname command on the 10.1.1.113 Node
Command: ansible 10.1.1.113-m raw-a 'hostname | tee'
Execution result:
Get_url module:
Objective: To download the http: // 10.1.1.116/favicon. ico file to the/tmp directory of the specified Node
Command: ansible 10.1.1.113-m get_url-a' url = http: // 10.1.1.116/favicon. ico dest =/tmp'
Execution result:
Synchronize module:
Objective: To push the master prosecution/root/a directory to the/tmp directory of the specified Node
Command: ansible 10.1.1.113-m synchronize-a 'src =/root/a dest =/tmp/compress = yes'
Execution result:
Delete = yes make the content on both sides the same (that is, the push party is the main)
Compress = yes. compression is enabled by default.
-- Exclude =. git ignore the file ending with synchronization. git
Because of the module, push is used by default. Therefore, if you are using the pull function, you can refer to the example below for implementation.
Mode = pull change push mode to pull mode
Objective: To pull the/tmp/a directory of the 10.1.1.113 node to the/root directory of the master node
Command: ansible 10.1.1.113-m synchronize-a 'mode = pull src =/tmp/a dest =/root /'
Execution result:
Because the archive parameter is enabled by the module by default, the recursive, links, perms, times, owner, group, and-D parameters are enabled by default. If you set this parameter to no, you will stop many parameters. For example, this will lead to recursive failure for the following purposes, resulting in pulling failure.
Other related parameter explanations:
Dest_port = 22 # specify the ssh port of the target host. The ansible_ssh_port variable in the ansible configuration file has a higher priority than the dest_port variable rsync_path # specifies the rsync command to run on the remote server. For more information, see the -- rsync-path parameter of the rsync command. -- rsync-path = PATH # specifies the path of the rsync command on the remote server. rsync_timeout # specifies the IP address timeout time of the rsync operation, the same effect as the -- timeout parameter of the rsync command
OK!
The above lists some of the modules frequently used in daily O & M. More module information will be improved later. You can go to the official website to view more information.