1, Playbook variable
ansible use variables, allow us to work more flexible
Define the variable
Variable names should consist of letters, numbers, underscores, variable names need to start with a letter, ansible built-in keyword can not be used as a variable name use
2, Playbook variable definitions
When using the keyword vars variables can be defined in the current playbook, so that we want to use a variable, you need to reference the corresponding variable name, use {{name}} variable can refer to the corresponding variable.
A simple definition of variables
- hosts: all
vars: #定义变量
file_name: jsn_yaml_vars #变量名(随意):file_name,变量的内容:jsn_yaml_vars
tasks: #{{ file_name }}引用上面定义的变量
- name: 应用变量名:file_name
file: path=/tmp/{{ file_name }} state=touch
Define multiple variables
- hosts: all
vars:
t1: v1
t2: v2
In addition to the above grammar, syntax sequences can also be custom variable speed YAML
- hosts: all
vars:
- t1: v1
- t2: v2
When defining a variable, it may also be similar to the "Properties" define variable
- hosts: 10.4.7.7
vars:
nginx:
proxy_1: /etc/nginx/conf.d/nginx_1.proxy
proxy_2: /etc/nginx/conf.d/nginx_2.proxy
tasks:
- name: task1
file:
path: "{{ nginx.proxy_1 }}"
state: touch
- name: task1
file:
path: "{{ nginx['proxy_2'] }}" # 第二种引用变量.属性的方法
state: touch
Node: Note that we use double quotes in the use of variables, but did not use double quotation marks when referring to variables in the example at the beginning of the most, because it is the first instance when the variable is referenced, and not in the beginning of the position path=/tmp/{{ file_name }}
, so do not need to double quotes, but path: "{{ nginx.proxy_1 }}"
in the beginning of the variable position, you need to double quotes
Define the variable written to the specified file and then referenced in the playbook in
1. Create nginx_vars.yaml file, define the variable
nginx:
proxy_1: /etc/nginx/conf.d/nginx_1.proxy
proxy_2: /etc/nginx/conf.d/nginx_2.proxy
2. After nginx_vars.yaml variables defined in the file, the file can be introduced into the variable contained in the playbook
- hosts: 10.4.7.7
vars_files:
- ~/nginx_vars.yaml
tasks:
- name: task1
file:
path: "{{ nginx.proxy_1 }}"
state: touch
- name: task1
file:
path: "{{ nginx['proxy_2'] }}"
state: touch
Used in this embodiment vars_files
is introduced corresponding to the keyword variable file, and then using the variables in the file, the file keyword can introduce a variable may be introduced into a plurality of variable files, each file will need to be introduced -
at the beginning, vars
and vars_files
simultaneously use
When playbook reference variable, execution of the specified variable assignment
1. Create yaml
- hosts: all
tasks:
- name: Create New File
file: path=/tmp/{{ file_name }} state=touch
When parameters 2.playbook execution of --extra-vars
the specified assignment to a variable
$ ansible-playbook f1.yaml --extra-vars "file_name=jsn_extra-vars"
3, Playbook variable registration
register keyword can be stored in the specified output of the command variable to a custom in which there is a great use of that we need to judge the implementation of an action or a command, how to make the appropriate response processing (perform other ansible statement), also used it.
register simple and practical
1. The f1.yaml
document reads as follows
- hosts: 10.4.7.7
tasks:
- name: register vars
shell: hostname
register: System_Status
- name: display vars
debug: msg={{ System_Status.stdout }}
2. Perform playbook
[root@m01 ~]# ansible-playbook f1.yaml
PLAY [10.4.7.7] ****************************************************************
TASK [Gathering Facts] *********************************************************
ok: [10.4.7.7]
TASK [register vars] ***********************************************************
changed: [10.4.7.7]
TASK [display vars] ************************************************************
ok: [10.4.7.7] => {
"msg": "web01"
}
PLAY RECAP *********************************************************************
10.4.7.7 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- The first shell after the implementation, use register to get the data
System_Status
in System_Status
It is a key / value dictionary- debug output
System_Status.stdout的
specific content
register production example
Sda1 I need to determine whether there is, if there is a copy of the executable file
- hosts: 10.4.7.7
tasks:
- name: Create a register to represent the status if the /dev/sda1 exsited
shell: df -h | grep sda1
register: dev_sda1_result
ignore_errors: True
- name: Copy test.sh to all hosts
copy: src=/root/test.sh dest=/tmp/test.sh mode=755
when: dev_sda1_result.rc == 0
When df -h | grep sda1
the execution, will dev_sda1_result
write the results, and then dev_sda1_result.rc
rc is returned in command of the state, nonzero if unsuccessful, ignore_errors
the keyword must be set to True, or if the command is not successful, that is echo $?
not 0, then ansible statement following the statement will not be executed, causing the program to abort.
Multi-conditional register
Then when
with and
or or
combined judgment. For example, when one of two conditions for success:
- name: Copy test.sh to all hosts
copy: src=/root/test.sh dest=/tmp/test.sh mode=755
when: ( dev_sda1_result.rc == 0 ) or ( dev_sda2_result.rc == 0 )