1、ansible剧本
对于重复性的操作和部署(部署环境或者yum源),推荐使用剧本(剧本的格式=模块+固定格式)
1.1剧本格式
yaml是一种比较新的配置文件的格式
yaml格式: 以 .yaml或者 .yml结尾
书写格式注意事项:
同一层级的内容对齐
不同层级通过两个空格对齐
不能使用Tab键
[root@ansible ~]# cat test.yml
- hosts: web1.com #被管理的主机
remote_user: root #远程连接的用户
tasks: #执行的任务
- name: test template
template: src=/root/temp.txt dest=/tmp
- 在webserver主机创建/tmp/old.txt
- hosts: webserver
tasks:
- name: touch file
shell: touch /tmp/old.txt
[root@ansible ~]# ansible-playbook 01.yml -C #-C检查语法
[root@ansible ~]# ansible-playbook 01.yml #执行
查看
[root@ansible ~]# ansible webserver -m shell -a 'cat /tmp/old*'
web2.com | CHANGED | rc=0 >>
web1.com | CHANGED | rc=0 >>
192.168.10.24 | CHANGED | rc=0 >>
!!!如果觉得执行过程太枯燥可以下载 yum -y install cowsay
变成奶牛了,嘿嘿
如果不想看奶牛,可以修改/etc/ansible/ansible.cfg将cow_selection = random注释打开
变成小猫和老虎了
OK,我们继续
2.根据模块写剧本
1.定时任务剧本
[root@ansible ~]# ansible-playbook cron.yml -C 检查并执行
[root@ansible ~]# ansible-playbook cron.yml
[root@ansible ~]# ansible webserver -m shell -a “crontab -l” 查看
- 多个任务剧本创建
- hosts: webserver
tasks:
- name: groupadd
group: name=sss gid=1999 state=present
- name: useradd
user: name=ttt uid=1999 group=sss shell=/sbin/nologin create_home=no state=present
[root@ansible ~]# ansible-playbook add.yml -C 检查并执行
[root@ansible ~]# ansible-playbook add.yml
[root@ansible ~]# ansible webserver -a ‘id ttt’ 查看信息
web2.com | CHANGED | rc=0 >>
uid=1999(ttt) gid=1999(sss) 组=1999(sss)
web1.com | CHANGED | rc=0 >>
uid=1999(ttt) gid=1999(sss) 组=1999(sss)
192.168.10.24 | CHANGED | rc=0 >>
uid=1999(ttt) gid=1999(sss) 组=1999(sss)
- 安装zabbix-agent
- hosts: webserver
tasks:
- name: mkdir
file: path=/usr/local/ state=directory
- name: download zbx rpm
get_url: url="https://mirrors.aliyun.com/zabbix/zabbix/5.4/rhel/7/x86_64/zabbix-agent-5.4.8-1.el7.x86_64.rpm" dest=/usr/local/
- name: install zbx rpm
yum: name=/usr/local/zabbix-agent-5.4.8-1.el7.x86_64.rpm
state=present
- name: systemctl start && enable
systemd:
name=zabbix_agent
enabled=yes
state=started
[root@ansible ~]# ansible-playbook install.yml -C 检查并执行
[root@ansible ~]# ansible-playbook install.yml
3.ansible的变量
定义与使用变量:在命令行定义(不推荐,无法保存)
在剧本中定义变量
在专门文件中定义变量
ansible的内置变量(facts)
1.在剧本中和专门文件中定义变量
(1)将变量写到剧本中
- hosts: web1.com
vars:
file: /etc/hostname
tasks:
- name: print var
debug:
msg: 变量file的内容是 {{ file }}
*
## **注意:必须要用两对儿大括号引起来,如果变量在开头要加双引号**
*
(2)将变量写到文件中
[root@ansible ansible-script]# cat vars_file.yml
file: /etc/hostname
dir: /root
[root@ansible ansible-script]# cat var02.yml
- hosts: web1.com
vars_files: ./vars_file.yml
tasks:
- name: print var
debug:
msg: '{{ dir }} 变量file的内容是 {{ file }}' 注意:变量在开头一定要加双引号
(3)根据主机清单分组自动识别变量
创建group_vars的目录,并在其下创建与主机清单相同分组相同的目录,在目录中保存变量
[root@ansible ansible-script]# cat group_vars/webserver/vars.yml
port: 80
[root@ansible ansible-script]# cat var03.yml
- hosts: webserver
tasks:
- name: print var
debug:
msg: '当前服务的端口号是 {{ port }}'
- hosts: nfs
tasks:
- name: print var
debug:
msg: '当前服务的端口号是 {{ port }}'
变量定义小结
命令行定义变量(不推荐)
主机清单定义变量(不推荐)
在剧本中定义(方便、易于理解)仅当前剧本生效,其他剧本需要重新定义
变量文件 :变量都定义在一个文件中vars_file(方便管理维护)无法对主机分组
根据主机清单分组进行定义变量:创建目录group_vars/{webserver、nfs}/varsyml如上述
(定以后,剧本自动根据分组识别)
4.ansible的内置变量(facts)
facts变量:运行剧本时,ansible收集每台主机的信息
如果不想使用facts变量可以关闭,加速ansible
查看ansible的facts的信息
[root@ansible ansible-script]# ansible web1.com -m setup
[root@ansible ansible-script]# vim motd.j2 ###以下是常用的facts模块
#Welcom to 小z的拖孩儿 elastic linux
#hostname: {{ ansible_hostname }}
#eth0.ip: {{ ansible_default_ipv4.address }}
#内存大小: {{ ansible_memtotal_mb }}
#发行版本: {{ ansible_distribution }}
#cpu数量: {{ ansible_processor_vcpus }}
#cpu核心数:{{ ansible_processor_cores }}
[root@ansible ansible-script]# vim facts.motd.yml
- hosts: web1.com
tasks:
- name: fenfa /etc/motd
template: ##解析变量
src: ./motd.j2
dest: /etc/motd
backup: yes
登录测试:
5.ansible的register变量类型
先执行模块,将模块结果保存下来,将模块结构二次利用
- hosts: web1.com
tasks:
- name: get date
command: date +%F_%w
register: time
- name: touch 时间命名文件
file:
path: "/tmp/{{ time.stdout }}.txt"
state: touch
ansible的变量及应用场景
用户自定义
用户自己定义|:剧本中通过vars定义变量、通过group_vars定义|
ansible内置facts变量 :获取系统基本信息,使用它会导致剧本解析速度变慢可以在 - hosts下写gather_facts=false
register变量 $()``:寄存器变量注册,临时变量,通过{{ 变量名.stdout }}调用。