I. Preparation of the first order
Ansible installation is complete, below learn how to use ansible.
Just like learning a programming language, first write a Hello World sample program. We also write a ansible "Hello World":
Ansible 192.168.128.83-m Ping
This command means ping 192.168.128.83 through the admin host
The "-M ping" in the command indicates that the ping module is invoked. It is known from section 00 that Ansible has many modules, including core modules and custom modules, and different modules can help us to accomplish different tasks. We will use the various modules according to the actual situation, ansible the actual task based on these modules.
If you want the above command to work correctly, you must meet the two most basic conditions:
(1) Ansible management host can connect to the managed host via SSH;
(2) Information such as the IP address of the managed host has been added to the Ansible "management list (Inventory)".
The condition (1) is because Ansible does not have to install the agent on the administrative host, but it needs to rely on SSH to connect to the managed host. The condition (2) indicates that even if the host hosting the Ansible is able to connect to the managed host via SSH, it is still necessary to add information such as the IP address of the managed host, the SSH port number, to a configuration file called Management manifest (Inventory), if the corresponding host information is Ansible does not exist in the "manifest", then ansible cannot manipulate the corresponding host. II. implementation of the first order
In section 01 we have installed the ansible, now execute this command to see the effect:
[Root@ansible-manager ~]# ansible 192.168.128.83-m ping
[WARNING]: Provided hosts list is empty, only localhost is AV Ailable. Note This implicit localhost does not match ' all '
[WARNING]: Could is not match supplied host pattern, ignoring:192. 168.128.83
The warning indicates that the host list is empty. That is, the second basic condition mentioned above is not satisfied, although we can ping the managed node from the management node, but the Inventory does not have this IP address configured, ansible can not execute correctly.
The following adds the 192.168.128.83 host and SSH authentication information to the location of the/etc/ansible/hosts (Inventory file):
192.168.128.83 ansible_port=22 ansible_user=root ansible_ssh_pass=root
Ansible_port: Used to configure the SSHD service port number on the managed host, which can be omitted if the default is 22.
Ansible_user: Used to configure the user name to be used when connecting to the managed host.
Ansible_ssh_pass: Used to configure the connection password for the managed host user.
Try again, the operation is successful:
[Root@ansible-manager ~]# ansible 192.168.128.83-m ping
192.168.128.83 | SUCCESS => {
"changed": false,
"ping": "Pong"
}
Ansible also supports adding aliases to hosts, and when the host has aliases, we can manage the corresponding host through the host's "Alias".
For example, 192.168.128.83 the alias of this host is ANSIBLE-DEMO3, then we can configure the list as follows:
Ansible-demo3 anible_host=192.168.128.83 ansible_port=22 ansible_user=root ansible_ssh_pass=root
When an alias is configured for a host, the host's IP address must be indicated using the Anible_host keyword, otherwise ansible will not be able to properly identify the host. The following uses the host's alias to manage the corresponding host:
[Root@ansible-manager ~]# ansible ansible-demo3-m ping
Ansible-demo3 | SUCCESS => {
"changed": false,
"ping": "Pong"
}
Third, configuration management node-free login
For security reasons, a key authentication method is typically used to log on to the host. SSH has no password access through certificate signing. The use of Ssh-keygen and Ssh-copy-id to achieve rapid certificate generation and public key issued.
[Root@ansible-manager ~]# Ssh-keygen ...
[Root@ansible-manager ~]# ssh-copy-id root@192.168.128.83 ...
Now that you have been able to create an SSH connection without a password, there is no need to provide the user name and password of the corresponding host when you configure the Manage manifests, so you can streamline the configuration in the manifest as follows (port defaults to 22 can also be omitted):
Ansible-demo3 anible_host=192.168.128.83
Execute again:
[Root@ansible-manager ~]# ansible ansible-demo3-m ping
Ansible-demo3 | SUCCESS => {
"changed": false,
"ping": "Pong"
}
If the 192.168.128.83 hostname is ANSIBLE-DEMO3, the configuration in the manifest can be reduced to:
Ansible-demo3
In subsequent examples, the default method of using key authentication is to connect to the managed host. Iv. Summary
In this section we learned the first ansible command to understand the prerequisites for the successful execution of the command. The following section provides a brief introduction to Ansible management commands.
Reference Documentation:
http://www.zsythink.net/archives/2481