Ansible learning records

Source: Internet
Author: User
Tags vps

Ansible Problems


Developed based on paramiko. What is this paramiko? It is a python-Only SSH protocol library. Therefore, fabric and ansible do not need to install client/agents on a remote host because they communicate with remote hosts Based on SSH.


The inventory file is used to define the host to be managed. The default location is/etc/ansible/hosts.


Ansible is a python package and is a complete unpack and play software. The only requirement on the client is that SSH has Python and python is installed with the python-simplejson package.


Ansible Installation Process

Configure the client group and host name in the server python2.6 hosts file

Client SSH authorized_keys


Key must be generated

Use paramiko to Generate Keys for Distribution


S = paramiko. sshclient ()

S. load_system_host_keys () # load the local know_hosts File

S. set_missing_host_key_policy (paramiko. autoaddpolicy () # if not added

Pkey_file = '/home/test/. Ssh/id_rsa'

Key = paramiko. rsakey. from_private_key_file (pkey_file)

S. Connect (host, 22, user, pkey = key, timeout = 5)

Stdin,stdout,stderrcmds.exe c_command (CMD) # Run the command

Stdout. Read ()

S. Close ()


How to define the hosts file


Commonly used ansible command-playbook

Ansible-I/etc/ansible/hosts all-a 'who'

Ansible-I/etc/ansible/hosts all-M Ping

Ansible all-m raw-a 'W'


The options of this command are used as follows:

-I: Specifies the inventory file and uses the hosts in the current directory.

ALL: run on all hosts defined by hosts. You can also specify the group name or mode.

-M: Specifies the module used. We use the ansible built-in Ping module to check whether remote machines can be managed normally.

-U: the user of the remote machine.


Install ansible


Ansible can be installed on Linux, BSD, Mac OS X, and other platforms. The minimum Python version requirement is 2.6. For common Linux distributions, you can install ansible through its own Package Manager:


Yum install ansible

Apt-Get install ansible

Paramiko, pyyaml, jinja2, and other Python dependent libraries.


Prepare inventory


The inventory file is used to define the host you want to manage. Its default location is/etc/ansible/hosts. If it is not saved in the default location, you can also specify it using the-I option.


The managed machine can be specified through its IP address or domain name. Ungrouped machines must be retained at the top of Hosts. You can use [] to specify the group, for example:


Now, run the following command to check whether ansible works properly:


Ansible-I hosts all-M Ping-u WWW

The options of this command are used as follows:


-I: Specifies the inventory file and uses the hosts in the current directory.

ALL: run on all hosts defined by hosts. You can also specify the group name or mode.

-M: Specifies the module used. We use the ansible built-in Ping module to check whether remote machines can be managed normally.

-U: the user of the remote machine.


If the following result is returned:


Linuxtoy.org | Success >> {

"Changed": false,

"Ping": "PONG"

}

It means everything is normal.


Next let's look at the uptime of the remote machine:


Ansible all-A 'uptime'

This will output:


Linuxtoy.org | Success | rc = 0>

11:23:16 up 177 days, 0 users, load average: 0.55, 0.45, 0.39

-M is omitted here. ansible uses the command module by default.-A specifies the parameters of the module, that is, the uptime command is executed.


Use ad-hoc to manage simple tasks


The execution of ad-hoc is similar to the execution of a single-line command in Linux. It is very convenient to quickly complete simple tasks. For example, if the python on the Management end is 2.4, the python-simplejson package is required. Run the following command to install it on all centos hosts:


Ansible all-m raw-A 'yum-y install Python-simplejson'

It's worth looking at ansible's modules. You'll understand what it can do. Creating users and groups, installing software packages, distributing configuration files, and managing services are all different. You can use ansible-doc to query module documents under the command line, such:


Ansible-Doc raw

Use playbook to manage complex tasks


Ad-hoc command-temporary Command Execution


For complex tasks that require repeated execution, you can define playbook. Playbook is really powerful for ansible. It allows you to use variables, conditions, loops, templates, and roles and include commands to reuse existing content. Let's look at a simple example. This example creates a new user on a remote machine:


---

-Name: create user

Hosts: VPS

User: Root

Gather_facts: false


Vars:

-User: "Toy"


Tasks:

-Name: Create {user} on VPS

User: Name = "{user }}"

First, we specify a name for the playbook. Then, we use hosts to allow the playbook to act only on the VPs group. The User specifies to execute the playbook as the root account. ansible also supports sudo execution for common users; gather_facts is used to collect information about the remote machine, which can be used in Playbook later in the form of variables; vars defines variables, which can also be separately placed in files; tasks specifies the task to be executed.


To execute a playbook, you can enter:


Ansible-playbook user. yml

The execution result is:


Play [create user] ********************************* *************************


Task: [Create toy on VPs] ******************************** *******************

Changed: [linuxtoy.org]


Play recap ************************************** ******************************

Linuxtoy.org: OK = 1 changed = 1 unreachable = 0 failed = 0


This article is from the "muzinan technology blog" blog, please be sure to keep this source http://muzinan110.blog.51cto.com/684213/1559799

Ansible learning records

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.