-
Ansible变量可以存储一些值,而这些值在一个Ansible项目中,是可以复用的;
-
Ansible变量可以存储一些值,而这些值在一个Ansible项目中,是可以复用的;
-
Ansible变量可以存储一些值,而这些值在一个Ansible项目中,是可以复用的;
-
Ansible中变量的命名规则是:__________________________________________________;
-
Ansible中变量的命名规则是:__________________________________________________;
-
Ansible中变量的命名规则是:__________________________________________________;
-
在playbook当中使用变量时,应该____________________________________________;
-
在playbook当中使用变量时,应该____________________________________________;
-
在playbook当中使用变量时,应该____________________________________________;
-
在一个play中使用变量user=joe,创建一个uid为1010的新用户;
-
错误示范:inventory文件的内容为:
192.168.197.128
192.168.197.130
[centos8]
192.168.197.128
[centos7]
192.168.197.130
[centos:children]
centos8
centos7
[centos8:vars]
user=Jorge Jorge
而此时playbook的内容为:
- name: Play1
hosts: all
tasks:
- name: Task1
ansible.builtin.user:
name: "{{ user }}"
uid: 1013
...
请问,如果此时用ansible-playbook命令运行这个playbook文件,都会成功嘛?为什么?
-
ansible中变量的优先级是什么?
-
ansible中变量的优先级是什么?
-
ansible中变量的优先级是什么?
-
写一个playbook,使用ansible.builtin.user模块,问如何得知这个模块的执行结果呢?
-
使用Ansible变量,写一个playbook,部署并启动Apache HTTPD服务。
-
写一个playbook,确保httpd服务已开启并且开机自启,并输出模块的执行结果;
-
写一个playbook,确保firewalld服务已开启并且开机自启,并输出模块的执行结果;
-
写一个playbook,确保tree软件的最新版已经被安装,并输出模块的执行结果;
-
写一个playbook,使用到./vars/AZ20240526.yaml与./vars/BY20240526.yaml这两份yaml文件中定义的变量;
-
写一个playbook,使用到./vars/CX20240526.yaml与./vars/DW20240526.yaml这两份yaml文件中定义的变量;
-
写一个playbook,使用到./vars/EV20240526.yaml与./vars/FU20240526.yaml这两份yaml文件中定义的变量;
-
必须以字母开头,并且只能包含字母、数字、下划线
-
必须以字母开头,并且只能包含字母、数字、下划线
-
必须以字母开头,并且只能包含字母、数字、下划线
-
用双大括号包裹起来,此外当一个变量被用来作为值的第一个元素起始时,使用引号是强制性的。
-
用双大括号包裹起来,此外当一个变量被用来作为值的第一个元素起始时,使用引号是强制性的。
-
用双大括号包裹起来,此外当一个变量被用来作为值的第一个元素起始时,使用引号是强制性的。
-
/* Z20240526.yaml */
- name: Play1
hosts: all
vars:
user: joe
tasks:
- name: Task1
ansible.builtin.user:
name: "{{ user }}"
uid: 1010
...
-
不是的,user变量仅针对centos8组生效,也就是仅针对192.168.197.128生效,不对centos7组生效,也就是不针对192.168.197.130生效,所以对于192.168.197.130会失效
-
命令行中指定的变量>playbook中vars和vars_files指定的playbook变量>host_vars目录中指定的playbook变量>inventory中为host指定的playbook变量>group_vars目录中指定的playbook变量>inventory中为group指定的playbook变量
-
命令行中指定的变量>playbook中vars和vars_files指定的playbook变量>host_vars目录中指定的playbook变量>inventory中为host指定的playbook变量>group_vars目录中指定的playbook变量>inventory中为group指定的playbook变量
-
命令行中指定的变量>playbook中vars和vars_files指定的playbook变量>host_vars目录中指定的playbook变量>inventory中为host指定的playbook变量>group_vars目录中指定的playbook变量>inventory中为group指定的playbook变量
-
/* B20240527.yaml */
- name: Play1
hosts: all
tasks:
- name: Task1
ansible.builtin.package:
name: httpd
state: latest
register: install_result
- name: Task2
ansible.builtin.debug:
var: install_result
...
- /* C20240527.yaml */
- name: Play1
hosts: centos
vars:
web_pkg: httpd
web_service: httpd
firewall_pkg: firewalld
firewall_service: firewalld
rule: http
tasks:
- name: Task1
ansible.builtin.package:
name: "{{ firewall_pkg }}"
state: latest
- name: Task2
ansible.builtin.service:
name: "{{ firewall_service }}"
state: started
enabled: true
- name: Task3
ansible.posix.firewalld:
service: "{{ rule }}"
permanent: true
immediate: yes
state: enabled
- name: Task4
ansible.builtin.package:
name: "{{ web_pkg }}"
state: latest
- name: Task5
ansible.builtin.service:
name: "{{ web_service }}"
state: started
enabled: true
- name: Task6
ansible.builtin.copy:
content: Welcome to the Winnder King!
dest: /var/www/html/index.html
owner: root
group: root
mode: '0644'
- name: Play2
hosts: workstation
remote_user: rhce
become: false
tasks:
- name: Task1
ansible.builtin.uri:
status_code: 200
- name: Task2
ansible.builtin.uri:
status_code: 200
...
- /* D20240527.yaml */
- name: Play1
hosts: all
tasks:
- name: Task1
ansible.builtin.service:
name: httpd
state: started
enabled: true
register: Task1result
- name: Task2
ansible.builtin.debug:
var: Task1result
...
- /* E20240527.yaml */
- name: Play1
hosts: all
tasks:
- name: Task1
ansible.builtin.service:
name: firewalld
state: started
enabled: true
register: Task1result
- name: Task2
ansible.builtin.debug:
var: Task1result
...
- /* F20240527.yaml */
- name: Play1
hosts: all
tasks:
- name: Task1
ansible.builtin.package:
name: tree
state: latest
register: Task1result
- name: Task2
ansible.builtin.debug:
var: Task1result
...
- name: Play1
hosts: all
vars_files:
-
AZ20240526.yaml
-
BY20240526.yaml
tasks:
- name: Task1
ansible.builtin.debug:
var: az
- name: Task2
ansible.builtin.debug:
var: by
...
- name: Play1
hosts: all
vars_files:
-
CX20240526.yaml
-
DW20240526.yaml
tasks:
- name: Task1
ansible.builtin.debug:
var: cx
- name: Task2
ansible.builtin.debug:
var: dw
...
- name: Play1
hosts: all
vars_files:
-
EV20240526.yaml
-
FU20240526.yaml
tasks:
- name: Task1
ansible.builtin.debug:
var: ev
- name: Task2
ansible.builtin.debug:
var: fu
...