当前位置:网站首页>Ansible playbook and ansible roles (III)
Ansible playbook and ansible roles (III)
2022-06-12 21:44:00 【Stay up late and soak wolfberry】
List of articles
1. playbook Judgment statement
Judgment in Ansible Very frequently used in tasks . such as yum The module can detect whether the software package has been installed , In this process, we do not need to do too much manual intervention
But there are also tasks that need to be judged , such as :web All server roles need to be installed nginx Warehouse , But other server roles don't need , You'll use when Judge .
such as :Centos And Ubuntu All systems need to be installed httpd service , Then we need to use when Judge the host system , Then call different modules to execute .
Practical cases 1: According to different operating systems , Install the same package
[[email protected] when]# cat when.yml
- hosts: webservers
tasks:
- name: Install httpd Server
yum: name=httpd state=present
when: ansible_distribution == "CentOS"
- name: Install httpd Server
apt: name=httpd2 state=present
when: ansible_distribution == "Ubuntu"
Practical cases 2: All for web Addition of host name nginx Warehouse , The rest skip adding
1. How to add yum Warehouse
2. How to determine , Judge what content
---
[[email protected] when]# cat when.yml
- hosts: web
tasks:
- name: Add Nginx Repos
yum_repository: # modular
name: nginx_tet # It represents /etc/yum.repos.d/nginx_tet.repo
description: Nginx YUM repo # describe ( No mistake will be reported )
baseurl: http://nginx.org/packages/centos/7/$basearch/
gpgcheck: no # Turn off the test
when: (ansible_hostname is match ("web*")) or (ansible_hostname is match ("lb*")) # According to the official writing
1. adopt register Save the result of command execution to a variable , And then through when Sentence to judge
- hosts: webservers
tasks:
- name: Check Httpd Server
command: systemctl is-active httpd
ignore_errors: yes
register: check_httpd
#- name: debug outprint # Just output the results
# debug: var=check_httpd
- name: Httpd Restart
service: name=httpd state=restarted
when: check_httpd.rc == 0
==========================================
(1) Judge according to different host names
[[email protected] when]# cat when.yml
- hosts: web
vars:
remote_ip: "{
{ ansible_default_ipv4.address }}"
host_name: "{
{ ansible_fqdn }}"
tasks:
- name: Print ip # Tested by
debug:
msg:
- "{
{ remote_ip }}"
- "{
{ host_name }}"
- name: Reboot Server
command: reboot
when: ansible_fqdn == "web02"
(2) According to different IP Address to determine
[[email protected] when]# cat when.yml
- hosts: web
vars:
remote_ip: "{
{ ansible_default_ipv4.address }}"
host_name: "{
{ ansible_fqdn }}"
tasks:
- name: Print ip
debug:
msg:
- "{
{ remote_ip }}"
- "{
{ host_name }}"
- name: Reboot Server
command: reboot
when: ansible_default_ipv4.address == "10.0.0.7"
(3) Judge according to the size of a parameter value obtained from the client
- hosts: web
vars:
remote_ip: "{
{ ansible_default_ipv4.address }}"
host_name: "{
{ ansible_fqdn }}"
host_mem: "{
{ ansible_memtotal_mb }}"
tasks:
- name: Print ip
debug:
msg:
- "{
{ remote_ip }}"
- "{
{ host_name }}"
- "{
{ host_mem }}"
- name: Reboot Server
command: reboot
when: ansible_memtotal_mb|int < "2000"
(4) List judgment and Relationship And the relationship
[[email protected] when]# cat when.yml
- hosts: web
vars:
remote_ip: "{
{ ansible_default_ipv4.address }}"
host_name: "{
{ ansible_fqdn }}"
host_mem: "{
{ ansible_memtotal_mb }}"
host_ver: "{
{ ansible_distribution }}"
tasks:
- name: Print ip
debug:
msg:
- "{
{ remote_ip }}"
- "{
{ host_name }}"
- "{
{ host_mem }}"
- "{
{ host_ver }}"
- name: Reboot Server
file:
path: /root/web01.txt
state: touch
when:
- ansible_distribution == "CentOS"
- ansible_fqdn == "web01"
(6) Or relationship judgment
[[email protected] when]# cat when.yml
- hosts: web
vars:
remote_ip: "{
{ ansible_default_ipv4.address }}"
host_name: "{
{ ansible_fqdn }}"
host_mem: "{
{ ansible_memtotal_mb }}"
host_ver: "{
{ ansible_distribution }}"
tasks:
- name: Print ip
debug:
msg:
- "{
{ remote_ip }}"
- "{
{ host_name }}"
- "{
{ host_mem }}"
- "{
{ host_ver }}"
- name: Reboot Server
file:
path: /root/web0102.txt
state: touch
when: ansible_distribution == "CentOS" or ansible_fqdn == "web02"
(7) modify Nginx The configuration file hold Nginx from ansible Server copy to web The server
The copy needs to be started or restarted If Nginx The configuration file is wrong Don't let load
1. install Nginx
2. Copy files to Nginx.conf Determine whether the file is correct ngixn -t Variable registration receives results
3. start-up
4. Reload At the reload location when Judge
[[email protected] when]# cat nginx.yml
- hosts: web02
tasks:
- name: Install Nginx
yum:
name: nginx
state: present
- name: scp configure Nginx
copy:
src: ./nginx.j2
dest: /etc/nginx/nginx.conf
- name: Check Nginx Configure
command: /usr/sbin/nginx -t
register: result
ignore_errors: yes # Ignore the error and continue
- name: print result
debug:
msg: "{
{ result.rc }}"
- name: Reload Nginx
systemd:
name: nginx
state: reloaded
when: result.rc == 0 # Judge nginx -t Execution results Is it 0 Obtained through variable registration
-----------------------------
- name: Reload Nginx
systemd:
name: nginx
state: reloaded
when: result.rc is match "0"
(8)rsync The service determines the host name (rsync The optimization of the )
[[email protected] rsync]# cat rsync.yml
- hosts: rsyncall
tasks:
- name: Install Rsyncd Server
yum:
name: rsync
state: present
- name: Create www Group
group:
name: www
gid: 666
- name: Create User www
user:
name: www
uid: 666
group: www
create_home: false
shell: /sbin/nologin
- name: Configure Rsync Server
copy:
src: rsync.j2
dest: /etc/rsyncd.conf
when: ansible_hostname == "backup"
- name: Auth Password
copy:
content: rsync_backup:123456
dest: /etc/rsync.passwd
mode: 0600
when: ansible_hostname == "backup"
- name: Create Dir /backup
file:
path: /backup
state: directory
owner: www
group: www
recurse: yes
when: ansible_hostname == "backup"
- name: Start Rsyncd Server
systemd:
name: rsyncd
state: started
enabled: yes
when: ansible_hostname == "backup"
- name: clinet Auth Password
copy:
content: 123456
dest: /etc/rsync.passwd
mode: 0600
when: ansible_hostname == "web01" or ansible_hostname == "web02"
(9) Judge http Survival
- hosts: web02
tasks:
- name: Install Nginx
yum:
name: nginx
state: present
- name: scp configure Nginx
copy:
src: ./nginx.j2
dest: /etc/nginx/nginx.conf
- name: Check Nginx Configure
command: /usr/sbin/nginx -t
register: result
ignore_errors: yes
- name: Check HTTP Server
command: systemctl is-active httpd
register: result_http
ignore_errors: yes
- name: print result
debug: var=result_http
- name: Reload Nginx
systemd:
name: nginx
state: reloaded
when: result.rc == 0
2. playbook Loop statement
Sometimes we write playbook I found a lot when I got there task Must repeatedly reference a module , For example, a startup 10 A service , Or one copy 10 File , If you follow the traditional way of writing, at least write 10 Time , It would seem playbook It's bloated . If you use a loop to write playbook, This can reduce the reuse of a module .
Practice case 1 、 Use the loop to start multiple services
[[email protected] project2]# cat with.yml
- hosts: webservers
tasks:
- name: Start httpd mariadb
systemd: name={
{
item }} state=started
with_items:
- httpd
- mariadb
1. Case 2 、 Use the defined variable method to cycle through the installation of software packages .
- hosts: webservers
tasks:
- name: ensure a list of packages installed
yum: name= "{
{
packages }}" state=present
vars:
packages:
- httpd
- httpd-tools
# Ways to discard
- hosts: webservers
tasks:
- name: ensure a list of packages installed
yum: name= "{
{
item }}" state=present
with_items:
- httpd
- httpd-tools
Practice case 3 、 Create user and batch copy files using dictionary looping
[[email protected] ~]# cat loop-user.yml
- hosts: webservers
tasks:
- name: Add Users
user: name={
{
item.name }} groups={
{
item.groups }} state=present
with_items:
- {
name: 'testuser1', groups: 'bin' }
- {
name: 'testuser2', groups: 'root' }
[[email protected] project2]# cat with4.yml
- hosts: webservers
tasks:
- name: Copy Rsync configure and Rsync passwd
copy: src={
{
item.src }} dest={
{
item.dest }} mode={
{
item.mode }}
with_items:
- {
src: "./rsyncd.conf", dest: "/etc/rsyncd.conf", mode: "0644" }
- {
src: "./rsync.passwd", dest: "/tmp/rsync.passwd", mode: "0600" }
===============================================
Official recommended writing :
[[email protected] when]# vim when.yml
- hosts: web
tasks:
- name: Started Server
yum: # systemd I can't get up
name: "{
{ packages }}"
state: started
vars:
packages:
- httpd
- mariadb
You can also write like this :
[[email protected] when]# vim when.yml
- hosts: web
tasks:
- name: Started Server
yum: # systemd I can't get up
name: "{
{ item }}"
state: started
with_items:
- httpd
- mariadb
(1) Use with_item Loop list
[[email protected] file]# cat create_file.yml
- hosts: web
tasks:
- name: Create file
file:
path: "{
{ item }}"
state: touch
with_items:
- file1.txt
- file2.txt
(2) Use loop Method to cycle through the list
[[email protected] file]# cat create_file.yml
- hosts: web
tasks:
- name: Create file
file:
path: "{
{ item }}"
state: touch
loop:
- file1.txt
- file2.txt
(3) Cycle start list Start multiple services at once
[[email protected] file]# cat start.yml
- hosts: web
tasks:
- name: Install HTTPD Nginx Mariadb Server
yum:
name: "{
{ item }}"
state: present
loop:
- nginx
- mariadb-server
- name: Start HTTP Nginx Mariadb Sever
systemd:
name: "{
{ item }}"
state: started
loop:
- nginx
- mariadb
(4) Copy two files to the destination
1.txt The destination host is 600
2.txt The destination host is 644
[[email protected] file]# cat scp.yml
- hosts: web
tasks:
- name: scp file dest web01 web02
copy:
src: "{
{ item }}"
dest: /root/
loop:
- 1.txt
- 2.txt
(5)scp Multiple files to target host Authority is different
[[email protected] file]# cat scp.yml
- hosts: web
tasks:
- name: scp file dest web01 web02
copy:
src: "{
{ item.src }}"
dest: "{
{ item.dest }}"
owner: "{
{ item.owner }}"
group: "{
{ item.group }}"
mode: "{
{ item.mode }}"
loop:
- { src: 1.txt,dest: /root/,mode: '0600',owner: www,group: root }
- { src: 2.txt,dest: /opt/,mode: '0000',owner: root,group: www }
(6)rsync Copy files (rsync Optimize )
[[email protected] rsync]# vim rsync.yml
[[email protected] rsync]# cat rsync.yml
- hosts: rsyncall
tasks:
- name: Install Rsyncd Server
yum:
name: rsync
state: present
- name: Create www Group
group:
name: www
gid: 666
- name: Create User www
user:
name: www
uid: 666
group: www
create_home: false
shell: /sbin/nologin
- name: Configure Rsync Server
copy:
src: "{
{ item.src }}"
dest: "{
{ item.dest }}"
mode: "{
{ item.mode }}"
loop:
- { src: rsync.j2, dest: /etc/rsyncd.conf,mode: '0644' }
- { src: rsync_pass.j2, dest: /etc/rsync.passwd,mode: '0600' }
when: ansible_hostname == "backup"
- name: Create Dir /backup
file:
path: /backup
state: directory
owner: www
group: www
recurse: yes
when: ansible_hostname == "backup"
- name: Start Rsyncd Server
systemd:
name: rsyncd
state: started
enabled: yes
when: ansible_hostname == "backup"
- name: clinet Auth Password
copy:
content: 123456
dest: /etc/rsync.passwd
mode: 0600
when: ansible_hostname == "web01" or ansible_hostname == "web02"
3. playbook handlers
handler Used to perform tasks under certain conditions , For example, when the configuration file changes , adopt notify Trigger handler To restart the service .
stay saltstack There are similar triggers in , The writing method is relatively Ansible Simple , It only needs watch, Configuration file .
[[email protected] project2]# cat han.yml
- hosts: webservers
vars:
- http_port: 8083
tasks:
- name: Install Http Server
yum: name=httpd state=present
- name: configure httpd server
template: src=./httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify:
- Restart Httpd Server
- Restart PHP Server
- name: start httpd server
service: name=httpd state=started enabled=yes
handlers:
- name: Restart Httpd Server
systemd: name=httpd state=restarted
- name: Restart PHP Server
systemd: name=php-fpm state=restarted
3.handlers matters needing attention
1. No matter how many task The same handlers,handlers Only in all tasks Run once after it's over .
2. Only task You will not be notified until there is a change handlers, No change will not trigger handlers
3. Out of commission handlers replace tasks
====================================================
handle matters needing attention :
1. No matter how many task The same handlers,handlers Only in all tasks Run once after it's over .
2. Only task You will not be notified until there is a change handlers, No change will not trigger handlers
3. Out of commission handlers replace tasks
(1)handlers Trigger modification
[[email protected] handlers]# cat handlers.yml
- hosts: web01
vars:
- http_port: 82
tasks:
- name: Install Nginx
yum:
name: nginx
state: present
- name: Configure Nginx Server
template: # Variables can be used in scripts , You can use system variables , You can also use your own defined variables
src: nginx_conf.j2
dest: /etc/nginx/nginx.conf
notify: Reload Nginx
- name: Start Nginx Server
systemd:
name: nginx
state: started
enabled: yes
handlers:
- name: Reload Nginx
systemd:
name: nginx
state: reloaded
(2) Restart after triggering nginx and mariadb
[[email protected] handlers]# cat handlers.yml
- hosts: web01
vars:
- http_port: 83
tasks:
- name: Install Nginx
yum:
name: nginx
state: present
- name: Configure Nginx Server
template:
src: nginx_conf.j2
dest: /etc/nginx/nginx.conf
notify: Reload Nginx
- name: Start Nginx Server
systemd:
name: nginx
state: started
enabled: yes
handlers:
- name: Reload Nginx
systemd:
name: "{
{ item }}"
state: restarted
loop:
- nginx
- mariadb
(2)handlers Trigger nginx and mariadb restart also when Judge nginx Is the document correct
[[email protected] handlers]# cat handlers.yml
- hosts: web01
vars:
- http_port: 85
tasks:
- name: Install Nginx
yum:
name: nginx
state: present
- name: Configure Nginx Server
template:
src: nginx_conf.j2
dest: /etc/nginx/nginx.conf
notify: Reload Nginx
- name: Check Nginx Configuer
command: /usr/sbin/nginx -t
ignore_errors: yes
register: result
- name: Start Nginx Server
systemd:
name: nginx
state: started
enabled: yes
handlers:
- name: Reload Nginx
systemd:
name: "{
{ item }}"
state: restarted
loop:
- nginx
- mariadb
when: result.rc == 0
4. Playbook Task tag
By default ,Ansible In the execution of a playbook when , Will execute playbook There are many tasks defined in .Ansible The label of (tags) Functions can be given to individual tasks or even the whole playbook tagged , Then use these tags to specify that you want to run playbook Individual tasks in , Or do not perform the specified task .( It can be debugged )
1. There are several ways to label , such as :
To a task Make a label 、 To a task Multiple labels 、 For many task Make a label
2. Yes task How to use the label after printing
-t : Execute specified tag Tag task
--skip-tags : perform --skip-tags Tag tasks other than
ansible-playbook tag.yml --list-tags See how many tags
tag Mark ( For debugging scenarios )
[[email protected] project2]# cat tag.yml
- hosts: webservers
vars:
- http_port: 8083
tasks:
- name: Install Http Server
yum: name=httpd state=present
tags:
- install_httpd
- httpd_server
- name: configure httpd server
template: src=./httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify: Restart Httpd Server
tags:
- confiure_httpd
- httpd_server
- name: start httpd server
service: name=httpd state=started enabled=yes
tags: service_httpd
handlers:
- name: Restart Httpd Server
systemd: name=httpd state=restarted
[[email protected] project2]# ansible-playbook tag.yml --list-tags
[[email protected] project2]# ansible-playbook tag.yml -t httpd_server
[[email protected] project2]# ansible-playbook tag.yml -t install_httpd,confiure_httpd
[[email protected] project2]# ansible-playbook tag.yml --skip-tags httpd_server
5. Playbook File reuse
include Used to dynamically include tasks Task list include——tasks new edition /include Old version 
Include contain
include(import_playbook)
include_tasks
[[email protected] project2]# cat task.yml
- hosts: webservers
vars:
- http_port: 801
tasks:
- include_tasks: task_install.yml
- include_tasks: task_configure.yml
- include_tasks: task_start.yml
handlers:
- name: Restart Httpd Server
systemd: name=httpd state=restarted
[[email protected] project2]# cat task_install.yml
- name: Install Http Server
yum: name=httpd state=present
[[email protected] project2]# cat task_configure.yml
- name: configure httpd server
template: src=./httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify: Restart Httpd Server
[[email protected] project2]# cat task_start.yml
- name: start httpd server
service: name=httpd state=started enabled=yes
6. Playbook Ignore mistakes
Default playbook Will check the tasks The return state of the execution , If errors are encountered, they will be planted immediately playbook The follow-up tasks perform . But sometimes playbook Even if the execution is wrong, let it continue
Add parameters :ignore_error:yes Ignore mistakes
- To write playbook, When there is task If the execution fails, the subsequent task function
Ignore mistakes ignore_errors
[[email protected] ~]# cat f9.yml
---
- hosts: webservers
tasks:
- name: Ignore False
command: /bin/false
ignore_errors: yes
- name: touch new file
file: path=/tmp/bgx_ignore state=touch
7. Playbook exception handling
Usually , When task After failure ,play Will terminate , Anything that has been tasks notify Of handlers Will not be implemented . If you are in the play Set up in force_handlers: yes Parameters , Notified handlers Will be enforced .( Some special scenarios may be used )
Case a : task Execution failed to force the call handlers
Case 2 : control task The status of the report , When not necessary "changed"
exception handling
force_handlers: yes Mandatory call handlers
changed_when: false The managed host has not changed , You can use parameters to change Status changed to ok
changed_when: httpd_check.stdout.find('OK') # View a string in a variable
[[email protected] project2]# cat changed_when.yml
- hosts: webservers
vars:
- http_port: 8083
tasks:
- name: configure httpd server
template: src=./httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify: Restart Httpd Server
- name: Check HTTPD
shell: /usr/sbin/httpd -t
register: httpd_check
changed_when:
- httpd_check.stdout.find('OK')
- false
- name: start httpd server
service: name=httpd state=started enabled=yes
handlers:
- name: Restart Httpd Server
systemd: name=httpd state=restarted
failed_when
The command does not rely on the return status code to determine whether the execution fails , Instead, check the contents returned by the command to determine , For example, the returned content includes failed character string , It is judged as failure . Examples are as follows :
- name: this command prints FAILED when it fails
command: /usr/bin/example-command -x -y -z
register: command_result
failed_when: "'FAILED' in command_result.stderr"
A small summary :
-------tasl Process and control parameters related to task processing
when Judge
item loop
handlers trigger ( need task Use notify notice )
tags label ( Debugging use )
include_tasks contain task Mission
ignore_errors Ignore mistakes
Error handling
force_handlers # Expand
changed_when false The state of inhibiting change is ok( When getting the status information of the system )
------------------------------ important ( Check whether the service configuration is normal , If it is normal, it will not be handled , If it is abnormal, it will be interrupted )
register: httpd_check
changed_when:
- httpd_check.stdout.find('OK')
- false
8. Ansible Valut summary
Ansible Vault As ansible A new feature of can be used for example passwords,keys And other sensitive data files for encryption , Not in plain text playbooks or roles in
8.1 Ansible Valut practice
ansible Encryption module
[[email protected] project2]# ansible-vault --help
Usage: ansible-vault [create|decrypt|edit|encrypt|encrypt_string|rekey|view] [options] [vaultfile.yml]
Encrypt a file
ansible-vault encrypt include.yml
Check a file
[[email protected] project2]# ansible-vault view include.yml
Vault password:
- import_playbook: han.yml
- import_playbook: when2.yml
Modify the encrypted file contents
[[email protected] project2]# ansible-vault edit include.yml
rekey Change Password
[[email protected] project2]# ansible-vault rekey include.yml
Vault password:
New Vault password:
Confirm New Vault password:
Rekey successful
Perform encrypted playbook
echo "1" >pass
chmod 600 pass
ansible-playbook include.yml --vault-password-file=pass
9. Ansible Jinja2 Templates


jinja Templates
2.jinja Template logical relationship
{
% for i in EXPR %}...{
% endfor%} As a loop expression
{
% if EXPR %}...{
% elif EXPR %}...{
% endif%} To judge as a condition
--------------------------------------nginx
[[email protected] project2]# cat jinja_nginx.yml
- hosts: webservers
vars:
- http_port: 80
- server_name: www.oldboyedu.com
tasks:
- name: Copy Nginx COnfigure
template: src=./oldboyedu.conf.j2 dest=/etc/nginx/conf.d/oldboyedu_proxy.conf
[[email protected] project2]# cat oldboyedu.conf.j2
upstream {
{
server_name }} {
{
% for i in range(1,20) %}
server 172.16.1.{
{
i}}:{
{
http_port}};
{
%endfor%}
}
server {
listen {
{
http_port }};
server_name {
{
server_name }};
location / {
proxy_pass http://{
{
server_name }};
proxy_set_header Host $http_host;
}
}
--------------------------------------keepalived
[[email protected] project2]# cat jinja_keepalived.yml
- hosts: webservers
tasks:
- name: Copy Keepalived Configure
template: src=./kee.conf.j2 dest=/tmp/keepalived.conf
[[email protected] project2]# cat kee.conf.j2
global_defs {
router_id {
{
ansible_hostname }}
}
vrrp_instance VI_1 {
{
%if ansible_hostname =="web01" %}
state MASTER
priority 150
{
%elif ansible_hostname == "web02" %}
state BACKUP
priority 100
{
%endif%}
interface eth0
virtual_router_id 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3
}
}
Use Ansible jinja IF Generate different mysql The configuration file ( Custom variable )
[[email protected] project2]# cat jinja_mysql.yml
- hosts: webservers
gather_facts: no
vars:
PORT: 13306
# PORT: false # Equivalent to a switch
tasks:
- name: Copy MySQL Configure
template: src=./my.cnf.j2 dest=/tmp/my.cnf
[[email protected] project2]# cat my.cnf.j2
{
% if PORT %}
bind-address=0.0.0.0:{
{
PORT }}
{
% else %}
bind-address=0.0.0.0:3306
{
%endif%}
10. Ansible Roles

tasks There can only be one... In the directory main.yml





role role
[[email protected] ~]# cd /etc/ansible/roles/
[[email protected] roles]# mkdir nfs/{tasks,handlers,templates} -pv
mkdir: created directory ‘nfs’
mkdir: created directory ‘nfs/tasks’
mkdir: created directory ‘nfs/handlers’
mkdir: created directory ‘nfs/templates’
[[email protected] roles]# cat nfs/tasks/install.yml
- name: Install NFS-utils Server
yum: name=nfs-utils state=present
[[email protected] roles]# cat nfs/tasks/config.yml
- name: Configure Nfs-utils Server
template: src=./exports.j2 dest=/etc/exports owner=root group=root mode=0644
notify: Restart NFS Server
[[email protected] roles]# cat nfs/tasks/start.yml
- name: Start NFS Server
systemd: name=nfs state=started enabled=yes
[[email protected] roles]# cat nfs/tasks/main.yml
- include_tasks: install.yml
- include_tasks: config.yml
- include_tasks: start.yml
[[email protected] roles]# cat nfs-client/tasks/main.yml
- name: Mount NFS Server
mount: path=/opt src=172.16.1.7:/data fstype=nfs opts=defaults state=mounted
[[email protected] roles]#
[[email protected] roles]#
[[email protected] roles]# cat site.yml
- hosts: web01
roles:
- nfs
- hosts: web02
roles:
- nfs-client
---------------------------- install memcached
[[email protected] roles]# mkdir memcached/{tasks,handlers,templates} -pv
mkdir: created directory ‘memcached’
mkdir: created directory ‘memcached/tasks’
mkdir: created directory ‘memcached/handlers’
mkdir: created directory ‘memcached/templates’
11. AnsibleGalaxy



12. Self summary
Conditional statements :
1. A simple judgment of a condition
(1) Judge according to the host (when、match)
when ansible_distribution == "Ubuntu"
Judge according to the host -->match
when (ansible_hostname is match ("web")) or (ansible_hostname is natch("lb"))
(2) Judge according to the host name
when ansible_fqdn =="web02"
(3) According to ip Address to determine
when ansible_default_ipv4.address == "10.0.0.7"
(4) Judge the size according to the memory value
when ansible_memtotal_mb | int < "2000"
(5) Judge nginx -t Execution results Is it 0, If result.rc == 0 The execution is correct ( This requires a registration variable )
when result.rc == 0
result.rc is match "0"
2. Judgment of multiple conditions
(1) List judgment and And the relationship ( Or two relationships are represented by a list )
when:
- ansible_distribution == "CentOS"
- ansible_fqdn == "web01"
when: ansible_default_ipv4.address == "10.0.0.7" and ansible_fqdn == "web1"
(2) perhaps or Relationship judgment
when:ansible_distribution == "CentOS" or ansible_fqdn == "web02"
Loop statement :
1. with_item How to write a circular list
file:
path: "{
{ item }}"
state: touch
with_items:
- file1.txt
- file2.txt
2. Use loop Method to cycle through the list
file:
path: "{
{ item }}"
state: touch
loop:
- file1.txt
- file2.txt
3. Dictionary way ( A loop can write multiple conditions )
copy:
src: "{
{ item.src }}"
dest: "{
{ item.dest }}"
owner: "{
{ item.owner }}"
group: "{
{ item.mode }}"
mode: "{
{ item.mode }}"
loop:
- { src: 1.txt , dest:/root/ , mode: '0600' , owner: www , group: root }
- { src: 2.txt , dest: /opt/ , mode: '0000' , owner: root , group: www}
playbook handlers
1. Where monitoring is required
notify: Reload Nginx
2. Trigger monitoring
handlers:
- name Reload Nginx
systemd:
name: "{
{ item }}"
state: restarted
loop:
- nginx
- mariadb
when: result.rc == 0 # Check the grammar for judgment
边栏推荐
- PCB封装下载网站推荐及其详细使用方法
- SQL调优指南笔记11:Histograms
- Permission to query execution plan in Oracle Database
- 服务没有报告任何错误mysql
- 【QNX Hypervisor 2.2 用户手册】4.4 构建Host
- 关于 安装Qt5.15.2启动QtCreator后“应用程序无法正常启动0xc0000022” 的解决方法
- 【QNX Hypervisor 2.2 用戶手册】4.2 支持的構建環境
- Yanghui triangle code implementation
- Thread safe level
- How to implement a simple publish subscribe mode
猜你喜欢
![[target detection] |dive detector into box for object detection new training method based on fcos](/img/ac/c54c2733dceffea086b772f35f128a.png)
[target detection] |dive detector into box for object detection new training method based on fcos

KDD2022 | GraphMAE:自监督掩码图自编码器
![Fill in the checklist & lt; int&gt; Have default values? [repeat] - fill list & lt; int&gt; with default values? [duplicate]](/img/65/a214d137e230b1a1190feb03660f2c.jpg)
Fill in the checklist & lt; int&gt; Have default values? [repeat] - fill list & lt; int&gt; with default values? [duplicate]

ICML2022 | GALAXY:極化圖主動學習

@loadbalance annotation of resttemplate

测试基础之:单元测试

SQL调优指南笔记9:Joins

复杂系统如何检测异常?北卡UNCC等最新《复杂分布式系统中基于图的深度学习异常检测方法综述》,阐述最新图异常检测技术进展

Recommended Chinese font in the code input box of Oracle SQL developer

JUC并发工具包使用指南
随机推荐
NiO User Guide
Pixel level reconstruction and restoration technology to solve severe image blur
Ansible playbook和Ansible Roles(三)
阅读笔记 Deep Hough Voting for 3D Object Detection in Point Clouds
What is the race condition? How do you find and solve the competition?
在同花顺开户证券安全吗,证券开户怎么开户流程
linux备份mysql
Oracle SQL Developer的代码输入框中推荐使用的中文字体
zgc 并发标识和并发转移阶段的多视图地址映射
Deep Hough voting for 3D object detection in point clouds
SQL tuning guide notes 10:optimizer statistics concepts
【QNX Hypervisor 2.2 用户手册】4.4 构建Host
SQL tuning guide notes 8:optimizer access paths
Recommended Chinese font in the code input box of Oracle SQL developer
ZGC concurrent identity and multi view address mapping in concurrent transition phase
数据批量写入
How do I create a daemon thread? And where to use it?
VagrantBox重新安装vboxsf驱动
MySQL介绍和安装(一)
Zip compression decompression