Automatic operation and maintenance tool ansible
Prepare two machines
192.168.220.140 a.com # # server
192.168.220.145 b.com # # client
1. Install # # install on a.com
Yum install-y epel-release
Yum install-y ansible
two。 Configuration
(1) ssh key configuration
First generate a key pair
Ssh-keygen-t rsa # # you can enter directly without setting the key password. This will generate a .ssh directory under the root home directory, where two files, id_rsa and id_rsa.pub, will also be generated.
Cat / root/.ssh/id_rsa.pub > > / root/.ssh/authorized_keys # # put the id_rsa.pub content in the / root/.ssh/authorized_keys of the other machine, including the local machine
Chmod / root/.ssh/authorized_keys # # configure the permissions of the client authorized_keys file
(2) ansible configuration
Vi / etc/ansible/hosts # # increased
[testhost] # # testhost host group name, custom. The following two ip are the machine ip in the group.
127.0.0.1
B.com
3. Execute command remotely
Ansible testhost-m command-a 'w' # testhost is the name of the host group, and commands can also be executed against a machine.
Error: "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren't installed!"
Solution: yum install-y libselinux-python
4. Copy a file or directory
The ansible testhost-m copy-a "src=/etc/passwd dest=/tmp/ owner=root group=root mode=0644" # # source directory will be placed under the destination directory.
5. Execute shell script remotely
Vim / tmp/test.sh # # increased
#! / bin/bash
Echo `11111111` > / tmp/1.txt
Ansible testhost-m copy-a "src=/tmp/test.sh dest=/tmp/test.sh mod=0755" # # then distribute the script to each machine
Ansible testhost-m shell-a "/ tmp/test.sh" # # batch execution of the shell script and shell module, also supports remote command execution with pipes
6. Cron
Ansible testhost-m cron-a "name='test cron' job='/bin/touch / tmp/123.txt' weekday=6" # # to delete the cron and add state=absent
7. Yum and service
Ansible testhost-m yum-a "name=httpd"
Ansible testhost-m service-a "name=httpd state=started enabled=yes"
The document uses:
Ansible-doc-l # # lists all modules
Ansible-doc cron # # View the documentation of the specified module