1、管理大型的playbook
2、包含或导入文件
3、导入playbook
演示:
演示实例:
在playbook目录下创建两个需要导入的playbook
~~~
[root@localhost playbook]# cat install.yml
---
- hosts: 192.168.145.162
tasks:
- name: install httpd
yum:
name: httpd
state: present
[root@localhost playbook]# cat start.yml
---
- hosts: 192.168.145.162
tasks:
- name: start httpd
service:
name: httpd
state: started
查看需要执行的playbook文件
[root@localhost ansible]# cat play.yml
---
- hosts: 192.168.145.162
tasks:
- name: install httpd
import_playbook: playbook/install.yml
- name: start httpd
import_playbook: playbook/start.yml
查看层级关系
[root@localhost ansible]# tree
.
├── ansible.cfg
├── hosts
├── inventory
├── playbook
│ ├── install.yml
│ └── start.yml
├── playbook.yml
├── play.yml
└── roles
执行playbook
[root@localhost ansible]# ansible-playbook play.yml
PLAY [192.168.145.162] **************************************************************
TASK [Gathering Facts] **************************************************************
ok: [192.168.145.162]
PLAY [192.168.145.162] **************************************************************
TASK [Gathering Facts] **************************************************************
ok: [192.168.145.162]
TASK [install httpd] ****************************************************************
changed: [192.168.145.162]
PLAY [192.168.145.162] **************************************************************
TASK [Gathering Facts] **************************************************************
ok: [192.168.145.162]
TASK [start httpd] ******************************************************************
changed: [192.168.145.162]
PLAY RECAP **************************************************************************
192.168.145.162 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
~~~
4、包含和导入任务
导入任务文件:
演示:
演示实例:
查看playbook目录下写的需要导入的任务文件
[root@localhost playbook]# cat install.yml
---
- name: install httpd
yum:
name: httpd
state: present
[root@localhost playbook]# cat start.yml
---
- name: start httpd
service:
name: httpd
state: started
查看playbook主要执文件
[root@localhost ansible]# cat play.yml
---
- hosts: all
tasks:
- name: install httpd
import_tasks: playbook/install.yml
- name: start httpd
import_tasks: playbook/start.yml
执行playbook
[root@localhost ansible]# ansible-playbook play.yml
PLAY [all] **************************************************************************
TASK [Gathering Facts] **************************************************************
ok: [192.168.145.162]
TASK [install httpd] ****************************************************************
ok: [192.168.145.162]
TASK [start httpd] ******************************************************************
ok: [192.168.145.162]
PLAY RECAP **************************************************************************
192.168.145.162 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
注意:import_tasks和import_playbook处于不同的级别;import_playbook是与tasks平级;import_tasks则是tasks的子任务
演示实例二:使用import_playbook和import_tasks混合使用
查看playbook目录下的需要导入的playbook文件和任务文件
[root@localhost playbook]# cat install.yml
- name: install httpd
yum:
name: httpd
state: present
[root@localhost playbook]# cat start.yml
---
- hosts: all
tasks:
- name: start httpd
service:
name: httpd
state: started
查看主要执行文件
[root@localhost ansible]# cat play.yml
---
- hosts: all
tasks:
- name: install httpd
import_tasks: playbook/install.yml
- name: start httpd
import_playbook: playbook/start.yml
执行playbook
[root@localhost ansible]# ansible-playbook play.yml
PLAY [all] **************************************************************************
TASK [Gathering Facts] **************************************************************
ok: [192.168.145.162]
TASK [install httpd] ****************************************************************
ok: [192.168.145.162]
PLAY [all] **************************************************************************
TASK [Gathering Facts] **************************************************************
ok: [192.168.145.162]
TASK [start httpd] ******************************************************************
ok: [192.168.145.162]
PLAY RECAP **************************************************************************
192.168.145.162 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
注意:使用混合模式的时候,import_playbook不能是第一个执行,必须从第二个开始执行
导入任务文件时,在解析该playbook时将直接插入该文件中的任务。由于import_tasks在解析playbook时静态导入任务,因此对其工作方式有一些影响
包含任务文件
如:
任务文明的用例:
管理任务文件
为外部play和任务定义变量
演示实例:
1:
查看需要导入的外部playbook文件
[root@localhost ansible]# cat playbook/install.yaml
- name: install {{ package }}
yum:
name: "{{ package }}"
state: present
[root@localhost ansible]# cat playbook/service.yaml
- name: start {{ package }} service
service:
name: "{{ package }}"
state: started
查看变量文件
[root@localhost ansible]# cat files/vars.yaml
package: httpd
查看主要执行的文件
[root@localhost ansible]# cat playbook/main.yaml
---
- hosts: all
vars_files:
../files/vars.yaml
tasks:
- import_tasks: install.yaml
- import_tasks: service.yaml
执行playbook
[root@localhost ansible]# ansible-playbook playbook/main.yaml
PLAY [all] **************************************************************************
TASK [Gathering Facts] **************************************************************
ok: [192.168.145.162]
TASK [install httpd] ****************************************************************
ok: [192.168.145.162]
TASK [start httpd service] **********************************************************
ok: [192.168.145.162]
PLAY RECAP **************************************************************************
192.168.145.162 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2:
查看需要导入的外部playbook文件
[root@localhost ansible]# cat playbook/mariadb.yaml
---
- hosts: all
vars_files:
../files/vars.yaml
tasks:
- name: install {{ package }}
yum:
name: "{{ package }}"
state: present
查看变量文件
[root@localhost ansible]# cat files/vars.yaml
package: mariadb-server
查看主要执行的文件
[root@localhost ansible]# cat playbook/main.yaml
- name: install package
import_playbook: mariadb.yaml
执行play
[root@localhost ansible]# ansible-playbook playbook/main.yaml
PLAY [all] **************************************************************************
TASK [Gathering Facts] **************************************************************
ok: [192.168.145.162]
TASK [install mariadb-server] *******************************************************
changed: [192.168.145.162]
PLAY RECAP **************************************************************************
192.168.145.162 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
一、What‘s 角色
使用rhel-system-roles软件包使用角色
在控制节点上查看本地的角色
[root@localhost ~]# ls /usr/share/ansible/roles/
安装rhel-system-roles
[root@localhost ~]# ls /usr/share/ansible/roles/
[root@localhost ~]# yum list | grep rhel-system-roles
rhel-system-roles.noarch 1.0-10.el7_7 extras
rhel-system-roles-techpreview.noarch 1.0-2.el7 extras
[root@localhost ~]# yum install -y rhel-system-roles
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 6.1 kB 00:00:00
* base: mirrors.cqu.edu.cn
* epel: mirrors.ustc.edu.cn
* extras: mirrors.cqu.edu.cn
* updates: mirrors.cqu.edu.cn
在次查看本地的角色
[root@localhost ~]# ls /usr/share/ansible/roles/
linux-system-roles.kdump linux-system-roles.storage rhel-system-roles.postfix
linux-system-roles.network linux-system-roles.timesync rhel-system-roles.selinux
linux-system-roles.postfix rhel-system-roles.kdump rhel-system-roles.storage
linux-system-roles.selinux rhel-system-roles.network rhel-system-roles.timesync
Ansible具备的优点:
二、Ansible角色结构
1、结构说明
2、Ansible角色子目录
子目录 | 功能 |
---|---|
defaults | 此目录中的main.yml文件包含角色变量的默认值,使用角色时可以覆盖这些默认值。 |
这些变量的优先级较低,应该在play中更改和自定义。 | |
files | 此目录包含由角色任务引用的静态文件。 |
handlers | 此目录中的main.yml文件包含角色的处理程序定义。 |
meta | 此目录中的main.yml文件包含与角色相关的信息,如作者、许可证、平台和可选的角色依赖项。 |
tasks | 此目录中的main.yml文件包含角色的任务定义。 |
templates | 此目录包含由角色任务引用的Jinja2模板。 |
tests | 此目录可以包含清单和名为test.yml的playbook,可用于测试角色。 |
vars | 此目录中的main.yml文件定义角色的变量值。这些变量通常用于角色内部用途。 |
这些变量的优先级较高,在playbook中使用时不应更改。 |
注意:并非每个角色都拥有所有这些目录。
三、定义变量和默认值
1、定义变量
2、定义默认值
3、注意事项
四、在palybook中使用ansbible角色
1、在playbook中简单调用Ansible角色
[root@localhost playbook]# cat main.yaml
---
- host: all
roles:
- role_one
- role_two
- role_three
2、角色调用
实例1:
[root@localhost playbook]# cat main.yaml
---
- host: all
gather_facts: no
roles:
- role: roles/role_one
- role: roles/role_two
- role: roles/role_three
重要:
五、控制执行顺序
1、顺序控制
实例演示:
查看playbook
[root@localhost ansible]# cat playbook.yaml
---
- hosts: all
gather_facts: no
pre_tasks:
- debug:
msg: "This is one test"
tasks:
- name: This is twotest
debug:
msg: ‘This is two test‘
post_tasks:
- debug:
msg: "This is three test"
执行play
[root@localhost ansible]# ansible-playbook playbook.yaml
PLAY [all] **************************************************************************
TASK [debug] ************************************************************************
ok: [192.168.145.162] => {
"msg": "This is one test"
}
TASK [This is twotest] **************************************************************
ok: [192.168.145.162] => {
"msg": "This is two test"
}
TASK [debug] ************************************************************************
ok: [192.168.145.162] => {
"msg": "This is three test"
}
PLAY RECAP **************************************************************************
192.168.145.162 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
查看playbook
[root@localhost ansible]# cat playbook.yaml
---
- hosts: all
gather_facts: no
tasks:
- name: This is onetest
debug:
msg: "This is one test"
notify: onetest
changed_when: yes
post_tasks:
- name: This is twotest
debug:
msg: "This is two test"
notify: twotest
changed_when: yes
pre_tasks:
- name: Thsi is threetest
debug:
msg: "This is three test"
notify: threetest
changed_when: yes
handlers:
- name: onetest
debug:
msg: "print the one test"
- name: twotest
debug:
msg: "print the two test"
- name: threetest
debug:
msg: "print the three test"
执行playbook
[root@localhost ansible]# ansible-playbook playbook.yaml
PLAY [all] **************************************************************************
TASK [Thsi is threetest] ************************************************************
changed: [192.168.145.162] => {
"msg": "This is three test"
}
RUNNING HANDLER [threetest] *********************************************************
ok: [192.168.145.162] => {
"msg": "print the three test"
}
TASK [This is onetest] **************************************************************
changed: [192.168.145.162] => {
"msg": "This is one test"
}
RUNNING HANDLER [onetest] ***********************************************************
ok: [192.168.145.162] => {
"msg": "print the one test"
}
TASK [This is twotest] **************************************************************
changed: [192.168.145.162] => {
"msg": "This is two test"
}
RUNNING HANDLER [twotest] ***********************************************************
ok: [192.168.145.162] => {
"msg": "print the two test"
}
PLAY RECAP **************************************************************************
192.168.145.162 : ok=6 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
6、在上例中,每个部分中都执行debug任务来通知my handler处理程序。my handler任务执行了三次
7、除了将角色包含在play的roles部分中外,也可以使用普通任务将角色添加到play中。
1、时间同步角色示例
演示实例:
查看playbook
[root@localhost ansible]# cat playbook.yaml
---
- hosts: all
vars:
timesync_ntp_servers:
-hostname: 192.168.145.162
iburst:yes
roles:
- rhel-system-roles.timesync
查看控制节点时间
[root@localhost ansible]# date
2021年 08月 02日 星期一 22:23:25 CST
执行playbook
[root@localhost ansible]# ansible-playbook playbook.yaml
PLAY [all] **************************************************************************
TASK [Gathering Facts] **************************************************************
ok: [192.168.145.162]
TASK [rhel-system-roles.timesync : Check if only NTP is needed] *********************
ok: [192.168.145.162]
TASK [rhel-system-roles.timesync : Check if single PTP is needed] *******************
skipping: [192.168.145.162]
TASK [rhel-system-roles.timesync : Check if both NTP and PTP are needed] ************
skipping: [192.168.145.162]
TASK [rhel-system-roles.timesync : Determine current NTP provider] ******************
ok: [192.168.145.162]
TASK [rhel-system-roles.timesync : Select NTP provider] *****************************
ok: [192.168.145.162]
TASK [rhel-system-roles.timesync : Install chrony] **********************************
ok: [192.168.145.162]
TASK [rhel-system-roles.timesync : Install ntp] *************************************
skipping: [192.168.145.162]
TASK [rhel-system-roles.timesync : Install linuxptp] ********************************
skipping: [192.168.145.162]
TASK [rhel-system-roles.timesync : Run phc_ctl on PTP interface] ********************
skipping: [192.168.145.162]
TASK [rhel-system-roles.timesync : Check if PTP interface supports HW timestamping] ***
skipping: [192.168.145.162]
TASK [rhel-system-roles.timesync : Get chrony version] ******************************
ok: [192.168.145.162]
TASK [rhel-system-roles.timesync : Get ntp version] *********************************
skipping: [192.168.145.162]
TASK [rhel-system-roles.timesync : Generate chrony.conf file] ***********************
fatal: [192.168.145.162]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: ‘ansible.parsing.yaml.objects.AnsibleUnicode object‘ has no attribute ‘hostname‘"}
PLAY RECAP **************************************************************************
192.168.145.162 : ok=6 changed=0 unreachable=0 failed=1 skipped=7 rescued=0 ignored=0
查看受控主机上的原始时间
[root@server2 ~]# date
2021年 08月 02日 星期一 22:23:59 CST
2、调用Selinux角色示例
包括:
演示实例:
查看受控主机上selinux的状态
[root@localhost ansible]# ansible all -a ‘cat /etc/selinux/config‘
192.168.145.162 | CHANGED | rc=0 >>
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
查看playbook
[root@localhost ansible]# cat playbook.yaml
---
- hosts: all
vars:
selinux_policy: targeted
selinux_state: disabled
tasks:
- name: apply selinux role
block:
- include_role:
name: rhel-system-roles.selinux
rescue:
- name: required reboot
fail:
when: not selinux_reboot_required
- name: reboot host
reboot:
- name: Reapply SElinux role to complete changes
include_role:
name: rhel-system-roles.selinux
执行playbook
[root@localhost ansible]# ansible-playbook playbook.yaml
PLAY [all] **************************************************************************
TASK [Gathering Facts] **************************************************************
ok: [192.168.145.162]
TASK [include_role : rhel-system-roles.selinux] *************************************
TASK [rhel-system-roles.selinux : Install SELinux python2 tools] ********************
ok: [192.168.145.162]
TASK [rhel-system-roles.selinux : Install SELinux python3 tools] ********************
skipping: [192.168.145.162]
TASK [rhel-system-roles.selinux : refresh facts] ************************************
ok: [192.168.145.162]
TASK [rhel-system-roles.selinux : Install SELinux tool semanage on Fedora] **********
skipping: [192.168.145.162]
TASK [rhel-system-roles.selinux : Set permanent SELinux state if enabled] ***********
[WARNING]: SELinux state temporarily changed from ‘enforcing‘ to ‘permissive‘. State
change will take effect next reboot.
changed: [192.168.145.162]
TASK [rhel-system-roles.selinux : Set permanent SELinux state if disabled] **********
skipping: [192.168.145.162]
TASK [rhel-system-roles.selinux : Set ansible facts if needed] **********************
ok: [192.168.145.162]
TASK [rhel-system-roles.selinux : Fail if reboot is required] ***********************
fatal: [192.168.145.162]: FAILED! => {"changed": false, "msg": "Reboot is required to apply changes. Re-execute the role after boot."}
TASK [required reboot] **************************************************************
skipping: [192.168.145.162]
TASK [reboot host] ******************************************************************
changed: [192.168.145.162]
TASK [Reapply SElinux role to complete changes] *************************************
TASK [rhel-system-roles.selinux : Install SELinux python2 tools] ********************
ok: [192.168.145.162]
TASK [rhel-system-roles.selinux : Install SELinux python3 tools] ********************
skipping: [192.168.145.162]
TASK [rhel-system-roles.selinux : refresh facts] ************************************
ok: [192.168.145.162]
TASK [rhel-system-roles.selinux : Install SELinux tool semanage on Fedora] **********
skipping: [192.168.145.162]
TASK [rhel-system-roles.selinux : Set permanent SELinux state if enabled] ***********
skipping: [192.168.145.162]
TASK [rhel-system-roles.selinux : Set permanent SELinux state if disabled] **********
ok: [192.168.145.162]
TASK [rhel-system-roles.selinux : Set ansible facts if needed] **********************
ok: [192.168.145.162]
TASK [rhel-system-roles.selinux : Fail if reboot is required] ***********************
skipping: [192.168.145.162]
TASK [rhel-system-roles.selinux : debug] ********************************************
ok: [192.168.145.162] => {
"msg": "SELinux is disabled on system - some SELinux modules can crash"
}
TASK [rhel-system-roles.selinux : Drop all local modifications] *********************
skipping: [192.168.145.162]
TASK [rhel-system-roles.selinux : Purge all SELinux boolean local modifications] ****
skipping: [192.168.145.162]
TASK [rhel-system-roles.selinux : Purge all SELinux file context local modifications] ***
skipping: [192.168.145.162]
TASK [rhel-system-roles.selinux : Purge all SELinux port local modifications] *******
skipping: [192.168.145.162]
TASK [rhel-system-roles.selinux : Purge all SELinux login local modifications] ******
skipping: [192.168.145.162]
TASK [rhel-system-roles.selinux : Reload SELinux policy] ****************************
skipping: [192.168.145.162]
TASK [rhel-system-roles.selinux : Set SELinux booleans] *****************************
TASK [rhel-system-roles.selinux : Set SELinux file contexts] ************************
TASK [rhel-system-roles.selinux : Restore SELinux labels on filesystem tree] ********
TASK [rhel-system-roles.selinux : Restore SELinux labels on filesystem tree in check mode] ***
TASK [rhel-system-roles.selinux : Set an SELinux label on a port] *******************
TASK [rhel-system-roles.selinux : Set linux user to SELinux user mapping] ***********
PLAY RECAP **************************************************************************
192.168.145.162 : ok=11 changed=2 unreachable=0 failed=0 skipped=20 rescued=1 ignored=0
执行playbook后,查看受管主机上的selinux状态
[root@localhost ansible]# ansible all -a ‘cat /etc/selinux/config‘
192.168.145.162 | CHANGED | rc=0 >>
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
3、配置SELinux角色
原文:https://www.cnblogs.com/Aimmi/p/15092329.html