ping module
ping is a common module to test whether the SSH connection of the remote node is ready. However, it is not as simple as the Linux command to ping the remote node. Instead, first check whether the remote node can log in through SSH, and then check whether its Python version meets the requirements. If it meets the requirements, it will return a response, indicating success. The usage is as follows:
ansible web -m ping
ping does not require any parameters. The output results of the above command are as follows:
192.168.1.2 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.1.4 | SUCCESS => { "changed": false, "ping": "pong" }
debug module
Print out information, similar to echo command on Linux. In the subsequent learning process, we will often use this command to debug the playbook we write.
There are two uses for the debug module. The following is a detailed summary of both usage.
Define the printed string through the parameter msg
Variables can be embedded in msg. For example, I first defined the following playbook.
--- - hosts: web vars: name: jellythink tasks: - name: display debug: msg="I am {{name}}"
Define the variables to be printed through the parameter var
Variables can be system variables or dynamic execution results, which are injected into variables through the keyword register. For variables, we can play this way:
--- - hosts: web vars: name: jellythink tasks: - name: display debug: var: name
For injected variables, you can play this way:
--- - hosts: web tasks: - name: register var shell: hostname register: result - name: display debug: var: result
copy module
Copy static files from the current machine to the remote node, and set reasonable file permissions. When copying a file, the copy module will first compare the checksum of the file. If it is the same, it will not be copied and the return status is OK; If it is different, it will be copied, and the return status is changed.
The general usage is as follows:
--- - hosts: server1 tasks: - name: copyDemo copy: src: /home/jelly/nameList.txt dest: /home/test1/nameList.txt
In actual work, the original file needs to be backed up during file distribution. At this time, we need to add the backup option:
--- - hosts: server1 tasks: - name: copyDemo copy: src: /home/jelly/nameList.txt dest: /home/test1/nameList.txt backup: yes
When backup: yes is added, the original files will be backed up on the target host, such as the following backup files:
nameList.txt.8648.2019-09-28@06:27:18~
template module
If you only copy static files, you can use the copy module; However, if you need to modify some contents according to the actual situation while copying, you need to use the template module.
For example, when distributing configuration files, each configuration file needs to configure different values according to some properties of the remote host. For the parts that need to be replaced, we can use the template module to replace them. The template module uses the Jinja2 template engine in Python. Here we don't need to pay too much attention to the template engine. We just need to know that the representation of variables is {}. For example, there is an HTTP The template file of conf.j2 is as follows:
Listen {{ansible_default_ipv4.address}} Port {{http_port}}
Among them, {{ansible_default_ipv4.address}} needs to change dynamically according to different hosts. Next, we can use the template module to replace variables.
--- - hosts: server1 vars: http_port: 8080 tasks: - name: Write Config File template: src: http.conf.j2 dest: /home/test1/http.conf
On the destination host, the file contents are as follows:
Listen 192.168.1.3
Port 8080
Like the copy module, the template module can also perform functions such as permission setting and file backup.
file module
They can also be used to create and delete the permissions of files and files on remote hosts.
We can use the mode parameter to modify permissions and assign numeric permissions directly (must start with 0).
--- - hosts: server1 tasks: - name: Modify Mode file: path: /home/test1/http.conf mode: 0777
We can also implement different behaviors according to different state parameters, such as creating soft links:
--- - hosts: server1 tasks: - name: Create Soft Link file: src: /home/test1/http.conf dest: /home/test1/conf state: link
You can also set state: touch to create a new file, for example:
--- - hosts: server1 tasks: - name: Create a new file file: path: /home/test1/touchfile state: touch mode: 0700
You can also set state: directory to create a new folder, for example:
--- - hosts: server1 tasks: - name: Create directory file: path: /home/test1/testDir state: directory mode: 0755
user module
The user module can manage users and add, delete and change user accounts of Linux remote nodes. For example, add users:
--- - hosts: server1 tasks: - name: Add user user: name: test3
Delete user:
--- - hosts: server1 tasks: - name: Add user user: name: test3 state: absent remove: yes
However, when using this user module, you need to pay attention to the permission problem.
shell module
Execute the command via / bin/sh on the remote node. If a command can be implemented through modules yum and copy, it is recommended not to use general command modules such as shell or command. Because the general command module will not judge the state according to the characteristics of specific operations, it will be executed again when it is not necessary to re execute.
Support <, >, |; And&
--- - hosts: server1 tasks: - name: Test shell
- Call script
--- - hosts: server1 tasks: - shell: ~/test.sh >> somelog.txt
Before executing the command, we can change the working directory, and only in the file somelog.txt Execute the command when it does not exist. In addition, you can specify bash Run command:
- hosts: server1 tasks: - shell: ~/test.sh >> somelog.txt args: chdir: ~/testDir creates: somelog.txt executable: /bin/bash
command modular Execute the command on the remote node. and shell Modules are similar but not supported<,>,|,;and&Other operations are generally similar.