Linux ansible installation and usage tutorial, linuxansible

Source: Internet
Author: User

Linux ansible installation and usage tutorial, linuxansible
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 # RHEL/CentOS/Fedora, You need to configure EPELapt-get install ansible # Debian/Ubuntuemerge-avt ansible # Gentoo/Funtoo

If you cannot find Ansible in the package repository of the Linux release, you can use pip to install Ansible and install Python dependent libraries such as paramiko, PyYAML, and jinja2.

pip install ansible
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:

vim  /etc/ansible/hosts[cluster1]192.168.0.1192.168.0.2192.168.0.3[web]  linuxtoy.org

Groups can also be nested:

[vps:children]  web  db

In addition, you can specify a series of consecutive hosts by using numbers and letters, such:

[] .Linuxtoy.org # is equivalent to 1.linuxtoy.org, 2.linuxtoy.org, and 3.linuxtoy.org [a: c] .linuxtoy.org #. It is equivalent to a.linuxtoy.org, B .linuxtoy.org, c.linuxtoy.org

You can also know the port number and password, such:

[port]185.207.129.[26:35] ansible_ssh_pass=123456 ansible_ssh_port=80222
Test Tool
[root@ydr1 ansible]# ansible -i hosts cluster1 -m ping -u root --ask-passSSH password:192.168.0.1 | success >> {    "changed": false,    "ping": "pong"}192.168.0.2 | success >> {    "changed": false,    "ping": "pong"}192.168.0.3 | success >> {    "changed": false,    "ping": "pong"}

The options of this command are used as follows:

-I: Specifies the inventory file and uses the hosts in the current directory.
Cluster1: runs 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.
-Ask-pass: because no ssh mutual trust is configured between machines, password is used for login.

Execute Command
[root@ydr1 ansible]# ansible -i hosts cluster1 -a "echo hello"  -u root --ask-passSSH password:10.133.47.63 | success | rc=0 >>hello10.133.47.56 | success | rc=0 >>hello10.133.47.60 | success | rc=0 >>hello
Synchronize files
[root@ydr1 ansible]# ansible -i hosts cluster1 -m copy -a 'src=/etc/hosts dest=/etc/hosts' -u root --ask-passSSH password:10.133.47.63 | success >> {    "changed": false,    "dest": "/etc/hosts",    "gid": 0,    "group": "root",    "md5sum": "9a344ec9cbe4d95f61a341d89898ac25",    "mode": "0644",    "owner": "root",    "path": "/etc/hosts",    "size": 243,    "state": "file",    "uid": 0}10.133.47.56 | success >> {    "changed": true,    "dest": "/etc/hosts",    "gid": 0,    "group": "root",    "md5sum": "9a344ec9cbe4d95f61a341d89898ac25",    "mode": "0644",    "owner": "root",    "size": 243,    "src": "/root/.ansible/tmp/ansible-tmp-1462349801.87-21441601299854/source",    "state": "file",    "uid": 0}10.133.47.60 | success >> {    "changed": true,    "dest": "/etc/hosts",    "gid": 0,    "group": "root",    "md5sum": "9a344ec9cbe4d95f61a341d89898ac25",    "mode": "0644",    "owner": "root",    "size": 243,    "src": "/root/.ansible/tmp/ansible-tmp-1462349801.89-191102139612524/source",    "state": "file",    "uid": 0}

The-a parameter indicates the module parameters.
6. Wildcards must be used in command execution.

[root@ydr1 yum.repos.d]# ansible -i /etc/ansible/hosts remotehosts -a "rm -rf /etc/yum.repos.d/*" -u root --ask-passSSH password:10.133.47.56 | success | rc=0 >>10.133.47.60 | success | rc=0 >>

You cannot delete the files under/etc/yum. repos. d/by running this command.
Use the raw module:

[root@ydr1 yum.repos.d]# ansible -i /etc/ansible/hosts remotehosts -m raw -a 'rm -rf /etc/yum.repos.d/*' -u root --ask-passSSH password:10.133.47.60 | success | rc=0 >>10.133.47.56 | success | rc=0 >>

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.