1 loop循环模块
https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html
1.需求:创建2个目录/data和/backup
[root@m01 /server/scripts]# cat for.yml
- hosts: backup
tasks:
- name: create directory /data
file:
path: /data
state: directory
- name: create directory /backup
file:
path: /backup
state: directory
2.使用loop循环完成
[root@m01 /server/scripts]# cat loop.yaml
- hosts: backup
tasks:
- name: create directory /data & /backup
file:
path: "{{ item }}"
state: directory
loop:
- /data
- /backup
3.如果两个目录的权限不一样 #多参数循环
[root@m01 /server/scripts]# cat loop.yaml
- hosts: backup
tasks:
- name: create directory /data & /backup
file:
path: "{{ item.path }}"
mode: "{{ item.mode }}"
state: directory
loop:
- { path: '/data', mode: '600' }
- { path: '/backup', mode: '700' }
4.创建两个用户test1和test2,创建两个目录/data1和/data2,/data1目录用户是test1,/data2目录用户是test2
[root@m01 /server/scripts]# cat test01.yaml
- hosts: backup
tasks:
- name: add user test1 test2
user:
name: "{{ item }}"
loop:
- test1
- test2
- name: create /data1 /data2
file:
path: "{{ item.path }}"
owner: "{{ item.owner }}"
state: directory
loop:
- { path: '/data1', owner: 'test1' }
- { path: '/data2', owner: 'test2' }
loop循环小结:
1.loop和模块对齐。
2.多变量循环要完全遵循格式,不能少了空格。
2 变量
1.vars和hosts对齐
2.变量退2格
3.在yml文件中定义的变量优先级要比在/etc/ansible/hosts文件中定义的变量要高。
4.https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html
1.自定义变量
[root@m01 /server/scripts]# cat vars.yml
- hosts: backup
vars:
dest_path: /oldboy
src_path: /server/scripts/test01.yaml
tasks:
- name: 01-mkdir
file:
path: "{{ dest_path }}"
state: directory
- name: 02-copy
copy:
src: "{{ src_path }}"
dest: "{{ dest_path }}"
2.内置变量
[root@m01 /server/scripts]# cat vars.yml
- hosts: backup
vars:
dest_path: /oldboy
src_path: /server/scripts/test01.yaml
tasks:
- name: 01-mkdir
file:
path: "{{ dest_path }}"
state: directory
- name: 02-copy
copy:
src: "{{ src_path }}"
dest: "{{ dest_path }}"
- name: 03-get ip address
shell: "echo {{ ansible_facts.eth1.ipv4.address }} >{{ dest_path }}/ip_eth1.txt"
3.其他ansible内置变量
ansible_facts.eth0.ipv4.address
ansible_facts.eth1.ipv4.address
ansible_nodename 节点名字
ansible_form_factor 服务器类型
ansible_virtualization_role 虚拟机角色(宿主机或者虚拟机)
ansible_virtualization_type 虚拟机类型(kvm)
ansible_system_vendor 供应商(Dell)
ansible_product_name 产品型号(PowerEdge R530)
ansible_product_serial 序列号(sn)
ansible_machine 计算机架构(x86_64)
ansible_bios_version BIOS版本
ansible_system 操作系统类型(linux)
ansible_os_family 操作系统家族(RedHat)
ansible_distribution 操作系统发行版(CentOS)
ansible_distribution_major_version 操作系统发行版主版本号(7)
ansible_distribution_release 操作系统发行版代号(core)
ansible_distribution_version 操作系统发行版本号(7.3.1611)
ansible_architecture 体系(x86_64)
ansible_kernel 操作系统内核版本号
ansible_userspace_architecture 用户模式体系(x86_64)
ansible_userspace_bits 用户模式位数
ansible_pkg_mgr 软件包管理器
ansible_selinux.status selinux状态
#--------------------------------------------
ansible_processor CPU产品名称
ansible_processor_count CPU数量
ansible_processor_cores 单颗CPU核心数量
ansible_processor_threads_per_core 每个核心线程数量
ansible_processor_vcpus CPU核心总数
ansible_memtotal_mb 内存空间
ansible_swaptotal_mb 交换空间
ansible_fqdn 主机的域名
ansible_default_ipv4.interface 默认网卡
ansible_default_ipv4.address 默认IP地址
ansible_default_ipv4.gateway 默认网关
********* json 格式 ********
ansible_devices 硬盘设备名
ansible_devices.vendor 硬盘供应商
ansible_devices.model 硬盘整列卡型号
ansible_devices.host 硬盘整列卡控制器
ansible_devices.size 设备存储空间
********* json 格式 ********
ansible_interfaces 网卡
ansible_{interfaces}.ipv4.address 网卡IP地址
ansible_{interfaces}.ipv6.0.address 网卡IPv6地址
ansible_{interfaces}.macaddress 网卡mac地址
3 注册变量register模块
不常用!注册一个变量名为ip_address,存储shell模块的输出,再使用debug模块输出,想要输出什么内容就用变量ip_address.xxx,进输出xxx,本案例中为仅输出IP地址。不加点则输出内容如下图:
[root@m01 /server/scripts]# cat register.yaml
- hosts: backup
vars:
dest_path: /oldboy
src_path: /server/scripts/test01.yaml
tasks:
- name: 01-mkdir
file:
path: "{{ dest_path }}"
state: directory
- name: 02-copy
copy:
src: "{{ src_path }}"
dest: "{{ dest_path }}"
- name: 03-get ip address
shell: "echo {{ ansible_facts.eth1.ipv4.address }} >{{ dest_path }}/ip_eth1.txt"
- name: 04-cat
shell: "cat {{ dest_path }}/ip_eth1.txt"
register: ip_address
- name: 05-echo register vars
debug:
msg: "{{ ip_address.stdout_lines }}"
register用途:
1.如果一个服务的配置文件发生变化,则重启服务;反之则不重启。将配置文件的状态注册成一个变量conf_status,当配置文件状态发生改变,服务重启。不改变,不重启。
[root@m01 /server/scripts]# cat register_restarted.yaml
- hosts: backup
tasks:
- name: 01-copy rsyncd conf file
copy:
src: /server/conf/rsyncd.conf
dest: /etc/
register: conf_status
- name: 02-start rsyncd
service:
name: rsyncd
state: started
- name: 03-restart rsyncd
service:
name: rsyncd
state: restarted
when: conf_status.changed
2.如果发送一个压缩包到目标主机,可以将发送包的状态注册成变量,如果目标主机上存在这个包,状态不改变,不解压这个包。如果目标主机上没有这个包,状态改变,发过去解压缩。
4 服务管理handlers
https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html?highlight=handlers#handlers-running-operations-on-change
如果服务的配置文件发生变化就重启服务,否则什么都不操作。notify模块监控配置文件发送的状态,handlers用来出来监控的变化,当状态改变,则对服务做对用的启停。
[root@m01 /server/scripts]# cat handlers.yml
- hosts: backup
tasks:
- name: 01-if rsyncd conf changed,then restart rsyncd
copy:
src: /server/conf/rsyncd.conf
dest: /etc/
notify: Restart_Rsyncd_Server
handlers:
- name: Restart_Rsyncd_Server
service:
name: rsyncd
state: restarted
5 打标签
使用情景:从我们指定的任务开始执行,而不是从头到尾执行一遍
调用标签:
1.打印出playbook里要执行的所有标签
ansible-playbook --list-tags tags2.yml
2.指定运行某个标签
ansible-playbook -t tags_name tags2.yml
3.指定运行多个标签,使用逗号隔开
ansible-playbook -t 05-create-passwd,06-create-backup tags2.yml
4.指定不运行某个标签
ansible-playbook --skip-tags=05-create-passwd tags2.yml
5.指定不运行多个标签
ansible-playbook --skip-tags=05-create-passwd,06-create-backup tags2.yml
6 选择任务
1.查看tasks列表
ansible-playbook --list-task auto_deploy.yml
2.从第几个tasks开始往下执行(可以不加等号=)
ansible-playbook --start-at-task=03_mkdir_data auto_deploy.ym
7 运行检查规范
00.检查剧本拼写规范
ansible-playbook --syntax-check check.yaml
01.检查这个任务执行的主机对象
ansible-playbook --list-hosts check.yaml
02.检查这个剧本需要执行哪些任务
ansible-playbook --list-tasks check.yaml
03.检查这个剧本执行哪些tag
ansible-playbook --list-tags check.yaml
04.模拟执行剧本
ansible-playbook -C check.yaml