Ansible Playbook Detailed

Source: Internet
Author: User
Tags ansible playbook

The previous article introduced the installation configuration and example of Ansible: http://msiyuetian.blog.51cto.com/8637744/1748143

The following article mainly introduces Ansible's playbook, playbook is equivalent to the module or function written into the configuration file, and then we execute the configuration file to achieve the purpose of remote operation and maintenance automation.


First, the simple use of playbook

1. Create a File instance

1) Edit the configuration file

[Email protected] ~]# cd/etc/ansible/

[[email protected] ansible]# VIM test.yml//fixed suffix is yml, be sure to note the space

---

-Hosts:testhost

User:root

Tasks

-Name:playbook_test

Shell:touch/tmp/playbook.txt

Attention:

The hosts parameter specifies which hosts are to be used for the reference;

The user parameter specifies what users are using to log on to the remote host operation;

tasks specifies a task whose name parameter is also a description of the task, which is printed during execution.

2) Execute the configuration file

[Email protected] ansible]# Ansible-playbook test.yml

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7C/ED/wKiom1bcJWzSG0JNAAA8ByLlQjk129.png "title=" 9.png " alt= "Wkiom1bcjwzsg0jnaaa8byllqjk129.png"/>

3) Remote machine view

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7C/EB/wKioL1bcJkeCkmo1AAAkQPNmgQ4944.png "title=" 10.png "alt=" Wkiol1bcjkeckmo1aaakqpnmgq4944.png "/>


2. Create a User instance

1) Edit the configuration file

[Email protected] ansible]# vim create_user.yml

--- P>-name:create_user

  user:root

  gather_facts:false

   -User: "Msiyuetian"

  tasks:

      User:name= "{{user}}"

Attention:

The name parameter provides an overview of the functionality implemented by the playbook, which prints the value of the name variable, which can be omitted, during subsequent execution;
The gather_facts parameter specifies whether to execute the Setup module to obtain host-related information before the following task section executes, which is used when the subsequent task uses the information obtained by Setup;
The VARs parameter specifies a variable, which refers to a user variable whose value is test, and it is important to note that the value of the variable must be quoted in quotation marks;
User has set the call to the user module, name is a parameter in the user module, and the added user name calls the value of the above username.

2) Execute the configuration file

[Email protected] ansible]# Ansible-playbook create_user.yml

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7C/EB/wKioL1bcJ57z5ojVAAA0UKJyY-0368.png "title=" 11.png "alt=" Wkiol1bcj57z5ojvaaa0ukjyy-0368.png "/>

3) Remote machine view

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7C/ED/wKiom1bcJ2_xwJaGAAAY0_xQCeE993.png "title=" 12.png "alt=" Wkiom1bcj2_xwjagaaay0_xqcee993.png "/>



Second, Playbook cycle

Example: Modifying 1.txt and 2.txt file properties under the/tmp/directory

1) New Experiment file

# Touch/tmp/{1.txt,2.txt}//Operate on all hosts in the Testhost group

2) Edit the configuration file

[Email protected] ansible]# vim loop.yml

---

-hosts:testhost

  tasks:

Span style= "font-family: ' Microsoft Jas Black ', ' Microsoft Yahei ';" >   -Name:change mode for files

      file:path=/tmp/{{Item}} mode=600 owner=root group=root

      With_items:

       -1.txt

       - 2. txt

3) Execute the configuration file

[Email protected] ansible]# Ansible-playbook loop.yml

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7D/90/wKiom1bqmFmBgecLAABAaffBSFo037.png "title=" 13.png "alt=" Wkiom1bqmfmbgeclaabaaffbsfo037.png "/>

4) Remote Viewing effect

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7D/8D/wKioL1bqmUvh-IXIAAAc4XRtF1Q540.png "title=" 14.png "alt=" Wkiol1bqmuvh-ixiaaac4xrtf1q540.png "/>

Note: you can see that the permissions are 600, and the primary and the group are root.



Three, playbook condition judgment

Conditional judgments are generally used for different versions of the system, such as CentOS, Ubuntu and other systems to operate different commands.

1) Edit the configuration file

[Email protected] ansible]# vim when.yml

--- P>-hosts:testhost

  gather_facts:true

  tasks:

      Shell:touch/tmp/when.txt

      When:facter_ipaddress = = "192.168.0.110"

Note: The specified file is only created on the machine when the parameter facter_ipaddress is 192.168.0.110, which means that only the specific hosts in the Testhost group are operating, ignoring the other hosts in the group. We can see the values of each parameter by the following command:

[Email protected] ansible]# ansible testhost-m Setup

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7D/8D/wKioL1bqnQHS6WARAAAvDENnCC0263.png "title=" 15.png "alt=" Wkiol1bqnqhs6waraaavdenncc0263.png "/>

2) Execute the configuration file

[Email protected] ansible]# Ansible-playbook when.yml

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7D/8D/wKioL1bqnkmgN0lMAABDvUqEBAg967.png "title=" 16.png "alt=" Wkiol1bqnkmgn0lmaabdvuqebag967.png "/>

3) Remote Viewing effect

[Email protected] ~]# ll/tmp/

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7D/90/wKiom1bqngfAQdCWAAAyv1BETps216.png "title=" 17.png "alt=" Wkiom1bqngfaqdcwaaayv1betps216.png "/>



Iv. Playbook Handlers

After we perform the tasks, we have to perform some actions after the server has changed. For example, if we modify the configuration file of a service, we need to restart the service. Examples are as follows:

1) Edit the configuration file

[Email protected] ansible]# vim handlers.yml

---

-Name:handlers Test

Hosts:testhost

User:root

Tasks

-Name:test Copy

COPY:SRC=/ETC/PASSWD Dest=/tmp/handlers.txt

Notify: Test handlers

Handlers:

-Name: Test Handlers

Shell:echo "msiyuetian.blog.51cto.com" >>/tmp/handlers.txt

Note: only the copy module is actually executed, it will call the following handlers related operation, append content. That is, if the SRC and dest contents are the same, the shell-related commands inside the handlers will not be executed. Therefore, this comparison is appropriate after the configuration file changes, the need to restart the service operation.

2) Execute the configuration file

[Email protected] ansible]# Ansible-playbook handlers.yml

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7D/90/wKiom1bqpgOwpY1LAABD9Vta2RA227.png "title=" 18.png "alt=" Wkiom1bqpgowpy1laabd9vta2ra227.png "/>

3) Remote Viewing effect

[Email protected] ~]# Cat/tmp/handlers.txt

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7D/90/wKiom1bqpn2jmQx2AACCE8D_WZw053.png "title=" 19.png "alt=" Wkiom1bqpn2jmqx2aacce8d_wzw053.png "/>

The copy file can be viewed successfully, and the relevant commands for handlers are executed, and new information is appended.



This article is from the "M April Days" blog, please be sure to keep this source http://msiyuetian.blog.51cto.com/8637744/1752326

Ansible Playbook Detailed

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.