Ansible installation of Ansible learning record

Source: Internet
Author: User
Tags aws cloudformation ansible modules

1. centos6.x x64 Server-side installation

# RPM-IVH http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# yum-y Install Ansible


Configuration file View:

[Email protected] ansible]# egrep-v "(#|^$)"/etc/ansible/ansible.cfg
[Defaults]
Hostfile =/etc/ansible/hosts
Library =/usr/share/ansible
Remote_tmp = $HOME/.ansible/tmp
Pattern = *
forks = 5 #默认并发5个.
Poll_interval = 15
Sudo_user = root
Transport = Smart
Remote_port = 22
Module_lang = C
Gathering = Implicit
Sudo_exe = sudo
Timeout = 10
ansible_managed = Ansible managed: {file} modified on%y-%m-%d%h:%m:%s by {UID} on {host}
Action_plugins =/usr/share/ansible_plugins/action_plugins
Callback_plugins =/usr/share/ansible_plugins/callback_plugins
Connection_plugins =/usr/share/ansible_plugins/connection_plugins
Lookup_plugins =/usr/share/ansible_plugins/lookup_plugins
Vars_plugins =/usr/share/ansible_plugins/vars_plugins
Filter_plugins =/usr/share/ansible_plugins/filter_plugins
[Paramiko_connection]
[Ssh_connection]
[Accelerate]
Accelerate_port = 5099
Accelerate_timeout = 30
Accelerate_connect_timeout = 5.0
Accelerate_daemon_timeout = 30


2. Configuring the Host Inventory

Ansible by reading the default host manifest file/etc/ansible/hosts, the file is specified in the/etc/ansible/ansible.cfg file, can be customized host, support IP, domain name, support grouping, convenient for a

Hosts or a group of identical hosts, and a default all group that represents all the hosts in the inventory.

# vi/etc/ansible/hosts

[Test]
192.168.10.191
192.168.10.192

Example: You can specify a port with a user and password:
192.168.10.191 ansible_ssh_port=2222 Ansible_ssh_user=root ansible_ssh_pass=passwd
192.168.10.191:2222


3. Free Key mode configuration

The Ansible terminal can contact each managed node based on the key authentication method. The master server generates Ssh-key and distributes it to all clients.

# ssh-keygen-t RSA
# ssh-copy-id-i ~/.ssh/id_rsa.pub [email protected]

Prompt for client password in this procedure


4. Rhel/centos/oel5. X Client Installation

Rhel/centos/oel5. X x64 defaults to python2.4,ansible to python2.4 need to install Python-simplejson installation package.

Description: python2.4/2.5 version too low will appear "MSG": "Error:ansible requires a JSON module, none found!", error.
Either upgrade python, or install Python-simplejson

[Email protected] server]# RPM-VIH python-simplejson-2.0.9-8.el5.x86_64.rpm
Warning:python-simplejson-2.0.9-8.el5.x86_64.rpm:header V3 DSA Signature:nokey, key ID 1e5e0159
Preparing ... ########################################### [100%]
1:python-simplejson ########################################### [100%]

5. ansible command Module and parameter query

(1) ansible command syntax

Grammar ansible Ansible <pattern_goes_here> M <module_name>-a <arguments>

Options:
-I device list path, you can make some dynamic path
- F Parallel Task Count
-private-key private Key Path
-M module name is not specified by default module is to use the command module
-m module clip in Path
-A parameter
-K Login Password
-k sudo password
-T output result save path
-B Background run time-out
-P Investigate Background program time
-U Execution User
-U sudo user
-L Limit Device range
-S is this user sudo does not need to enter a password


(2) View Ansible's modules and parameters

# Ansible-doc

-l list all ansible modules
-s lists the relevant instructions for the module


[Email protected] ~]# ansible-doc-l

Boundary_meter Manage Boundary meters
BZR Deploy software (or files) from BZR branches
Campfire Send a message to campfire
Capabilities Manage Linux Capabilities
Cloudformation Create a AWS cloudformation stack
Command executes a command on a remote node
Composer Dependency Manager for PHP
Copy Copies files to remote locations.
CPANM manages Perl library dependencies.
Cron Manage CRON.D and crontab entries.
Datadog_event Posts events to Datadog service


[[email protected] ~]# ansible-doc-s command
-Name:e x E c U T e s a c o m m a n d o n a r E m o t e n o d E
Action:command
ChDir # CD into this directory before running the command
Creates # a filename, when it already exists, the this step would *not* be run.
Executable # Change the shell used to execute the command. Should is an absolute path to the executable.
free_form= # The Command module takes a free form command to run. There is no parameter actually named ' Free form '. See the

examples!
Removes # a filename, when it does isn't exist, this step would *not* be run.
Warn # If command warnings is on ansible.cfg, does not warn about this particular line if set to NO/FA Lse.

6. Verify by password, add-k parameter

#提供了三种方式: A password is added to the Hosts file, one is a key-free method, one is to use the-K method to manually enter the password, often used for temporary testing.

[Email protected] ansible]# ansible all-m ping-k

SSH Password:
192.168.10.192 | Success >> {
"Changed": false,
"Ping": "Pong"
}

192.168.10.191 | Success >> {
"Changed": false,
"Ping": "Pong"
}


7. Common examples:

[[email protected] ansible]# ansible test-a ' df-h ' #这个省略了-M command Module name,-a connection parameter, can refer to line Linux command

#执行操作系统命令可以采用command, Shell,raw, and so on, where the shell can use pipelines and command parameters, script usually executes scripts and so on, can see the relevant parameter.

192.168.0.125 | success | rc=0 >>  
filesystem                     size  used Avail use% mounted on    
/dev/mapper/vg_test2-lv_root   36g  3.6g   30g  11%/   
tmpfs & nbsp;                       1004m   72K 1004m   1%/dev/shm    
/dev/sda1                      485m   39m  421m   9%/boot

192.168.0.124 | Success | Rc=0 >>
Filesystem Size used Avail use% mounted on
/dev/mapper/vg_test1-lv_root 36G 3.6G 30G 11%/
Tmpfs 1004M 72K 1004M 1%/DEV/SHM
/DEV/SDA1 485M 39M 421M 9%/boot

The execution of a single remote command, which can be operated by the relevant modules, can also be executed directly, and the command will lay the groundwork for the subsequent play-book (which can be easily understood as batch processing).

Ansible test-m yum-a "Name=dstat state=latest" #执行yum install Dstat
Ansible test-m raw-a "Rpm-qa|grep dstat" #执行rpm-qa |grep Dstat
Ansible test-m shell-a "service mysqld restart" #通过命令方式执行service mysqld restart

Single Command mode:

Ansible test-m service-a "Name=mysqld state=stopped" #通过服务模块执行service mysqld stop

Play-book Way:

-Name:stop mysqld Service #随便取名称
Service:enabled=off name=rpcbind state=stopped #模块与参数, a single command line can easily be turned into play-book mode.

This article is from the "Koumm Linux Technology blog" blog, be sure to keep this source http://koumm.blog.51cto.com/703525/1789401

Ansible installation of Ansible learning record

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.