firewalld模块:
使用firewalld模块可以配置防火墙策略。
1. [root@control ~]# vim ~/ansible/firewall.yml
2. ---
3. - hosts: test #hosts定义需要远程的主机
4. tasks: #tasks定义需要执行哪些任务
5. - name: install firewalld. #name为第一个任务定义描述信息
6. yum: #第一个任务调用yum模块安装软件
7. name: firewalld #需要安装的软件名称为firewalld
8. state: present #state等于present代表安装软件
9. - name: run firewalld. #定义第二个任务的描述信息
10. service: #第二个任务调用service模块启动服务
11. name: firewalld #启动的服务名称为firewalld
12. state: started #state等于started代表启动服务
13. enabled: yes #enabled等于yes是设置服务为开机自启动
14. - name: set firewalld rule. #第三个任务的描述信息
15. firewalld: #第三个任务调用firewalld模块设置防火墙规则
16. port: 80/tcp #在防火墙规则中添加一个放行tcp,80端口的规则
17. permanent: yes #permaenent 是设置永久规则
18. immediate: yes #immediate 是让规则立刻生效
19. state: enabled #state等于enabled是添加防火墙规则
20. #最终:在默认zone中添加一条放行80端口的规则
template模块:
copy模块可以将一个文件拷贝给远程主机,但是如果希望每个拷贝的文件内容都不一样呢?
如何给所有web主机拷贝index.html内容是各自的IP地址?
Ansible可以利用Jinja2模板引擎读取变量,之前在playbook中调用变量,也是Jinja2的功能,Jinja2模块的表达式包含在分隔符"{{ }}"内
这里,我们给webserver主机拷贝首页,要求每个主机内容不同。
1. [root@control ansible]# mkdir ~/ansible/template
3. [root@control ansible]# vim ~/ansible/template/index.html
4. Welcome to {{ansible_hostname}} on {{ ansible_eth0.ipv4.address }}.
5. #注意网卡名称根据实际情况填写,不可以完全照抄,不知道网卡名可以通过ip a s查询!
6. #{{ansible_hostname}}和{{ ansible_eth0.ipv4.address }}是ansible自动的facts变量。
将网页模板文件拷贝到远程主机。
1. [root@control ansible]# vim ~/ansible/template.yml
2. ---
3. - hosts: webserver
4. tasks:
5. - name: use template copy index.html to webserver.
6. template:
7. src: ~/ansible/template/index.html
8. dest: /tmp/index.html
9. #hosts定义需要远程的目标主机是谁;tasks定义需要执行的任务是什么
10. #- name定义任务的描述信息;任务需要调用的模块是template模块
11. #template模块需要两个参数,src指定需要拷贝的源文件,dest指定需要拷贝的目标位置
12. #src: ~/ansible/template/index.html是上面创建的文件,文件中包含变量
13. #dest: /tmp/index.html拷贝到目标主机放在/tmp目录下
高级语法应用模块:error 和 handlers
如果一个剧本里面有20个任务,执行到第3个时失败,则不再往下执行。此时可以使用ignore_errors选项
默认ansible在遇到error会立刻停止playbook,使用ignore_errors可以忽略错误,继续后续的任务。
root@control ansible]# vim ~/ansible/error.yml
2. ---
3. - hosts: test
4. tasks:
5. - name: start a service that does not exist.
6. service:
7. name: hehe #注意:没有这个服务(启动一个不存在的服务),如何让接下来任务接着运行
8. state: started
9. - name: touch a file.
10. file:
11. path: /tmp/service.txt
12. state: touch
13. [root@control ansible]# vim ~/ansible/error.yml
14. ---
15. - hosts: test
16. tasks:
17. - name: start a service that does not exist.
18. service:
19. name: hehe
20. state: started
21. ignore_errors: true #针对某一个任务忽略错误(ignore_errors是关键词)
22. - name: touch a file.
23. file:
24. path: /tmp/service.txt
25. state: touch
26. [root@control ansible]# cat ~/ansible/error.yml
27. ---
28. - hosts: test
29. ignore_errors: true #针对playbook全局忽略错误
30. tasks:
31. - name: start a service that does not exist.
32. service:
33. name: hehe
34. state: started
35. - name: touch a file.
36. file:
37. path: /tmp/service.txt
38. state: touch
handlers:
在剧本中tasks用来定义任务(一定会执行),handlers也可以定义任务(不一定执行),handlers任务要想执行必须要被别人触发才能执行。
1. 实例草稿:
2. ---
3. - hosts: test
4. tasks:
5. - 任务1
6. notify:任务5
7. - 任务2
8. handlers:
9. - 任务5
10. - 任务6
可以通过handlers定义一组任务,仅当某个任务触发(notify)handlers时才执行相应的任务,如果有多个notify触发执行handlers任务,也仅执行一次。仅当任务的执行状态为changed时handlers任务才执行,handlers任务在所有其他任务都执行后才执行。
下面编写一个通过notify触发执行handlers任务的案例。
1. [root@control ansible]# vim ~/ansible/handlers.yml
2. ---
3. - hosts: test
4. tasks:
5. - name: create directory. #多次执行playbook该任务状态不再是changed
6. file: #调用file模块创建目录
7. path: /tmp/parents/subdir/ #需要创建的具体目录名称
8. state: directory #state等于directory代表创建目录
9. notify: touch file #notify后面名称必须和handlers中的任务名称一致
10. handlers: #通过handlers再定义一组任务
11. - name: touch file #给任务写描述信息(任务的名字,名字可以任意)
12. file: #调用file模块创建文件
13. path: /tmp/parents/subdir/new.txt #需要创建的文件名
14. state: touch #state等于touch代表创建文件
15.
16. #备注:仅当file模块执行成功,
17. #并且状态为changed时才会通过notify触发执行handlers下面的任务,
18. #所以多次执行该剧本时,handlers任务不会被重复执行,
19. #notity后面的名称必须和handlers下面name定义的任务名称一致(名称可以任意)。
when条件判断:
when可以定义判断条件,条件为真时才执行某个任务。
常见条件操作符有:==、!=、>、>=、<、<=。
多个条件可以使用and(并且)或or(或者)分割,when表达式中调用变量不要使用{{ }}。
下面编写Playbook,远程主机剩余内存不足700M则关闭NetworkManager服务
1. [root@control ansible]# vim ~/ansible/when_1.yml
2. ---
3. - hosts: test
4. tasks:
5. - name: check memory size.
6. service:
7. name: NetworkManager
8. state: stopped
9. when: ansible_memfree_mb < 700 #可以放在service之前
10. #被管理端主机剩余内存不足700M则关闭NetworkManager服务(也可以关闭别的不需要的服务)
11. #ansible_memfree_mb这个是ansible自带的facts变量,代表剩余内存的容量。
下面再编写一个Playbook,判断操作系统是RedHat8则创建测试文件。YAML的语法格式中>支持多行输入,但不保留换行符。
1. [root@control ansible]# vim ~/ansible/when_2.yml
2. ---
3. - hosts: test
4. tasks:
5. - name: touch a file
6. file:
7. path: /tmp/when.txt
8. state: touch
9. when: >
10. ansible_distribution == "RedHat"
11. and
12. ansible_distribution_major_version == "8"
13. #判断操作系统是RedHat8则创建测试文件
14. #YAML的语法格式中>支持多行输入,但不保留换行符(计算机会认为实际是一行内容),| 符 会保留换行符,系统认为是多行内容。
15. #ansible_distribution和ansible_distribution_major_version都是自带的facts变量
16. #可以使用setup模块查看这些变量
block任务块:
如果我们需要当条件满足时执行N个任务,我们可以给N个任务后面都加when判断(但是很麻烦),此时可以使用block定义一个任务块,当条件满足时执行整个任务块.
任务块就是把一组任务合并为一个任务组,使用block语句可以将多个任务合并为一个任务组。
1. [root@control ansible]# vim ~/ansible/block_1.yml
2. ---
3. - hosts: test
4. tasks:
5. - name: define a group of tasks. #定义一组任务 block
6. block: #block是关键词,定义任务组
7. - name: install httpd #任务组中的第一个任务
8. yum: #调用yum模块安装httpd软件包
9. name: httpd
10. state: present
11. - name: start httpd #任务组中的第二个任务
12. service: #调用service模块启动httpd服务
13. name: httpd
14. state: started
15. when: ansible_distribution == "RedHat" #仅当条件满足再执行任务组
16. #注意:when和block是对齐的,他们在一个级别,当条件满足时要执行的是任务组(不是某一个任务)
17. #判断条件是看远程的目标主机使用的Linux发行版本是否是RedHat.
rescue和always:
对于block任务块,我们可以使用rescue语句定义在block任务执行失败时要执行的其他任务,还可以使用always语句定义无论block任务是否成功,都要执行的任务。
1. [root@control ansible]# vim ~/ansible/block_2.yml
2. ---
3. - hosts: test
4. tasks:
5. - block:
6. - name: touch a file test1.txt
7. file:
8. name: /tmp/test1.txt #如果修改为/tmp/xyz/test1.txt就无法创建成功
9. state: touch
10. rescue: #如果上一个任务失败就执行这个任务
11. - name: touch a file test2.txt
12. file:
13. name: /tmp/test2.txt
14. state: touch
15. always: #anyway,都执行这个任务
16. - name: touch a file test3.txt
17. file:
18. name: /tmp/test3.txt
19. state: touch
20. #默认在/tmp/目录下创建test1.txt会成功,所以不执行rescue(创建test2.txt)
21. #如果我们把block中的任务改为创建/tmp/xyz/test1.txt(因为没有xyz目录所以会失败)
22. #当block默认任务失败时就执行rescue任务(创建test2.txt)
23. #但是不管block任务是否成功都会执行always任务(创建test3.txt
loop循环:
相同模块需要反复被执行怎么处理?使用loop循环可以避免重复。
编写Playbook,循环创建目录。
1. [root@control ansible]# vim ~/ansible/simple_loop.yml
2. ---
3. - hosts: test
4. tasks:
5. - name: mkdir multi directory.
6. file:
7. path=/tmp/{{item}} #注意,item是关键字,调用loop循环的值
8. state=directory
9. loop: #loop是关键词,定义循环的值,下面是具体的值
10. - School
11. - Legend
12. - Life
13. #最终在/tmp目录下创建三个子目录.file模块被反复执行了三次。
14. #mkdir /tmp/School; mkdir /tmp/Legend; mkdir /tmp/Life
编写Playbook,循环创建用户并设置密码。
1. [root@control ansible]# vim ~/ansible/complex_loop.yml
2. ---
3. - hosts: test
4. tasks:
5. - name: create multi user.
6. user:
7. name: "{{item.iname}}"
8. password: "{{item.ipass | password_hash('sha512')}}"
9. loop:
10. - { iname: 'term', ipass: '123456' }
11. - { iname: 'amy' , ipass: '654321' }
12. #loop循环第一次调用user模块创建用户,user模块创建用户会读取loop里面的第一个值.
13. #loop第一个值里面有两个子值,iname和ipass
14. #创建用户item.iname就是loop第一个值里面的iname=term
15. #修改密码item.ipass就是loop第一个值里面的ipass=123456
16.
17. #loop循环第二次调用user模块创建用户,user模块创建用户会读取loop里面的第二个值.
18. #loop第二个值里面有两个子值,iname和ipass
19. #创建用户item.iname就是loop第二个值里面的iname=amy
20. #修改密码item.ipass就是loop第二个值里面的ipass=654321
加密敏感数据
使用ansible-vault管理敏感数据
- encrypt(加密)、decrypt(解密)、view(查看)
- [root@control ansible]# echo 123456 > data.txt #新建测试文件
- [root@control ansible]# ansible-vault encrypt data.txt #加密文件
- [root@control ansible]# cat data.txt
- [root@control ansible]# ansible-vault view data.txt #查看加密文件
2)修改密码(rekey) - [root@control ansible]# ansible-vault rekey data.txt
3)解密文件 - [root@control ansible]# ansible-vault decrypt data.txt #解密文件
- [root@control ansible]# cat data.txt
4)使用密码文件
加密、解密每次都输入密码很麻烦,可以将密码写入文件。 - [root@control ansible]# echo “I’m secret data” > data.txt #需要加密的敏感数据
- [root@control ansible]# echo 123456 > pass.txt #加密的密码
- [root@control ansible]# ansible-vault encrypt --vault-id=pass.txt data.txt
- [root@control ansible]# cat data.txt
- [root@control ansible]# ansible-vault decrypt --vault-id=pass.txt data.txt
- [root@control ansible]# cat data.txt