Catalog
1.Playbook's initial understanding of scripts
1. What is a playbook? Playbook is translated as a "play". The playbook consists of
play: Defines the role task of the host: Defines the specific task playbook: consists of one or more plays that can contain multiple task tasks
Simple understanding: use different modules to accomplish one thing
2. Advantages of Playbook
1. More functionality than ad-hoc 2. Better control over execution order and dependencies 3. More intuitive syntax 4.ad-hoc cannot be persisted, playbook can be persisted
3. The playbook configuration syntax is described by the yaml syntax and the extension is yaml
- indent
- YAML uses a fixed indentation style to represent hierarchical structure, where each indentation consists of two spaces and tabs cannot be used
- colon
- All colons except those ending with a colon must have spaces after them.
- Short Horizontal Line
- Represents a list item, with a short bar and a space.
- Multiple items use the same indentation level as the same list.
#playbook example [root@manager ~]# cat f1.yml --- - hosts: all remote_user: root vars: file_name: xuliangwei tasks: - name: Create New File file: name=/tmp/{{ file_name }} state=touch #inspect ansible-playbook --syntax-check httpd.yml #playbook execution [root@manager ~]# ansible-playbook f1.yml PLAY [all] ******************************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************** ok: [10.0.0.30] TASK [Using variables] ******************************************************************************************************************************* changed: [10.0.0.30] PLAY RECAP ******************************************************************************************************************************** 10.0.0.30 : ok=2 changed=1 unreachable=0 failed=0
Playbook execution results return color status
Red: Information indicating a task execution failure or reminder
Yellow: indicates that the remote host state has been executed and changed
Green: Successful execution
2.Playbook variable use
Playbook defines variables in three ways
1) Define variable assignment in playbook's yaml file
2) --extra-vars execution parameters assigned to variables
3) Define variables in the file
1. Define variable assignment in playbook's yaml file
#Definition in playbook [root@manager ~]# cat f2.yml - hosts: all vars: #Define Variables file_name: bgx_yaml_vars tasks: - name: # {{file_name}} refers to the variable defined above file: path=/tmp/{{ file_name }} state=touch #playbook, create bgx_yaml_vars file in / tmp directory [root@manager ~]# ansible-playbook f1.yml
2. --extra-vars execution parameters assign to variables
#Reference variable in playbook [root@manager ~]# cat f3.yml - hosts: all tasks: - name: Create New File file: path=/tmp/{{ file_name }} state=touch #The playbook executes by passing in the parameter of the file_name variable, creating the bgx_extra-vars file in the / tmp directory [root@manager ~]# ansible-playbook f2.yml --extra-vars "file_name=bgx_extra-vars"
3) Define a variable in the file: you can define it in the / etc/ansible/hosts host group, then use palybook to schedule the variable
#Define variables in files [root@manager ~]# cat /etc/ansible/hosts [nfs] 10.0.0.20 [nfs:vars] file_name=bgx_filename #Call the variable in Playbook [root@manager ~]# cat f4.yml --- - hosts: all tasks: - name: Create New File file: path=/tmp/{{ file_name }} state=touch #playbook, create bgx_filename file in / tmp directory
If the variables defined are duplicated and cause conflicts, the priority is as follows:
1. External arguments to extra-vars have the highest priority [all executing hosts are valid]
2. Define priority in the yml file Secondly [All executing hosts are valid]
3. The variable defined in the hosts file has the lowest priority [the current host group definition will take effect]
3.Playbook variable registration
1) Register variables: The register keyword stores the output of the specified command into a custom variable
[root@manager ~]# cat f5.yml --- - hosts: all tasks: - name: shell: netstat -lntp register: System_Status - name: Get System Status debug: msg={{System_Status.stdout_lines}} #playbook execution results [root@manager ~]# ansible-playbook f5.yml PLAY [all] ******************************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************** ok: [10.0.0.30] TASK [shell] ****************************************************************************************************************************** changed: [10.0.0.30] TASK [Get System Status] ****************************************************************************************************************** ok: [10.0.0.30] => { "msg": [ "tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 925/sshd ", "tcp6 0 0 :::22 :::* LISTEN 925/sshd " ] } PLAY RECAP ******************************************************************************************************************************** 10.0.0.30 : ok=3 changed=1 unreachable=0 failed=0
4.Playbook conditional statements
Conditional judgment statements in playbook use when
[root@manager ~]# cat f6.yml - hosts: all remote_user: root tasks: - name: Create File file: path=/tmp/this_is_{{ ansible_hostname }}_file state=touch when: (ansible_hostname == "nfs") or (ansible_hostname == "backup") #Only when the system is centos's host will it execute - name: Centos Install httpd yum: name=httpd state=present when: (ansible_distribution == "CentOS") #Only if the system is a ubuntu host will it execute - name: Ubuntu Install httpd yum: name=httpd2 state=present when: (ansible_distribution == "Ubuntu") #playbook execution results: [root@manager ~]# vim f6.yml [root@manager ~]# ansible-playbook f6.yml PLAY [all] ******************************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************** ok: [10.0.0.30] TASK [Create File] ************************************************************************************************************************ skipping: [10.0.0.30] #Host name mismatch is skipped and matching creates a file PLAY RECAP ******************************************************************************************************************************** 10.0.0.30 : ok=1 changed=0 unreachable=0 failed=0
5.Playbook Loop Statement
1. Standard Reuse Scenario - Bulk Installation Software
[root@manager ~]# cat f7.yml --- - hosts: all remote_user: root tasks: - name: Installed Pkg yum: name={{ item }} state=present with_items: - wget - tree - lrzsz #palybook execution results [root@manager ~]# ansible-playbook f7.yml PLAY [all] ******************************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************** ok: [10.0.0.30] TASK [Installed Pkg] ********************************************************************************************************************** ok: [10.0.0.30] => (item=[u'wget', u'tree', u'lrzsz']) PLAY RECAP ******************************************************************************************************************************** 10.0.0.30 : ok=2 changed=0 unreachable=0 failed=0
2. Standard reuse scenarios - creating users in bulk
[root@manager ~]# cat f7.yml - hosts: all remote_user: root tasks: - name: Add Users user: name={{ item.name }} groups={{ item.groups }} state=present with_items: - { name: 'testuser1', groups: 'bin' } - { name: 'testuser2', groups: 'root' } #palybook execution results [root@manager ~]# ansible-playbook f7.yml PLAY [all] ******************************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************** ok: [10.0.0.30] TASK [Add Users] ************************************************************************************************************************** changed: [10.0.0.30] => (item={u'name': u'testuser1', u'groups': u'bin'}) changed: [10.0.0.30] => (item={u'name': u'testuser2', u'groups': u'root'}) PLAY RECAP ******************************************************************************************************************************** 10.0.0.30 : ok=2 changed=1 unreachable=0 failed=0
3. Standard reuse scenarios - Copy multiple directories
[root@manager ~]# cat f7.yml - hosts: all remote_user: root tasks: - name: Configure Rsync Server copy: src={{ item.src }} dest=/etc/{{ item.dest }} mode={{ item.mode }} with_items: - {src: "rsyncd.conf", dest: "rsyncd.conf", mode: "0644"} - {src: "rsync.passwd", dest: "rsync.passwd", mode: "0600"}
6.Playbook exception handling
The default Playbook checks the return status of commands and modules and interrupts playbook execution if an error occurs, adding parameters: ignore_errors: yes Ignores errors
[root@manager ~]# cat f9.yml --- - hosts: all remote_user: root tasks: - name: Ignore False command: /bin/false ignore_errors: yes - name: touch new file file: path=/tmp/bgx_ignore state=touch
Skip errors during playbook
[root@manager ~]# ansible-playbook f9.yml PLAY [all] ******************************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************** ok: [10.0.0.30] TASK [Ignore False] *********************************************************************************************************************** fatal: [10.0.0.30]: FAILED! => {"changed": true, "cmd": ["/bin/false"], "delta": "0:00:00.002819", "end": "2018-11-13 07:22:47.301758", "msg": "non-zero return code", "rc": 1, "start": "2018-11-13 07:22:47.298939", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []} ...ignoring TASK [touch new file] ********************************************************************************************************************* changed: [10.0.0.30] PLAY RECAP ******************************************************************************************************************************** 10.0.0.30 : ok=3 changed=2 unreachable=0 failed=0
7.Playbook tags Tags
1. Labeling
Label an object One object Many labels One label many objects
2. Tag usage, bundled by tags and task objects, controls partial or specified task execution
-t:Perform the specified tag label task
--skip-tags: Perform tag tasks other than--skip-tags
[root@manager ~]# cat f10.yml --- - hosts: all remote_user: root tasks: - name: Install Nfs Server yum: name=nfs-utils state=present tags: - install_nfs - install_nfs-server - name: Service Nfs Server service: name=nfs-server state=started enabled=yes tags: start_nfs-server #Perform playbook normally [root@manager ~]# ansible-playbook f10.yml PLAY [all] ******************************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************** ok: [10.0.0.30] TASK [Install Nfs Server] ***************************************************************************************************************** ok: [10.0.0.30] TASK [Service Nfs Server] ***************************************************************************************************************** ok: [10.0.0.30] PLAY RECAP ******************************************************************************************************************************** 10.0.0.30 : ok=3 changed=0 unreachable=0 failed=0
Use -t to specify tags to execute, multiple tags separated by commas
[root@manager ~]# ansible-playbook -t install_nfs-server f10.yml PLAY [all] ******************************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************** ok: [10.0.0.30] TASK [Install Nfs Server] ***************************************************************************************************************** ok: [10.0.0.30] PLAY RECAP ******************************************************************************************************************************** 10.0.0.30 : ok=2 changed=0 unreachable=0 failed=0
Use --skip-tags to exclude tags that are not executed
[root@manager ~]# ansible-playbook --skip-tags install_nfs-server f10.yml PLAY [all] ******************************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************** ok: [10.0.0.30] TASK [Service Nfs Server] ***************************************************************************************************************** ok: [10.0.0.30] PLAY RECAP ******************************************************************************************************************************** 10.0.0.30 : ok=2 changed=0 unreachable=0 failed=0
8.Playbook Handlers
playbook Install Apache Example
[root@m01 ~]# cat webserver.yml - hosts: web remote_user: root #1. Define variables and call them in the configuration file vars: http_port: 8881 #2. Install the httpd service tasks: - name: Install Httpd Server yum: name=httpd state=present #3. Using the template, reference the variables defined by vars above to the configuration file - name: Configure Httpd Server template: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf notify: Restart Httpd Server #4. Start the Httpd service - name: Start Httpd Server service: name=httpd state=started enabled=yes #5. Check the current running port status of the Httpd service - name: Get Httpd Server Port shell: netstat -lntp|grep httpd register: Httpd_Port #6. Output the status of Httpd running to the panel - name: Out Httpd Server Status debug: msg={{ Httpd_Port.stdout_lines }} ignore_errors: yes #6. If the configuration file changes, the module below the handlers will be called handlers: - name: Restart Httpd Server service: name=httpd state=restarted
9.Playbook Include
Include is used to dynamically include the tasks task list, include_tasks new/old
include Call Task Method
#Main Entry File [root@mha ~]# cat main.yml - hosts: all remote_user: root tasks: - include_tasks: f20.yml - include_tasks: f21.yml #f20.yml [root@mha ~]# cat f20.yml - name: create file1 command: touch file1 #21.yml [root@mha ~]# cat f21.yml - name: create file2 command: touch file2