Ansible playbooks Introduction and use of two

Source: Internet
Author: User
Tags egrep

Directory

    • Handlers
      • Playbook Case 2 handlers
      • VARs variable
        • Setup Facts Variable Usage
        • Case
        • Defining variables in Inventory
        • Case
    • Condition test
      • When statement
      • Case
Handlers

Follow up on an article Ansible playbooks introduction and use a continuation note

Used to take certain actions when the resources being followed are changed.

notifyThis action can be used to be punished at the end of each play, which avoids having to perform the specified action every time a change occurs, instead of doing the specified action once all changes have been completed. The actions listed in notify become handler.

For example:

- name: template configuration file  template: src=template.j2 dest=/etc/foo.conf  notify:  - restart memcached  - restart apache

Handler are task lists, and these tasks are not inherently different from the aforementioned tasks.

handlers:- name: restart memcached  service: name=memcached state=restarted- name: restart apache  service: name=httpd state=restarted
Playbook Case 2 handlers

Application Scenarios

Installing the HTTPD service in the Webservs group, the default boot httpd is listening on the 80 port

Steps

    1. Create a playbook that configures the installation of the HTTPD service, and configure related operations such as boot startup
    2. Executive Httpd.yml
    3. Modify the port in configuration file httpd.conf to 8080
    4. Execute the httpd playbook file again

First create the Httpd.yml file

[[email protected] ansible]# pwd/etc/ansible[[email protected] ansible]# cat httpd.yml - hosts: webservs  remote_user: root  tasks:  - name: install httpd packge    yum: name=httpd state=present  - name: configuration file for httpd    copy: src=/etc/ansible/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf  - name: start httpd service    service: name=httpd enabled=true state=started

Create a configuration file directory and copy the httpd.conf

[[email protected] ansible]# pwd/etc/ansible[[email protected] ansible]# mkdir conf[[email protected] ansible]# cp /etc/httpd/conf/httpd.conf conf/[[email protected] ansible]# vim conf/httpd.conf

Execute the playbook of HTTPD.YUML

[[email protected] ansible]# ansible-playbook httpd.yml PLAY [webservs] ***********************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************ok: [10.0.0.65]TASK [install httpd packge] ***********************************************************************************************changed: [10.0.0.65]TASK [configuration file for httpd] ***************************************************************************************ok: [10.0.0.65]TASK [start httpd service] ************************************************************************************************changed: [10.0.0.65]PLAY RECAP ****************************************************************************************************************10.0.0.65                  : ok=4    changed=2    unreachable=0    failed=0

Verify:

[[email protected] ansible]# ansible webservs -a ‘rpm -qa httpd‘10.0.0.65 | CHANGED | rc=0 >>httpd-2.4.6-80.el7.centos.1.x86_64[[email protected] ansible]# ansible webservs -m shell -a ‘netstat -lntup | grep 80‘10.0.0.65 | CHANGED | rc=0 >>tcp6       0      0 :::80                   :::*                    LISTEN      31354/httpd

Modify the port in the httpd.conf configuration file on the Ansible service side to 8080, and then re-execute

[[email protected] ansible]# egrep ' ^listen ' conf/httpd.conf Listen 8080[[email protected] ansible]# Ansible-playbook httpd.yml PLAY [Webservs] *********************************************************************** TASK [Gathering Facts] ****************************************************** OK: [10.0.0.65]task [Install httpd packge] ************************** OK: [10.0.0.65]task [Configuration file for HTTPD] ***************************************************************************************changed: [ 10.0.0.65]task [start httpd service] ***************************************************************************** OK: [10.0.0.65]play RECAP ******************************************************************** 10.0.0.65:ok=4 changed=1 UnreachabLe=0 failed=0 [[email protected] ansible]# ansible webservs-m shell-a ' Netstat-lntup | grep httpd ' 10.0.0.65 | CHANGED | Rc=0 >>TCP6 0 0::: +:::* LISTEN 31354/httpd [[Email&nbs P;protected] ansible]# ansible webservs-m shell-a ' egrep ' ^listen '/etc/httpd/conf/httpd.conf ' 10.0.0.65 | CHANGED | Rc=0 >>listen 8080

Finally, although the port in the remote host's configuration file has been modified, the actual listening port has not changed, indicating that httpd did not restart

At this point, you need to use handlers, to listen when there are similar to the configuration file operation changes, you need to restart such operations,

Modify the content in Httpd.yml below

[[email protected] ansible]# cat httpd.yml - hosts: webservs  remote_user: root  tasks:  - name: install httpd packge    yum: name=httpd state=present  - name: configuration file for httpd    copy: src=/etc/ansible/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf    notify:     - restart httpd  - name: start httpd service    service: name=httpd enabled=true state=started  handlers:  - name: restart httpd    service: name=httpd state=restarted

Change the port in httpd.conf again to 808 and then execute again

[[email protected] ansible]# egrep ' ^listen ' conf/httpd.conf Listen 808[[email protected] ansible]# Ansible-playbook httpd.yml PLAY [Webservs] *********************************************************************** TASK [Gathering Facts] ****************************************************** OK: [10.0.0.65]task [Install httpd packge] ************************** OK: [10.0.0.65]task [Configuration file for HTTPD] ***************************************************************************************changed: [ 10.0.0.65]task [start httpd service] ***************************************************************************** OK: [10.0.0.65]running HANDLER [Restart httpd] ************************************************* Changed: [10.0.0.65]play RECAP *****************************10.0.0.65:ok=5 CH anged=2 unreachable=0 failed=0 [[email protected] ansible]# ansible webservs-m shell-a ' Netstat-lntup | grep httpd ' 10.0.0.65 | CHANGED | Rc=0 >>TCP6 0 0::: 808:::* LISTEN 32212/httpd

Finally, you can see that the remote host's HTTPD service listener's port has become 808.

VARs variable

Using variables in playbook, you can directly define variables directly in the playbook, or you can define variables in other templates and drink them in the playbook file.

In the following example, add the VARs variable to the file in Httpd.yml

[[email protected] ansible]# cat httpd.yml - hosts: webservs  remote_user: root  vars:  - package: httpd  - service: httpd  tasks:  - name: install httpd package    yum: name={{ package }} state=present  - name: configuration file for httpd    copy: src=/etc/ansible/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf    notify:     - restart httpd  - name: start httpd service    service: name={{ service }} enabled=true state=started  handlers:  - name: restart httpd    service: name={{ service }} state=restarted

As can be seen from the above definition, the reference to the variable is through {{ }} two curly braces.
Take a look at the next step, look at the effect

[[email protected] ansible]# ansible-playbook httpd.yml PLAY [webservs] ***********************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************ok: [10.0.0.65]TASK [install httpd package] **********************************************************************************************ok: [10.0.0.65]TASK [configuration file for httpd] ***************************************************************************************ok: [10.0.0.65]TASK [start httpd service] ************************************************************************************************ok: [10.0.0.65]PLAY RECAP ****************************************************************************************************************10.0.0.65                  : ok=4    changed=0    unreachable=0    

You can see that all is OK, indicating that the variable reference was successful.

Setup Facts Variable Usage

Playbook can also refer directly to variables obtained from the remote host information in facts to use

Case

First, check out

[[email protected] ansible]# ansible webservs -m setup | head 10.0.0.65 | SUCCESS => {    "ansible_facts": {        "ansible_all_ipv4_addresses": [            "10.0.0.65"        ],         "ansible_all_ipv6_addresses": [            "fe80::20c:29ff:fe07:47f6"        ],         "ansible_apparmor": {            "status": "disabled"

Here we use the variable: ansible_all_ipv4_addresses to invoke the use of the

  [[email protected] ansible]# cat Test.yml-hosts:webservs remote_user:root tasks:-name:copy file Copy:content= "{{ansible_all_ipv4_addresses}}" dest=/tmp/vars.ans[[email protected] ansible]# Ansible-playbook test.yml PLAY [Webservs] ************************************************************************ TASK [Gathering Facts] ******************************************************* OK: [10.0.0.65]task [Copy file] ************************************* Changed: [10.0.0.65]play RECAP *************                  10.0.0.65 : ok=2 changed=1 unreachable=0 failed=0 [[email protected] ansible]# ansible webservs-a ' Cat/tmp/var S.ans ' 10.0.0.65 | CHANGED | Rc=0 >>["10.0.0.65"]  

From the results of the last output above, the invocation of the variable is successful.

Defining variables in Inventory

You can also define variables in inventory and then reference them in playbook

Case

Modify Inventory File hosts

[[email protected] ansible]# cat hosts# This is the default ansible ‘hosts‘ file.## It should live in /etc/ansible/hosts##   - Comments begin with the ‘#‘ character#   - Blank lines are ignored#   - Groups of hosts are delimited by [header] elements#   - You can enter hostnames or ip addresses#   - A hostname/ip can be a member of multiple groups[webservs]10.0.0.65 testvars="10.0.0.65"[dbservs]10.0.0.66 testvars="10.0.0.66"

The above defines a testvars variable for a host

The following is a reference to modifying the playbook file

[[email protected] ansible]# cat Test.yml-hosts:webservs, Dbservs remote_user:root tasks:-name:copy file Copy:content= "{{ansible_all_ipv4_addresses}}\n{{Testvars}}" dest=/tmp/vars.ans[[email protected] ansible]# Ansible-playbook test.yml PLAY [Webservs, Dbservs] **************************************************************** TASK [Gathering Facts] ******************************************************** OK: [10.0.0.65]ok: [10.0.0.66]task [Copy file] ************************ Changed: [10.0.0.66]changed: [ 10.0.0.65]play RECAP *******************************************************************************************                  10.0.0.65:ok=2 changed=1 unreachable=0 failed=0 10.0.0.66 : ok=2 changed=1 unreachable=0 failed=0 [[Email proTected] ansible]# ansible all-a ' Cat/tmp/vars.ans ' 10.0.0.66 | CHANGED | Rc=0 >>[u ' 10.0.0.66 ']10.0.0.6610.0.0.65 | CHANGED | Rc=0 >>[u ' 10.0.0.65 ']10.0.0.65

As you can see from the last output above, the reference succeeds.

Condition test

A conditional test is used if a task is to be executed or not based on the result of a variable, facts, or previous task execution.

When statement

You can use conditional testing when you add a when clause after a task, and when statements support JINJA2 expression syntax, for example:

tasks:- name: "shutdown Debian flavored systems"  command: /sbin/shutdown -h now  when: ansible_os_family == "Debian"

The When statement can also use too many filter jinja2, such as to ignore the previous statement's error and run the statement specified later based on its result (failed or sucess), using a form similar to the following:

tasks:- command: /bin/false  register: result  ignore_errors: True- command: /bin/something  when: result | failed- command: /bin/something_else  when: result | success- command: /bin/still/something_else  when: result | skipped

The variables defined in facts or playbook can also be used in the When statement.

Case

Ansible playbooks Introduction and use of two

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.