代码改变世界

5 - Ansible playbook

2021-08-08 10:24  WayneWei  阅读(36)  评论(0编辑  收藏  举报
  1. Offical introduction to playbook
    You can get official reference of playbook from below url:
    https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html#playbook-syntax
  2. Key element of playbook
    Task The management activities we are going to perform for remote servers
    Variables Variables we can use in the playbook YAML file, including user defined variables, built-in variables, and also variables defined in Inventory file
    Template Template file with a syntax, like some configuration files etc.
    Handlers Handlers working with notify, while the activities match a condition, then it's trigger the handler to perform some specific action
    Tags Mark the task in playbook with a tag, while we execute the playbook, we can specify which task to be executed by specify the tag name, like:
    ansible-playbook hosts.yml --tags="only"










  3. Ansible playbook syntax
    Below is an example of the playbook should be looks like:
    ---
    - hosts: webservers
      remote_user: root
      vars:
        filename: readme.txt
    
      tasks:
        - name: create readme file
          file: name=/tmp/{{filename}} state=touch
        - name: create new user
          user: name=wayne03 state=present shell=/bin/bash

    By execute above playbook, save above content to file named test.yml, you can run below command:

    ansible-playbook test.yml

    Then switch to shell-node1, verify if the readme.txt and user wayne03 been created or not: