Install Ansible
Prepare three test VMS whose IP addresses are master: 192.168.44.130 Agent1:192.168.44.132 Agent2 :192.168.44.133
Refer to website: docs.ansible.com/ansible/lat…
Download Ansible and its dependencies
Run the following command on the controller: Perform the following operations as user root. Otherwise, permissions may be insufficient.
Switch to user root first: su - root $ sudo apt update $ sudo apt install software-properties-common $ sudo add-apt-repository --yes --update Ppa :ansible/ansible $sudo apt install ansible #Copy the code
Generating a key pair:
Ansible uses SSH to connect to the Test group, and first generates a public key on the control machine.
ssh-keygen root@yge:~# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:7n7dWeR4Jw9XmbamcYxWdKVTN54O60EpMo39UBq8g0c root@yge The key's randomart image is: +---[RSA 2048]----+ | .. . .=| | +E+ o+=| | +o*.++o+| | .o+= +*o| | S . .+==o| | . .+==*| | . .. o=O.| | . . .. o .| | .o. | +----[SHA256]-----+ root@yge:~# ll /root/.ssh/ total 16 drwx------ 2 root root 4096 Aug 25 09:28 ./ drwx------ 3 root root 4096 Aug 17 05:07 .. / -rw------- 1 root root 0 Aug 17 05:07 authorized_keys -rw------- 1 root root 1679 Aug 25 09:28 id_rsa -rw-r--r-- 1 root root 390 Aug 25 09:28 id_rsa.pubCopy the code
Send the public key
It then sends the public key to Agent1 and Agent2
root@yge:/root/. SSH # ssh-copy-id [email protected] /usr/bin/ssh-copy-id: ERROR: no ID file foundCopy the code
No users found here. Ubuntu SSH is not available to root users
root@master:/root/. SSH # ssh-copy-id [email protected] /usr/bin/ssh-copy-id: ERROR: No identities foundCopy the code
This error is reported because /usr/bin/does not have ssh-copy-id because the user is not switched to root
Log in to the client node to be connected. Edit the vim /etc/ssh/sshd_config file
Vim /etc/ssh/sshd_config # PermitRootLogin yes systemctl restart SSHDCopy the code
Return to the master
root@master:~# ssh-copy-id [email protected] /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 Key (S) Remain to be installed -- if you are prompted now it is to install the new keys [email protected]'s password: [email protected]'s password: Number of key(s) added: 1 Now try logging into the machine, with: "SSH '[email protected]'" and check to make sure that only the key(s) you wanted were added. root@master:~# Ssh-copy-id [email protected] /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 Key (S) Remain to be installed -- if you are prompted now it is to install the new keys [email protected]'s password: Number of key(s) added: 1 Now try logging into the machine, with: "SSH '[email protected]'" and check to make sure that only the key(s) you wanted were added.Copy the code
Configure the Host
Edit /etc/ansible/hosts to add test information:
root@master:~# cat /etc/ansible/hosts # This is the default ansible 'hosts' file. # # It should live in /etc/ansible/hosts # # - Comments begin with the '#' character # - Blank lines are ignored # - Groups of hosts are delimited by [header] elements # - You can enter hostnames or ip addresses # - A hostname/ip can be a member of multiple groups # Ex 1: Ungrouped hosts, Specify before any group headers. ## green.example.com ## blue.example.com ## 192.168.100.1 ## 192.168.100.10 # Ex 2: A collection of hosts belonging to the 'webservers' group ## [webservers] ## alpha.example.org ## beta.example.org ## 192.168.1.100 ## 192.168.1.110 # If you have multiple hosts following a pattern you can specify # them like this: ## www[001:006].example.com # Ex 3: A collection of database servers in the 'dbservers' group ## [dbservers] ## ## db01.intranet.mydomain.net ## Db02.intranet.mydomain.net # # # # # 10.25.1.57 10.25.1.56 Here 's another example of the host ranges, this time there are no # leading 0s: ## db-[99:101]-node.example.com [test] Agent1 anSIBLE_sSH_host =192.168.44.132 Agent2 AnSIBLE_ssh_host =192.168.44.133Copy the code
Testing:
root@master:~# ansible test -m ping [DEPRECATION WARNING]: Distribution Ubuntu 18.04 on host agent2 should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior Ansible releases. A future Ansible release will Default to using the the discovered platform python for this host. See https://docs.ansible.com/ansible/2.9 /reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg. agent2 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } agent1 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python3" }, "changed": false, "ping": "pong" }Copy the code
A DEPRECATION WARNING was detected here. Enter the configuration file. Change deprecation_warnings = False to False
root@master:~# vim /etc/ansible/ansible.cfg
Copy the code
Then run it again and try again.
root@master:~# ansible test -m ping
agent2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
agent1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
Copy the code