Ansible Installation and use

Source: Internet
Author: User
Tags regular expression ssh ansible playbook example ansible playbook
Any server can be the master terminal
1, install Epel Source:# RPM-IUVH http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm
2. Install the package:# yum Install Ansible
3, set up SSH password-free login:# # Generate public/private key # Ssh-keygen-t Rsa-p ' # # Write a trusted file (distribute/root/.ssh/id_rsa_storm1.pub to other servers and execute the following on all servers): # CAT/ROOT/.SSH/ID_ Rsa_storm1.pub >>/root/.ssh/authorized_keys # chmod 600/root/.ssh/authorized_keys
4, Configuration ansible:# vim/etc/ansible/ansible.cfg private_key_file =/root/.ssh/id_rsa_storm1 #指定密钥位置 # vim/etc/ansible/hosts [storm_clust ER] #指定组名
10.223.55.100 10.223.55.101 linuxtoy.org #指定执行任务的主机, can be IP, hostname, support regular expression
For example: [1:3].linuxtoy.org # is equivalent to 1.linuxtoy.org, 2.linuxtoy.org, 3.linuxtoy.org [A:c].linuxtoy.org # equivalent to a.linuxtoy.org, b.linuxtoy.org, c.linuxtoy.org
5, command use:# ansible-i hosts all-m ping-u www The role of this command option is:
-I: Specify the inventory file, using the hosts in the current directory
-All: For all hosts defined by the hosts, you can also specify the group name or mode
-M: Specify the module used, we use the Ansible built-in ping module to check whether the remote machine can be managed properly
-U: Specify the user of the remote machine
If you return the following result: linuxtoy.org | Success >> {"Changed": false, "ping": "Pong"} means everything is OK.
Let's look at the uptime of the remote machine: # ansible vps-a ' uptime ' which will output: linuxtoy.org | Success | Rc=0 >> 11:23:16 up 177 days, 21:19, 0 users, load average:0.55, 0.45, 0.39 here we omit the-m,ansible default use command module; Specifies the parameters of the module, which is the Execute uptime command.
Execute installation package: ansible all-m raw-a ' yum-y install Python-simplejson '
Simple Ansible Playbook Example: Create user toy: # vim user.yml-name:create user hosts:host user:root Gather_facts:false v ARS:-User: "Toy" tasks:-name:create {{User}} user:name= "{{user}}" # Ansible-playbook USER.YML
※ Basic command: # # to see some basic information about a remote host # ansible STORM_CLUSTER-M setup
# # used to test the running status of the remote host # ansible Storm_cluster-m Ping
# # Remote File Information View # ansible storm_cluster-m command-a "ls–al/tmp/resolv.conf"
# # Copy the local file "/etc/ansible/ansible.cfg" to the remote server # ansible storm_cluster-m copy-a "Src=/etc/ansible/ansible.cfg dest=/tmp/ Ansible.cfg owner=root group=root mode=0644 "
# # EXECUTE command on remote host # ansible storm_cluster-m command-a "uptime"
# # Remote Execute Shell Script # ansible storm_cluster-m shell-a "/tmp/rocketzhang_test.sh"
More modules can be consulted: #ansible-doc–l

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.