On the basis of the interpretation ansible, enough

Basic usage of the base ansible

  In some companies, usually as a batch operation and maintenance management of hundreds or thousands of machines, and that is what we have today in some small and medium enterprises in dozens of machines batch management toolansible

  ansible rpm package installation source is epel source, of course, write the source code can be deciphered based installation.

  yum install ansibleA small command.

  Some of the options on ansible

  --version 显示版本

  -m module 指定模块,默认为command

  -v 详细过程 –vv -vvv更详细

  --list-hosts 显示主机列表,可简写 --list

  -k, --ask-pass 提示输入ssh连接密码,默认Key验证

  -K, --ask-become-pass 提示输入sudo时的口令

  -C, --check 检查,并不执行

  -T, --timeout=TIMEOUT 执行命令的超时时间,默认10s

  -u, --user=REMOTE_USER 执行远程执行的用户

  -b, --become 代替旧版的sudo 切换

  --become-user=USERNAME 指定sudo的runas用户,默认为root

  ansible --versionYou can view more information about the installed version and the like. The main is to confirm installation.

  The next step is to edit the list of hosts ansible management. All managed machines are stored in /etc/ansible/hoststhis directory, in the following out of the way to find a place to put the host IP packet needs to be managed type can be. E.g

  [an***vs]

  192.168.37.103

  192.168.37.23

  [appsrvs]

  192.168.37.3

  But the machine can not be managed passwords are the same, batch management and ansible time only for the opportunity to enter a password, if not managed machine login password is not the other side to manage, so we need to be based key to login.

  ssh-keygenA small command

  ssh-copy-id IPYou can put your public key to send the ip to each other. Can be used ssh IPto test some other remote connection is also required password yo, after the completion of this step or manage remote connections again when you do not need each other's password.

  According to individual needs can set your own preferences, I used to put into the default module command shell, it is to vim /etc/ansible/ansible.cfgfind a directory called " module_name" remove the comment in front of the # ' module_name = shell' on the line, because the command does not recognize some special symbols , shell modules are take-all. Back to the fact ansible basically learned all, but although enough but not very professional! ! !

  Green represents the state in which the results of the successful implementation of the operation and do not need to be changed; ××× indicates a successful execution and make changes to the target host; we want to see in the red failed;

  Then let us try to execute a command to its format it.

  ansible all -a 'ls /data'All managed machine wherein all represented, including all packets inside. Because the module using a shell and we just have to shell module as the default module, so we do not write -m shell(Note that if a user needs to write other modules -madd other modules, we will introduce later) -arepresents the our normal linux commands.

Script: Run the script on the server on the remote host ansible

  ansible all -m script -a '/root/test.sh'Test.sh this script is executed.

Copy: Copy the files from the master to the remote host

  ansible all -m copy -a 'src=/data/f1 dest=/data/ owner=root mode=600 backup=yes'Which is the master file src source dest placed in the position of the controlled machine owner is to change the file owner, mode is to change the file permissions, because if there is a default target is covered, so the backup is a backup of the target file .

Fetch: extract files from a remote host to the host, copy the contrary, is not currently supported directory

  ansible all -m fetch -a 'src=/data/f1 dest=/data/The grab is controlled machine / data / file F1 to the master / data / lower. (Currently only supports crawl file does not support folders yo)

File: set file attributes

  ansible all -m file -a ' src=/data/f1 dest=/data/fi-link state=link' | ansible all -m file -a 'path= /data/f1 owner=root mode=600'One of the most basic is to change file permissions and owners, in addition to the file can also file snake soft connection, state is the need for more and more of the state.

unarchive: unpack unzip, there are two ways:

  1, the compressed packet transmitted to the host on the remote host ansible after local decompression disposed copy = yes.

  For 2, decompress compressed on a remote host to the specified path, set copy = n

  Common parameters:

    copy: The default is yes, when the copy = yes, copy the files are copied from the ansible host to the remote host, if set to copy = no, will look src source file on the remote host

    src: source path, the path may be ansible host, may be a path on the remote host, if the remote
path on the host, the need to set copy = no

    Target path on the remote host: dest

    mode: Set file permissions decompressed

  ansible all -m unarchive -a 'src=https://example.com/example.zip dest=/data copy=no'This example is found in the host controlled compressed https://example.com/example.zip and extract to / data / directory.

Yum: Management Pack

  ansible srv -m yum -a ‘name=httpd state=present’ installation

  ansible srv -m yum -a ‘name=httpd state=absent’ delete

Cron: Scheduled Tasks

  Support time: minute, hour, day, month, weekday

  ansibleall -m cron -a "minute=*/5 job=/usr/sbin/ntodate172.16.0.1&> / dev / null 'name = Synctime "is to create a task, every 5 minutes to the log garbage thrown into the trash.

  ansible all -m cron -a 'state=absent name=Synctime'It is to delete the task just created.

Service: Management Services

  ansible all -m service -a 'name=httpd state=started enabled=yes'Httpd this service is to open and set boot.

  ansible all -m service -a 'name=httpd state=stopped'HTTP is to close this service.

User: User Management

  ansible all -m user -a 'name=user system=yes home=/data/user'It is to create a system user and the user's home directory in the / data / down.

  ansible all -m user -a 'name=user state-absent remove=yes'It is to delete the user and the user's home directory delete attached together.

Group: Management Group

  ansible all -m group -a 'name=testgroup syste=yes'It is to create a system group

  ansible all -m group -a 'name=testgroup state=absent'It is to remove the group testgroup

  These modules is almost, if these can basically grasp ansible used maxed out, of course, is not so every minute of it ?????? meet the general work is certainly every problem.

  OK, bye!

Guess you like

Origin blog.51cto.com/14322562/2422302