定义变量的三种方式:
1.在play中定义变量(测试 变量少)
2.在文件中定义变量(当有多个playbook调用相同变量时推荐使用)
3.在invertory清单中定义变量(不推荐)
4.官网推荐定义方式
第一种方法: 在play中定义变量:
[root@ansible ansible]# cat yum.yml
- hosts: web01
vars:
- package: wget # 定义变量 变量的名称自定义
tasks:
- name: Install Servers
yum:
name: "{{ package }}" # 调用变量
state: absent
以列表的方式定义变量:
[root@ansible ansible]# cat yum.yml
- hosts: web01
vars:
- pk1: wget
- pk2: lrzsz
- pk3: tree
tasks:
- name: Install Servers
yum:
name:
- "{{ pk1 }}"
- "{{ pk2 }}"
- "{{ pk3 }}"
state: absent
使用类似数组的方式定义变量:
[root@ansible ansible]# cat yum.yml
- hosts: web01
vars:
pk:
- lrzsz
- tree
- wget
tasks:
- name: Install Servers
yum:
name: "{{ pk }}"
state: present
注意: 如果是路径中使用变量 则不需要使用双引号 ""
[root@ansible ansible]# cat yum.yml
- hosts: web01
vars:
- code_ip: 10.0.0.7
- code_dir: web01
tasks:
- name: Install Servers
file:
path: /root/{{ code_dir }}_{{ code_ip }}
state: directory
第二种定义变量的方式: 在文件中定义(文件名称自定义)
优势: 可以在任意的playbook中调用变量
1.定义变量
[root@ansible ansible]# cat vars.yml
pk1: wget
pk2: tree
pk2: lrzsz
2.在play中引用变量
[root@ansible ansible]# cat yum.yml
- hosts: web01
vars_files: vars.yml # 引用变量的文件
tasks:
- name: Install Servers
yum:
name:
- "{{ pk1 }}"
- "{{ pk3 }}"
- "{{ pk2 }}"
state: absent
调用多个变量文件:
[root@ansible ansible]# cat vars.yml
pk1: wget
pk2: tree
pk3: lrzsz
[root@ansible ansible]# cat vars1.yml
test:
- httpd
- tree
- lrzsz
[root@ansible ansible]# cat yum.yml
- hosts: web01
vars_files: # 引用多个变量文件
- vars.yml
- vars1.yml
tasks:
- name: Install Servers
yum:
name:
- "{{ pk1 }}"
- "{{ pk3 }}"
- "{{ pk2 }}"
state: present
- name: Remove httpd lrzsz tree
yum:
name: "{{ test }}"
state: absent
在主机清单中定义变量: 了解
[root@ansible ansible]# cat /etc/ansible/hosts
[webs:vars]
pk1=wget
pk2=httpd
pk3=tree
变量调用:
[root@ansible ansible]# cat yum.yml
- hosts: web01
tasks:
- name: Install Servers
yum:
name:
- "{{ pk1 }}"
- "{{ pk3 }}"
- "{{ pk2 }}"
state: present
[all:vars] # 为所有的主机定义变量
pk1=wget
pk2=httpd
pk3=tree
官网推荐的变量定义方式:
创建以组名称命名的目录: 给组定义变量
group_vars/ 目录名称必须是group_vars
vim webs # 为webs组定义变量
创建以主机名命名的文件: 给主机定义变量
host_vars/ 目录名称必须是host_vars
vim web01 # 只给单台定义变量
给web01定义变量:
[root@ansible ansible]#
[root@ansible ansible]# mkdir host_vars
[root@ansible ansible]# mkdir group_vars
[root@ansible ansible]# cat host_vars/web01
pk1: httpd
pk2: lrzsz
pk3: tree
调用阶段:
[root@ansible ansible]# cat yum.yml
- hosts: web01
tasks:
- name: Install Servers
yum:
name:
- "{{ pk1 }}"
- "{{ pk3 }}"
- "{{ pk2 }}"
state: present
为组定义变量: 变量名称以组名定义
[root@ansible ansible]# cat group_vars/webs
pk1: lrzsz
pk2: httpd
pk3: tree
调用变量:
[root@ansible ansible]# cat yum.yml
- hosts: webs
tasks:
- name: Install Servers
yum:
name:
- "{{ pk1 }}"
- "{{ pk3 }}"
- "{{ pk2 }}"
state: present
如果想对所有的主机进行定义使用组下面all定义:
[root@ansible ansible]# cat group_vars/all
pk1: httpd
pk2: lrzsz
pk3: tree
变量注册: [root@ansible ansible]# cat register.yml - hosts: web01 tasks: - name: Test Register Vars shell: 'ls -l /root/' # 将shell命令执行结果赋值给list_dir变量 register: list_dir - name: Print List_dir Vars debug: msg: "{{ list_dir }}" # 使用debug模块将变量中的值输出 Nginx返回结果: [root@ansible ansible]# cat register.yml - hosts: web01 tasks: - name: Test Register Vars shell: 'nginx -t' ignore_errors: yes # 忽略错误 register: nginx_result - name: Print nginx_result debug: msg: "{{ nginx_result.stderr_lines }}" 调用客户端的变量生成不同的配置文件: [root@ansible ansible]# cat vars.yml - hosts: webs tasks: - name: Create Hostname Dir IP file: path: /root/{{ ansible_hostname }}_{{ ansible_default_ipv4.address }} state: directory
template模块: 配置文件中可以识别变量 和copy模块相同(copy只识别字符串 不识别变量)
- name: Configure NFS Server
template:
src: exports
dest: /etc/exports
小结:
变量的定义:
第一种: 在play中定义变量
- hosts:
vars:
- pk1: wget
- pk2: tree
第二种: 在文件中定义变量
vim vars.yml
pk1: wget
pk2: tree
在play中调用引用文件:
- hosts:
vars_files: vars.yml
第三种: 官网推荐的定义方式
在当前目录创建:
group_vars
host_vars
在组目录下定义组的变量
在主机目录下定义主机的变量
vim group_vars/webs
pk1: tree
pk2: lrsz
vim host_vars/web01
pk1: tree
主机清单中定义变量: 了解
vim /etc/ansible/hosts
[webs]
pk1=tree
pk2=lrzsz
变量注册: register 将结果输出显示到屏幕
shell 'ls -l'
register: ls_result
调用变量:
debug:
msg: "{{ ls_result }}"
调用变量: 如果变量在task文件中则直接调用即可
如果变量在配置文件中需要将copy模块修改为tmpelate模块可以识别变量
2.Ansible-when判断
1.根据系统版本安装不同的软件
[root@ansible ansible]# cat http.yml
- hosts: webs
tasks:
- name: Install CentOS Httpd
yum:
name: httpd
state: present
when: ansible_distribution == "CentOS"
2.使用and并且
[root@ansible ansible]# cat http.yml
- hosts: webs
tasks:
- name: Install CentOS Httpd
yum:
name: httpd
state: absent
when: ( ansible_distribution == "CentOS" and ansible_hostname == "web01" )
[root@ansible ansible]# cat http.yml
- hosts: webs
tasks:
- name: Install CentOS Httpd
yum:
name: httpd
state: present
when:
- ansible_distribution == "CentOS" # 判断版本为centos
- ansible_hostname == "web01" # 判断主机名称为web01
或者关系:
[root@ansible ansible]# cat http.yml
- hosts: webs
tasks:
- name: Install CentOS Httpd
yum:
name: httpd
state: present
when: ( ansible_hostname == "web02" or ansible_default_ipv4.address == "10.0.0.8" )
通过nginx返回结果来判断是否让nginx重启
[root@ansible ansible]# cat when.yml
- hosts: web01
tasks:
- name: Configure Nginx file
copy:
src: nginx.conf
dest: /etc/nginx/nginx.conf
- name: test nginx file
shell: 'nginx -t'
ignore_errors: yes
register: ngx_re
- name: print ngx_re # 调试完成后可以取消debug输出
debug:
msg: "{{ ngx_re.stderr_lines }}"
- name: Restart Nginx Server
systemd:
name: nginx
state: restarted
when: ngx_re.stderr_lines is search "ok"
web服务器和nfs都需要安装nfs-utils
但是配置文件和启动nfs只在nfs服务器上执行
[root@ansible ansible]# cat /etc/ansible/hosts
[nfs]
10.0.0.31
[webs]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8
[lnmp:children]
nfs
webs
[root@ansible ansible]# cat nfs.yml
- hosts: lnmp
vars:
- nfs_dir: /code/zh
- nfs_ip: 172.16.1.0/24
tasks:
- name: Install nfs-utils Server
yum:
name: nfs-utils
state: present
- name: Configure NFS Server
template:
src: exports
dest: /etc/exports
when: ansible_hostname is match "nfs"
- name: Create Group www
group:
name: www
gid: 666
- name: Create www User
user:
name: www
uid: 666
group: www
shell: /sbin/nologin
create_home: false
- name: Create /code/blog dir
file:
path: "{{ nfs_dir }}"
state: directory
owner: www
group: www
- name: Start NFS Server
systemd:
name: nfs
state: started
enabled: yes
when: ansible_hostname is match "nfs"
3.Ansible循环语句
1.启动多个服务
[root@ansible ansible]# cat server.yml
- hosts: web01
tasks:
- name: Start Nginx PHP-FPM
systemd:
name: "{{ item }}"
state: started
loop:
- nginx
- php-fpm
2.创建多个用户
[root@ansible ansible]# cat user.yml
- hosts: web01
tasks:
- name: Create www baimei User
user:
name: "{{ item }}"
state: present
loop:
- www
- baimei
删除两个用户:
[root@ansible ansible]# cat user.yml
- hosts: web01
tasks:
- name: Create www baimei User
user:
name: "{{ item }}"
state: absent
loop:
- www
- baimei
使用循环创建多个用户:
注意: item的子值可以不使用单引号
[root@ansible ansible]# cat user.yml
- hosts: web01
tasks:
- name: Create www baimei User
user:
name: "{{ item.name }}"
uid: "{{ item.uid }}"
group: "{{ item.group }}"
shell: "{{ item.shell }}"
create_home: "{{ item.home }}"
state: present
loop:
- { name: 'www',uid: '666' ,group: 'www', shell: '/sbin/nologin',home: 'false' }
- { name: 'baimei',uid: '667' ,group: 'root', shell: '/bin/bash',home: 'true'}
创建多个文件:
[root@ansible ansible]# cat touch.yml
- hosts: web01
tasks:
- name: touch 1.txt 2.txt
file:
path: "{{ item.src }}"
owner: "{{ item.owner }}"
group: "{{ item.group }}"
mode: "{{ item.mode }}"
state: touch
loop:
- { src: 1.txt,owner: www ,group: www,mode: '0600'}
- { src: 2.txt,owner: root ,group: root,mode: '0755'}
知识点小结:
重点: 变量定义
变量注册
when判断
欢迎来撩 : 汇总all