Inventory文件定义了ansible管理的主机,说白了就是Inventory文件中的内容是记录被管理的主机。
Inventory文件分为两种,一种是静态的Inventory文件,一种是动态的Inventory文件。
静态的Inventory文件其实非常好理解,就是txt文本记录的被管理主机。只要不修改这个静态
Inventory文件,被管理的主机就不会发生变化。
动态的Inventory文件是能动态的输出被管理主机。动态Inventory文件的原理就是一个脚本,大部分
都是python的脚本。动态Inventory文件能连接到某个管理系统的节点信息数据库,并将节点信息以特
定的格式输出(绝大多数情况下是json),也就是说动态Inventory文件输出的主机信息是管理系统节
点信息数据库的内容。也就是说节点信息数据库的变化就会导致Inventory输出的被管理主机数量不同。
所以称之为动态的Inventory文件。
# 我们只讲静态inventory
#静态Inventory文件就是txt文件,下面是一个简单的txt格式Inventory文件(这个文件名自定义)
ssh workstation # 登录到workstation控制节点中
# 创建inventory文件,写入以下被管理主机的名称/IP地址,均可。
[student@workstation ~]$ vim inventory
[student@workstation ~]$ cat inventory
servera
serverb
serverc
serverd
---------------------------------------------------------------------------------------
#Inventory文件还可以定义主机组
#一个主机文件可以属于多个组
[student@workstation ~]$ cat inventory
[storage]
servera
serverb
[compute]
serverc
serverd
---------------------------------------------------------------------------------------
# 特殊的主机组
在ansible的Inventory文件中有两个特殊的组。一个是all,一个是ungrouped。
all主机组表示Inventory文件中所有的主机
ungrouped主机组表示不属于任何主机组的被管理主机
#Inventory的嵌套组定义
[student@workstation ~]$ cat inventory
------------------------------------
[storage]
servera
serverb
[compute]
serverc
serverd
[cloud:children]
storage
compute
------------------------------------
嵌套的主机组必须要在组名后面加上“:children”,否则嵌套主机组会认为下面的名字指定的是某个主机,而不是某个主机组。
比如想定义192.168.0.0~192.168.0.255和192.168.1.0~192.168.1.255,总共有512个主机
#中级玩家
192.168.0.[0:255]
192.168.1.[0:255]
#高级玩家
192.168.[0:1].[0:255]
server[a:d]
#无论是嵌套的主机组的定义还是指定Inventory主机的范围,本意都是让Inventory文件看起来别那么复杂。
# 目前inventory文件里的主机组
[student@workstation ~]$ cat inventory
[storage]
servera
serverb
[compute]
serverc
serverd
[cloud:children]
storage
compute
------------------------------------------------
# 查看主机组信息
[student@workstation ~]$ ansible -i /home/student/inventory storage --list-hosts
hosts (2):
servera
serverb
ansible命令-i参数可以指定Inventory文件的路径,由于在你的ansible控制节点里面可能有多个
Inventory文件,所以使用正确的Inventory文件就非常有必要。
当然还有其他方式可以指定Inventory文件的路径,但是-i参数的优先级是最高的。
[student@workstation ~]$ ansible -i ./inventory cloud --list-hosts
hosts (4):
servera
serverb
serverc
serverd
[student@workstation ~]$ ansible -i /home/student/ergou storage --listhosts
hosts (2):
servera
serverb
storage表示storage主机组。
--list-hosts表示列出你想查看的主机
[kiosk@foundation0 ~]$ ssh workstation # 登录到workstation主机
[student@workstation ~]$ lab deploy-inventory start # 执行这条命令(其实就是给默认的inventory文件做了备份而已)
# 练习1:编辑/etc/ansible/hosts 文件,加入 servera.lab.example.com 主机
[student@workstation ~]$ sudo vim /etc/ansible/hosts # 编辑这个hosts文件
[student@workstation ~]$ cat /etc/ansible/hosts #写入这一条主机。其余的注释啥的全都删掉,留这一行就可以,作为练习。
servera.lab.example.com
【注意】
# /etc/ansible/hosts 是ansible配置文件指定Inventory默认的文件
# 如果使用ansible命令不加-i参数,默认就会使用这个Inventory文件。
# 练习2:编辑/etc/ansible/hosts 文件,加入webservers主机组,并在主机组加入serverb.lab.example.com主机
[student@workstation ~]$ sudo vim /etc/ansible/hosts
[student@workstation ~]$ cat /etc/ansible/hosts
servera.lab.example.com
[webservers]
serverb.lab.example.com
# 练习3:查看主机和主机组的信息
[student@workstation ~]$ ansible all --list-hosts # 查看所有主机信息
hosts (2):
servera.lab.example.com
serverb.lab.example.com
[student@workstation ~]$ ansible ungrouped --list-hosts # 查看未加入主机组的主机信息
hosts (1):
servera.lab.example.com
[student@workstation ~]$ ansible webservers --list-hosts # 查看主机组的信息
hosts (1):
serverb.lab.example.com
# 练习4:依照上面的图中信息,在/deploy-inventory 目录下自定义一个inventory文件。按照上面给出的信息,写一个inventory文件
# 然后建立一个嵌套组,里面包含了raleigh 和 mountainview两个主机组
student@workstation ~]$ mkdir ~/deploy-inventory
[student@workstation ~]$ cd deploy-inventory/
[student@workstation deploy-inventory]$ vim inventory
-----------------------------------------------------
[webserver]
server[a:d].lab.example.com
[raleigh]
server[a:b].lab.example.com
[mountainview]
serverc.lab.example.com
[london]
serverd.lab.example.com
[development]
servera.lab.example.com
[testing]
serverb.lab.example.com
[production]
server[c:d].lab.example.com
[us:children]
raleigh
mountainview
-----------------------------------------------------
[student@workstation deploy-inventory]$ ansible -i ./inventory us --list-hosts
hosts (3):
servera.lab.example.com
serverb.lab.example.com
serverc.lab.example.com
[student@workstation deploy-inventory]$ ansible -i ./inventory production --list-hosts
hosts (2):
serverc.lab.example.com
serverd.lab.example.com
[student@workstation deploy-inventory]$ ansible -i ./inventory ungrouped --list-hosts
[WARNING]: No hosts matched, nothing to do
hosts (0):
# 再次执行 lab deploy-inventory finish 命令。
# 这个命令意思就是练习完成,完全恢复到练习之前的环境了。
[student@workstation deploy-inventory]$ cd
[student@workstation ~]$ lab deploy-inventory finish
关于ansible的配置文件,有一些非常重要的细节知识需要掌握。
ansible的配置文件不是全局的,任何用户都可以有自己的ansible配置文件。
ansible的配置文件在安装的时候,就有一个缺省的配置文件
[student@workstation ~]$ rpm -qf /etc/ansible/ansible.cfg
ansible-2.8.0-1.el8ae.noarch
如果你不想使用这个安装自带的ansible配置文件,你可以自己创建。
#一般在生产环境中,都是创建一个你自己的目录,然后在该目录下创建自己的ansible配置文件。
[student@workstation ~]$ mkdir gzy
[student@workstation ~]$ cd gzy/
#如何查看你当前正在使用哪个ansible的配置文件呢?
[student@workstation gzy]$ ansible --version
ansible 2.8.0
config file = /etc/ansible/ansible.cfg # 这个就是可以当前正在使用的配置文件
configured module search path = ['/home/student/.ansible/plugins/modules',
'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.8 (default, Apr 3 2019, 17:26:03) [GCC 8.2.1
20180905 (Red Hat 8.2.1-3)]
------------------------------------------------------------
ansible的配置文件有4种形式可以指定,4种指定方式优先级是不同的。
如果没有任何其他的ansible配置文件,默认就会使用/etc/ansible/ansible.cfg
#1.家目录下的.ansible.cfg优先级高于/etc/ansible/ansible.cfg
[student@workstation gzy]$ touch ~/.ansible.cfg
[student@workstation gzy]$ ansible --version | grep cfg
config file = /home/student/.ansible.cfg
# 2.当前目录下的ansible.cfg的优先级高于~/.ansible.cfg(使用频率最高)
[student@workstation gzy]$ touch ansible.cfg
[student@workstation gzy]$ pwd
/home/student/gzy
[student@workstation gzy]$ ansible --version | grep cfg
config file = /home/student/gzy/ansible.cfg
#3.ANSIBLE_CONFIG变量指定的配置文件优先级最高(基本很少使用)
[student@workstation gzy]$ touch /tmp/ansible.cfg
[student@workstation gzy]$ export ANSIBLE_CONFIG=/tmp/ansible.cfg
[student@workstation gzy]$ ansible --version | grep cfg
config file = /tmp/ansible.cfg
当你毫无头绪的时候,可以先看示例文件,示例配置文件就是安装软件自带的那个配置文件。
[student@workstation gzy]$ grep -v "#" /etc/ansible/ansible.cfg | grep -v "^$"
[defaults]
[inventory]
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]
# ansible的配置文件中是以sector作为划分的。每个方括号就表示一个sector。
# ansible文件中最常用的两个sector ->defaults ,privilege_escalation
[defaults]
inventory = /etc/ansible/hosts
#表示该配置文件默认使用的Inventory文件是/etc/ansible/hosts
remote_user = ergou
#表示该配置文件使用ergou用户来去进行ssh连接
ask_pass = false
#表示使用ergou用户去ssh连接的时候不提示输入密码
[privilege_escalation]
#如果你remote_user使用的是root用户,就不需要配置提权部分,如果你的remote_user不是root,但是不需要做特权操作,那么也可以不用配置这部分。如果是普通用户,但是需要做特权操作,那么就需要配置这部分。
become = true
#true表示需要提权,false就表示不需要提权
become_method = sudo
#表示提权的方式是sudo提权
become_user = root
#表示提权到root用户
become_ask_pass = false
#false表示进行sudo操作的时候不提示输入密码,true表示需要输入密码
不是任何用户作为remote_user,且配置了提权就能真的提权。而必须要在被管理主机里面配置sudoers文件,让这个remote_user有提权的能力才可以。
ssh可以设置免密登陆,避免remote_user使用ssh登陆的时候需要输入密码
sudoers文件中也可以设置sudo命令不提示输入密码
1.创建配置文件
[student@workstation ~]$ lab deploy-manage start # 开始练习
[student@workstation ~]$ mkdir ~/deploy-manage
[student@workstation ~]$ cd ~/deploy-manage/
[student@workstation deploy-manage]$ cat > ansible.cfg <<END
> [defaults]
> inventory = ./inventory
> END
2.配置Inventory文件
[student@workstation deploy-manage]$ cat > inventory <<END
> [myself]
> localhost
>
> [intranetweb]
> servera.lab.example.com
>
> [internetweb]
> serverb.lab.example.com
>
> [web:children]
> intranetweb
> internetweb
> END
[student@workstation deploy-manage]$ ansible myself --list-hosts
hosts (1):
localhost
[student@workstation deploy-manage]$ ansible intranetweb --list-hosts
hosts (1):
servera.lab.example.com
[student@workstation deploy-manage]$ ansible internetweb --list-hosts
hosts (1):
serverb.lab.example.com
[student@workstation deploy-manage]$ ansible web --list-hosts
hosts (2):
servera.lab.example.com
serverb.lab.example.com
3.配置ansible配置文件提权部分
[student@workstation deploy-manage]$ cat >> ansible.cfg <<END
> [privilege_escalation]
> become = true
> become_method = sudo
> become_user = root
> become_ask_pass = true
> END
[student@workstation deploy-manage]$ cat ansible.cfg
[defaults]
inventory = ./inventory
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = true
[student@workstation deploy-manage]$ lab deploy-manage finish # 完成练习
两种方式 ad Hoc(很少用) 和 playbook(工作中常用)
# 这里讲ad hoc是为了方便理解playbook
#ad hoc的命令格式
ad hoc的命令格式其实就是
ansible "host-pattern" -m "moudle" -a "moudle argument" -i "inventroy-path"
host-pattern表示某个主机或者某个主机组
-m参数表示使用ansible的模块
-a参数后面跟的是模块的参数
-i参数后面跟的是Inventory的路径
[student@workstation deploy-manage]$ cat ansible.cfg
[defaults]
inventory = ./inventory
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = true
[student@workstation deploy-manage]$ cat inventory
[myself]
localhost
[intranetweb]
servera.lab.example.com
[internetweb]
serverb.lab.example.com
[web:children]
intranetweb
internetweb
# 用刚才的配置文件做练习。
[student@workstation deploy-manage]$ ansible intranetweb -m ping
BECOME password: # 密码 student
servera.lab.example.com | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
ping模块表示在被管理主机上使用ping命令来ping控制节点。
如果success就表示你的被管理主机到控制节点是可达的。
# 1.修改配置文件加入 ask_pass = true 表名需要主机密码
[student@workstation deploy-manage]$ cat ansible.cfg
[defaults]
inventory = ./inventory
ask_pass = true
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = true
[student@workstation deploy-manage]$ ansible intranetweb -m ping
SSH password: # student
BECOME password[defaults to SSH password]: # 直接回车,因为这里的密码也是student
servera.lab.example.com | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
# 2.修改配置文件 ,加入remote_user = root 远程用于设置为root
[student@workstation deploy-manage]$ cat ansible.cfg
[defaults]
inventory = ./inventory
ask_pass = true
remote_user = root
#[privilege_escalation]
#become = true
#become_method = sudo
#become_user = root
#become_ask_pass = true
[student@workstation deploy-manage]$ ansible intranetweb -m ping
SSH password: # student
servera.lab.example.com | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
# 注意:如果become_ask_pass = false 被设置为false,说明提权的时候,不需要用户输入密码,
# 但是如果你没有在登录的那台主机上的sudoers 文件设置免密登录,就会报错。
假设此时你要 ansible intranetweb -m ping 的主机是servera,远程登录用户是student
[student@workstation deploy-manage]$ cat ansible.cfg
[defaults]
inventory = ./inventory
remote_user = student #远程登录用户
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false # 这里设置为false
[student@workstation deploy-manage]$ ansible intranetweb -m ping
# 执行 ansible intranetweb -m ping 会报错,因为在servera里没有做免密登录。
#我们登录到servera 上,切换到root用户,修改 /etc/sudoers文件。在里面加入如下的命令
student ALL=(ALL) NOPASSWD: ALL
# 然后在执行,就会成功。
ansible-doc
[student@workstation ~]$ ansible-doc -l
列出当前系统所有的ansible模块
[student@workstation ~]$ ansible-doc ping
查看ping模块的操作,查看到的都是playbook的操作
# 此时的配置文件和inventory文件
[student@workstation deploy-manage]$ cat ansible.cfg
[defaults]
inventory = ./inventory
ask_pass = false
remote_user = student
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false
[student@workstation deploy-manage]$ cat inventory
[myself]
localhost
[intranetweb]
servera.lab.example.com
[internetweb]
serverb.lab.example.com
[web:children]
intranetweb
internetweb
# 添加一个用户
[student@workstation deploy-manage]$ ansible -m user -a 'name=glstest
uid=5000 state=present' intranetweb
servera.lab.example.com | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 5000,
"home": "/home/glstest",
"name": "glstest",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 5000
}
# 登录到被管理主机servera上可以看到成功创建了glstest用户
[root@servera ~]# id glstest
uid=5000(glstest) gid=5000(glstest) groups=5000(glstest)
# 删除一个用户
[student@workstation deploy-manage]$ ansible -m user -a 'name=glstest
state=absent' intranetweb
servera.lab.example.com | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"force": false,
"name": "glstest",
"remove": false,
"state": "absent"
}
# 此时就没有该用户
[root@servera ~]# id glstest
id: ‘glstest1’: no such user
[student@workstation ~]$ lab deploy-review start
# 查看版本
[student@workstation ~]$ ansible --version
# 创建配置文件
[student@workstation ~]$ mkdir deploy-review
[student@workstation ~]$ cd deploy-review/
[student@workstation deploy-review]$ vim ansible.cfg
-------------------------------------------
[defaults]
remote_user = devops
inventory = ./inventory
ask_pass = false
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false
-------------------------------------------
# 创建inventory子目录,在里面下载inventory文件
[student@workstation deploy-review]$ mkdir inventory
[student@workstation deploy-review]$ cd inventory/
[student@workstation inventory]$ wget http://materials.example.com/labs/deploy-review/inventory
[student@workstation inventory]$ ls
inventory
[student@workstation inventory]$ cat inventory
[internetweb]
serverb.lab.example.com
[intranetweb]
servera.lab.example.com
serverc.lab.example.com
serverd.lab.example.com
# 在所有主机上执行id命令
[student@workstation inventory]$ cd ..
[student@workstation deploy-review]$ ansible all -a "id"
servera.lab.example.com | CHANGED | rc=0 >>
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
serverd.lab.example.com | CHANGED | rc=0 >>
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
serverc.lab.example.com | CHANGED | rc=0 >>
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
serverb.lab.example.com | CHANGED | rc=0 >>
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
# 向所有被管理主机的/etc/下copy一个motd文件。
[student@workstation deploy-review]$ ansible all -m copy -a 'content="This server is managed by Ansible.\n" dest=/etc/motd'
# 执行两次,两次颜色不一样,第二次文件其实并没有被copy,都是绿色。第一次都是黄色。
[student@workstation deploy-review]$ ansible all -m copy -a 'content="This server is managed by Ansible.\n" dest=/etc/motd'
# 查看
[student@workstation deploy-review]$ ansible all -a 'cat /etc/motd'
# 然后,进行评分
[student@workstation deploy-review]$ lab deploy-review grade
Grading the student's work on workstation:
· Verify active configuration file............................ PASS
· Verify servera /etc/motd.................................... PASS
· Verify serverb /etc/motd.................................... PASS
· Verify serverc /etc/motd.................................... PASS
· Verify serverd /etc/motd.................................... PASS
Overall lab grade.............................................. PASS
# 完成实验,还原环境
[student@workstation deploy-review]$ cd ..
[student@workstation ~]$ lab deploy-review finish
ad hoc命令是单行,一个简单的任务,运行一次。ansible真正强大的地方是使用ansible的playbook重复运行多次复杂的任务。
一个play是是一组有序的任务,该paly对应着在inventory被选择的主机。一个playbook是一个包含若干个paly的text文本文件。
plays让你将又长又复杂的管理任务变的简单。在palybook中,你可以用易懂和立即能运行的格式将tasks保存在play中。tasks本身由于书写方式的原因,就是一个一部一部部署你的架构或者应用的文档。
[student@workstation ~]$ cat .vimrc
autocmd FileType yaml setlocal ai ts=2 sw=2 et
每一个playbook就是一个整体的剧本
这个剧本里的每一集就是一个play
每一集的多个情节就是task
也就是说:一个playbook 里可以包含多个play
一个play可以可以包含多个task
#测试都放在ztf目录下去做
[student@workstation ~]$ mkdir ztf
[student@workstation ~]$ cd ztf/
# 在这个目录下一定要记得创建一个配置文件和inventory文件
[student@workstation ztf]$ vim ansible.cfg
[defaults]
inventory = ./inventory
ask_pass = false
remote_user = student
[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false
-----------------------------------------------------------
[student@workstation ztf]$ vim inventory
servera
------------------------------------------------------------
# 检查当前的配置文件是否路径正确
[student@workstation ztf]$ ansible --version
ansible 2.8.0
config file = /home/student/ztf/ansible.cfg
configured module search path = ['/home/student/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.8 (default, Apr 3 2019, 17:26:03) [GCC 8.2.1 20180905 (Red Hat 8.2.1-3)]
# ping一下servera,看是否通
[student@workstation ztf]$ ansible servera -m ping
servera | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
# 执行简单的id命令 来测试查看免密登录是否正常(当然,你也可以用其他的Linux命令来检测)
[student@workstation ztf]$ ansible servera -a 'id'
servera | CHANGED | rc=0 >>
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
# 创建create_user.yaml文件(创建playbook)
[student@workstation ztf]$ vim create_user.yaml
-----------------------------------------------------------
---
- name: this is my first play
hosts: servera
tasks:
- name: create a new user
user:
name: ly
uid: 1314
state: present # state状态:present 提供的意思,就是有这个用户就修改了uid,没有的话就添加
-----------------------------------------------------------
# 这个playbook 里只有一个play ,这个play被管理的主机是servera,里面有一个task ,这个task的名字是crete a new user,
# 这个task里面有一个user的模块,这个user的模块创建了一个ly用户,uid是1314
# 执行playbook
[student@workstation ztf]$ ansible-playbook create_user.yaml # 当前的yaml文件
[student@workstation ztf]$ ansible servera -a 'id ly'
servera | CHANGED | rc=0 >>
uid=1314(ly) gid=1314(ly) groups=1314(ly)
ansible-playbook create_user.yaml -v # 详细信息
ansible-playbook create_user.yaml -vv # 更加详细的信息 常用
ansible-playbook create_user.yaml -vvv # 不常用
ansible-playbook create_user.yaml -vvvv # 不常用
[student@workstation ~]$ ansible-playbook --syntax-check create_user.yaml # 运行之前先检查语法是否正确
#搭一个web服务器
# 登录到workstation上
# 启动练习环境
[student@workstation ~]$ lab playbook-basic start
# 进入 playbook-basic目录,创建一个site.yml文件 内容如下:
[student@workstation playbook-basic]$ vim site.yml
# 检测语法错误
[student@workstation playbook-basic]$ ansible-playbook --syntax-check site.yml # 语法没有问题
# 运行site.yml
[student@workstation playbook-basic]$ ansible-playbook site.yml
# 运行会失败,因为我们把enable写错了,应该是enabled模块,少写了个d
# 重新运行即可成功
# 测试
[student@workstation playbook-basic]$ curl serverc
This is a test page.
[student@workstation playbook-basic]$ curl serverd
This is a test page.
# 环境还原
[student@workstation ~]$ lab playbook-basic finish
查文档的方式两种
# 第一种:官方文档---> http://docs.ansible.com
# 第二种:ansible-doc 命令查看本地系统上安装的模块的信息(常用)
ansible-doc -l #显示模块名称列表以及其功能的概述
ansible-doc [module-name] # 查看具体模块的信息
比如:查看yum模块
ansible-doc yum # 其实最主要的就是查看里面的example示例。
这里提供一个文档工具,百度云链接放在下面,可以搜、下载很多的文档,不止ansible
链接:https://pan.baidu.com/s/1L7BrQm6DK7BO5mNIUVevzg
提取码:6666
yaml注释用 ‘#’ 号
不加引号,和单引号、双引号效果是一样的。
说明白点:| 就是你写的每一行都加了换行符,
> 就是你写的每一行都是一行,这一行结束才换行。
这个在写入一些文件内容的时候,尤其重要。
Yaml列表
[student@workstation ~]$ lab playbook-multi start
[student@workstation ~]$ cd playbook-multi/
[student@workstation playbook-multi]$ vim intranet.yml
---
- name: first play
hosts: servera.lab.example.com
become: yes
tasks:
- name: install package
yum:
name:
- httpd
- firewalld
state: present
- name: web content
copy:
content: "this is a test page\n"
dest: /var/www/html/index.html
- name: start firewalld service
service:
name: firewalld
enabled: true
state: started
- name: set firewalld rule
firewalld:
service: http
permanent: true
immediate: yes
state: enabled
- name: start httpd service
service:
name: httpd
enabled: true
state: started
- name: second paly
hosts: localhost
become: no
tasks:
- name: test web server
uri:
url: http://servera.lab.example.com
retrun_content: yes
status_code: 200
# 运行intranet.yml
[student@workstation playbook-multi]$ ansible-playbook intranet.yml
[student@workstation ~]$ lab playbook-multi finish
[student@workstation ~]$ lab playbook-review start
[student@workstation playbook-review]$ vim internet.yml
[student@workstation playbook-review]$ cat internet.yml
---
- name: enable internet services
hosts: serverb.lab.example.com
become: yes
tasks:
- name: install some pkgs
yum:
name:
- firewalld
- httpd
- mariadb-server
- php
- php-mysqlnd
state: latest
- name: set firewalld enabled and running
service:
name: firewalld
enabled: yes
state: started
- name: set firewalld rule to allow httpd service
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
- name: set httpd enabled and running
service:
name: httpd
enabled: yes
state: started
- name: set mariadb enabled and running
service:
name: mariadb
enabled: yes
state: started
- name: use get_url module to download wen content file on managed host
get_url:
url: http://materials.example.com/labs/playbook-review/index.php
dest: /var/www/html
- name: second play
hosts: localhost
become: no
tasks:
- name: access web server
uri:
url: http://serverb.lab.example.com
status_code: 200
[student@workstation playbook-review]$ ansible-playbook --syntax-check internet.yml
[student@workstation playbook-review]$ ansible-playbook internet.yml
# 评分
[student@workstation playbook-review]$ lab playbook-review grade
[student@workstation ~]$ lab playbook-review finish