06 ansible script function practices introduced

 


ansible script function practices introduced
============================================= ================================================== =
01. the important function of writing the script describes
a variable set in the script information OK
b disposed in the script when the script execution OK registration information, can display the output results of the command information
b OK judgment information set in the script
c disposed in the script information circulating
d is set to ignore errors in the script
d set in the script tag information
e to set the trigger information in the script
f be integrated in the script, the script


02. set the variable information in the script
1: direct written in the script file
VARS:
oldboy01: DATA01
oldboy02 : DATA02

way: specified on the command line
ansible-playbook --extra-vars = oldboy01 = data01

Three ways: writing in the host list file
[Oldboy]
oldboy01 = DATA01
oldboy02 = DATA02

Three kinds of variable settings are configured the way, priority ??? three ways of
top priority: Command-line variable settings
lower priority: the script variable is set to
last: a list of host variable settings

How to set up global variables: roles script integration


03. Set login information in the script
- hosts: Oldboy
Tasks:
- name: the Check Server Port
shell: netstat -lntup --- port information
register: get_server_port <- port information

- name: display info Port
Debug: MSG = {{}} get_server_port.stdout_lines
displaying process information, service has been started normally
PS: no space provided variable information

04. information provided in the script determines
how to specify the condition is determined:
(ansible_hostname == "nfs01" )
(ansible_hostname == "web01")
details of the managed host system setup display module

- hosts: oldboy
remote_user: root
tasks:
- name: Check File
file: path=/tmp/this_is_{{ ansible_hostname }}_file state=touch
when: (ansible_hostname == "nfs") or (ansible_hostname == "backup")

- name: install httpd
yum: name=httpd state=installed
when: (系统情况 == "CentOS")

- name: install httpd2
yum: name=httpd2 state=installed
when: (系统情况 == "ubuntu")

Get built-in variables method:
ansible Oldboy Setup -a -m "filter = ansible_hostname"
common host information:
ansible_all_ipv4_addresses: display only the information of ipv4.
ansible_devices: displays only the disk device information.
ansible_distribution: Display what system, for example: centos, suse like.
ansible_distribution_major_version: displays the main version of the system.
ansible_distribution_version: show only system version.
ansible_machine: the type of display system, for example: 32-bit, or 64-bit.
ansible_eth0: display only the information for eth0.
ansible_hostname: Only the host names.
ansible_kernel: displays only the kernel version.
ansible_lvm: lvm show information.
ansible_memtotal_mb: Displays the total system memory.
ansible_memfree_mb: Display of available system memory.
ansible_memory_mb: detailed display memory situation.
ansible_swaptotal_mb: displays the total swap memory.
ansible_swapfree_mb: display swap memory available memory.
ansible_mounts: display system mounted disk situation.
ansible_processor: the number of display cpu (cpu specifically shown for each model).
ansible_processor_vcpus: the number of display CPU (only the total number of display).

The method of obtaining the sub information:
ansible_eth0 [IPv4]

04. 在剧本中设置循环信息
vim test04.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' }

vim test05.yml
- hosts: all
remote_user: root
tasks:
- name: Installed Pkg
yum: name={{ item }} state=present
with_items:
- wget
- tree
- lrzsz

05. Ignore errors in the script set
the default playbook will check the command module and return to the state, such as it encounters an error interrupts the execution of the playbook
can be added ignore_errors: yes ignore the error
vim test06.yml
- hosts: All
REMOTE_USER: root
Tasks:
- name : false Ignore
the Command: / bin / false
ignore_errors: yes
- name: Touch new new File
File: path = / tmp / oldboy_ignore State = Touch

06. 在剧本中设置标签功能
- hosts: oldboy
ignore_errors: yes
remote_user: root
tasks:
- name: Check File
file: path=/tmp/this_is_{{ ansible_hostname }}_file state=touch
when: (ansible_hostname == "nfs01") or (ansible_hostname == "backup")
tags: t1

- name: bad thing
command: ech 123
#ignore_errors: yes
tags: t2

- name: install httpd
yum: name=httpd state=installed
when: (ansible_all_ipv4_addresses == ["172.16.1.7","10.0.0.7"])
tags: t3

- name: install httpd2
yum: name=httpd2 state=installed
when: (ansible_distribution == "ubuntu")
tags: t4

Tags specify which tasks are performed: ansible-playbook --tags = t2 test05.yml
skipped tasks specified tags: ansible-playbook --skip-tags = t2 test05.yml

07. 在剧本中设置触发功能
- hosts: backup
remote_user: root
tasks:
- name: 01 Install rsync
yum: name=rsync state=present

- name: 02 push config file
copy: src=./file/{{ item.src }} dest=/etc/{{ item.dest }} mode={{ item.mode }}
with_items:
- { src: "rsyncd.conf", dest: "rsyncd.conf", mode: "0644" }
- { src: "rsync.password", dest: "rsync.password", mode: "0600" }
notify: restart rsync server

handlers:
- name: restart rsync server
service: name=rsyncd state=restarted


08. integrate multiple script
Method 1: include_tasks: f1.yml
- hosts: All
REMOTE_USER: root
Tasks:
- include_tasks: f1.yml
- include_tasks: f2.yml

方式二: the include: f1.yml
- the include: f1.yml
- the include: f2.yml

方式三: - import_playbook:
[root @ m01 ansible-playbook] # cat main.yml
- import_playbook: base.yml
- import_playbook: rsync.yml
- import_playbook: nfs.yml
- import_playbook: oxxx.yml
- import_playbook: rsync.yml
- import_playbook: nfs.yml

Guess you like

Origin www.cnblogs.com/linux985/p/11320228.html