Automatic operation and maintenance Ansible use (follow the steps in the document to be successful).
The document sits and installs Ansible separately, and uses Ansible to control two machines.
Special note: Why do you need to make an Ansible document after doing pssh, because Ansible is flexible, and I will list its advantages below.
1. You can enter a password to control without having to use a secret key.
2. Even if the hosts list is uninstalled, some machines can be controlled separately, and machines can be classified.
#####When following the document deployment, be sure to read every word carefully. Be sure to do it (every word is important)
. When you are prompted to use the ansible redis -m command module to execute some commands, it may fail. Can be changed to use the shell module.
When I did the experiment, I used two methods of remote control:
1. Key-free method
2. Password input method
#Private self reprint, please contact the blogger, otherwise the copyright must be investigated. There is a WeChat
system environment below :
IP | Node name |
---|---|
192.168.182.150 | admin |
192.168.182.150 | client-1 |
192.168.182.150 | client-2 |
1. Set three node hostnames
admin node execution
[root@localhost ~]# hostnamectl set-hostname admin
[root@localhost ~]# su
client-1 node execution
[root@localhost ~]# hostnamectl set-hostname client-1
[root@localhost ~]# su
client-2 node execution
[root@localhost ~]# hostnamectl set-hostname client-2
[root@localhost ~]# su
2. Install Ansible
admin node execution
[root@admin ~]# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -y
[root@admin ~]# yum install ansible -y
3. Configure the ansible
admin node to execute.
Modify the size of the number in line 20. By default, only 5 machines can be connected.
[root@admin ansible]# vim /etc/ansible/ansible.cfg
20行 forks = 20
4. Modify the ansible-hosts file
admin node execution
[root@admin ansible]# vim /etc/ansible/hosts
[web] #这样就定义了一个组web是组名称可以自定义,可以根据组名称来具体控制哪些机器
192.168.182.150
192.168.182.151
[redis] #这样就定义了一个组redis是组名称可以自定义,可以根据组名称来具体控制哪些机器
192.168.182.152
5. Configure the secret key to connect to the
admin node for execution
[root@admin ansible]# ssh-keygen
##直接回车
[root@admin ansible]# ssh-copy-id -i root@admin
##会有提示输入密码
[root@admin ansible]# ssh-copy-id -i [email protected]
##会有提示输入密码
[root@admin ansible]# ssh-copy-id -i [email protected]
##会有提示输入密码
6, view the defined web group host disk space
admin node execution
[root@admin ansible]# ansible web -m command -a 'df -h'
192.168.182.150 | CHANGED | rc=0 >>
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 50G 1.4G 49G 3% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 124K 489M 1% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/mapper/centos-home 45G 33M 45G 1% /home
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 98M 0 98M 0% /run/user/0
192.168.182.151 | CHANGED | rc=0 >>
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 50G 1019M 49G 2% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/mapper/centos-home 45G 33M 45G 1% /home
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 98M 0 98M 0% /run/user/0
6. View the defined redis group host disk space
admin node execution
[root@admin ansible]# ansible redis -m command -a 'df -h'
192.168.182.152 | CHANGED | rc=0 >>
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 17G 1.1G 16G 7% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 1014M 125M 890M 13% /boot
tmpfs 98M 0 98M 0% /run/user/0
7. View all group host disk space
admin node execution
[root@admin ansible]# ansible all -m command -a 'df -h'
192.168.182.152 | CHANGED | rc=0 >>
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 17G 1.1G 16G 7% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 1014M 125M 890M 13% /boot
tmpfs 98M 0 98M 0% /run/user/0
192.168.182.151 | CHANGED | rc=0 >>
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 50G 1019M 49G 2% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/mapper/centos-home 45G 33M 45G 1% /home
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 98M 0 98M 0% /run/user/0
192.168.182.150 | CHANGED | rc=0 >>
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 50G 1.4G 49G 3% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 124K 489M 1% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/mapper/centos-home 45G 33M 45G 1% /home
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 98M 0 98M 0% /run/user/0
8. Use regular expressions to control the host (the host address must be written in the hosts file or the execution fails)
admin node execution
[root@admin ansible]# ansible 192* -m command -a 'df -h'
192.168.182.151 | CHANGED | rc=0 >>
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 50G 1019M 49G 2% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/mapper/centos-home 45G 33M 45G 1% /home
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 98M 0 98M 0% /run/user/0
192.168.182.152 | CHANGED | rc=0 >>
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 17G 1.1G 16G 7% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 1014M 125M 890M 13% /boot
tmpfs 98M 0 98M 0% /run/user/0
192.168.182.150 | CHANGED | rc=0 >>
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 50G 1.4G 49G 3% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 124K 489M 1% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/mapper/centos-home 45G 33M 45G 1% /home
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 98M 0 98M 0% /run/user/0
9. Check which host
admin node executes
[root@admin ansible]# ansible all --list-hosts
hosts (3):
192.168.182.150
192.168.182.151
192.168.182.152
10. You can also specify a custom file (in the case that the default hosts file is not applicable)
admin node execution
[root@admin ansible]# ansible -i /etc/ansible/zhiding redis -m command -a 'df -h'
11. You can also execute remote command
admin node execution without the secret key
[root@admin ansible]# ansible -K web -m command -a 'df -h'
BECOME password: ##这里是让你输入密码(有一个前提你控制的所有主机密码应该是一致的)
192.168.182.151 | CHANGED | rc=0 >>
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 50G 1019M 49G 2% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/mapper/centos-home 45G 33M 45G 1% /home
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 98M 0 98M 0% /run/user/0
192.168.182.150 | CHANGED | rc=0 >>
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 50G 1.4G 49G 3% /
devtmpfs 478M 0 478M 0% /dev
tmpfs 489M 124K 489M 1% /dev/shm
tmpfs 489M 6.8M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/mapper/centos-home 45G 33M 45G 1% /home
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 98M 0 98M 0% /run/user/0
12. You can also write a script to define the command to be executed and which host to execute the
admin node on
[root@admin ansible]# vim test.yaml
- hosts: redis
remote_user: root
tasks:
- name: yum apache
shell: yum -y install httpd
13. Specify the script file to execute the
admin node in batch using Ansible
[root@admin ansible]# ansible-playbook test.yaml
PLAY [redis] ********************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************
ok: [192.168.182.152]
TASK [yum apache] ***************************************************************************************************************
[WARNING]: Consider using the yum module rather than running 'yum'. If you need to use command because yum is insufficient you
can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [192.168.182.152]
PLAY RECAP **********************************************************************************************************************
192.168.182.152 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
14. In the client section, check whether the httpd
client-2 node is installed.
[root@client-2 ~]# rpm -qa|grep httpd
httpd-tools-2.4.6-97.el7.centos.x86_64
httpd-2.4.6-97.el7.centos.x86_64
wx:a1362623821