ansible特性 1.模块化设计,调用特定的模块来完成特定任务 2.基于python语言实现 3.其模块支持JSON、YAML等标准输出格式 4.支持playbook
管理端: 192.168.110.135 被管理端:192.168.110.136 被管理端:192.168.110.137
yum install -y epel-release //安装epel源
yum install ansible -y
ansible --version //查看ansible版本
yum install tree -y
tree /etc/ansible/ //树状结构展示文件夹
/etc/ansible/
├── ansible.cfg #ansible的配置文件
├── hosts #ansible的主仓库,用于存储需要管理的远程主机的相关信息
└── roles #角色
cd /etc/ansible
vi hosts //配置主机清单
[webserver]
192.168.110.136
[mysql]
192.168.110.137
ssh-keygen -t rsa //一路回车
ssh-copy-id root@192.168.110.136
ssh-copy-id root@192.168.110.137 //配置密钥对验证
命令格式:ansible [主机] [-m 模块] [-a args]
ansible-doc -l //列出所有已安装的模块 注:按q退出
ansible-doc -s yum //-s列出yum模块描述信息和操作动作
eg1:指定ip执行命令
[root@localhost opt]# ansible 192.168.110.136 -m command -a 'date' //这里的命令要加单引号
192.168.110.136 | CHANGED | rc=0 >>
2020年 10月 17日 星期六 15:30:41 CST
[root@localhost opt]# ansible webserver -m command -a 'date' //也可以指定host文件定义的名字
192.168.110.136 | CHANGED | rc=0 >>
2020年 10月 17日 星期六 15:31:26 CST
eg2:所有host文件定义的一起执行命令
[root@localhost opt]# ansible all -m command -a 'date'
192.168.110.137 | CHANGED | rc=0 >>
2020年 10月 17日 星期六 15:32:55 CST
192.168.110.136 | CHANGED | rc=0 >>
2020年 10月 17日 星期六 15:33:00 CST
不加-m command 默认是command
[root@localhost opt]# ansible all -a 'ls'
192.168.110.136 | CHANGED | rc=0 >>
anaconda-ks.cfg
initial-setup-ks.cfg
公共
模板
视频
图片
文档
下载
音乐
桌面
192.168.110.137 | CHANGED | rc=0 >>
anaconda-ks.cfg
initial-setup-ks.cfg
公共
模板
视频
图片
文档
下载
音乐
桌面
查看所有cron模块信息
[root@localhost opt]# ansible-doc -s cron
- name: Manage cron.d and crontab entries
cron:
backup: # If set, create a backup of the crontab before it is
modified. The
location of the
backup is returned in
the `backup_file'
variable by this
module.
cron_file: # If specified, uses this file instead of an
individual user's
crontab. If this is a
relative path, it is
interpreted with
respect to
`/etc/cron.d'. If it
is absolute, it will
typically be
:
eg1: 执行cron计划任务 每分钟执行一次输入 到/opt/info.txt中
[root@localhost opt]# ansible webserver -m cron -a 'minute="*/1" job="/usr/bin/echo heihei >> /opt/info.txt" name="test cron job"'
192.168.110.136 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"test cron job"
]
}
查看cron计划任务
[root@localhost opt]# ansible webserver -a 'crontab -l'
192.168.110.136 | CHANGED | rc=0 >>
#Ansible: test cron job
*/1 * * * * /usr/bin/echo heihei >> /opt/info.txt
删除计划任务 state=absent (缺少的)
[root@localhost opt]# ansible webserver -m cron -a 'name="test cron job" state=absent'
192.168.110.136 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": []
}
user模块是请求的是useradd, userdel, usermod三个指令
[root@localhost opt]# ansible-doc -s user
- name: Manage user accounts
user:
append: # If `yes', add the user to the groups specified in
`groups'. If `no',
user will only be
added to the groups
specified in
`groups', removing
them from all other
groups. Mutually
exclusive with
`local'
authorization: # Sets the authorization of the user. Does nothing
when used with other
platforms. Can set
multiple
authorizations using
comma separation. To
eg1:创建mysql主机的账户 shangzhen
[root@localhost opt]# ansible mysql -m user -a 'name="shangzhen"'
192.168.110.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1002,
"home": "/home/shangzhen",
"name": "shangzhen",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1002
}
[root@localhost opt]#
查看是否创建成功
[root@localhost opt]# ansible mysql -m command -a 'tail /etc/passwd'
192.168.110.137 | CHANGED | rc=0 >>
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:988:982::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
shang:x:1000:1000:shang:/home/shang:/bin/bash
test01:x:1001:1001::/home/test01:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
shangzhen:x:1002:1002::/home/shangzhen:/bin/bash '发现已经创建成功'
删除刚刚创建的账户 思路还是一样将state改为absent
[root@localhost opt]# ansible mysql -m user -a 'name="shangzhen" state=absent'
192.168.110.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"force": false,
"name": "shangzhen",
"remove": false,
"state": "absent"
}
[root@localhost opt]# ansible mysql -m command -a 'tail /etc/passwd'
192.168.110.137 | CHANGED | rc=0 >>
setroubleshoot:x:990:984::/var/lib/setroubleshoot:/sbin/nologin
saned:x:989:983:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:988:982::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
shang:x:1000:1000:shang:/home/shang:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
group模块请求的是groupadd, groupdel, groupmod 三个指令。
eg1:创建mysql的组 并设置为系统账户 设置组id为306
[root@localhost opt]# ansible mysql -m group -a 'name=mysql gid=306 system=yes'
192.168.110.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 306,
"name": "mysql",
"state": "present",
"system": true
}
[root@localhost opt]# ansible mysql -m command -a 'tail /etc/group'
192.168.110.137 | CHANGED | rc=0 >>
gnome-initial-setup:x:982:
sshd:x:74:
slocate:x:21:
avahi:x:70:
postdrop:x:90:
postfix:x:89:
tcpdump:x:72:
shang:x:1000:shang
apache:x:48:
mysql:x:306:
eg2:结合上面设置mysql的组 再次创建一个test01的用户 将其加入到这个组中
[root@localhost opt]# ansible mysql -m user -a 'name=test01 uid=306 system=yes group=mysql'
192.168.110.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 306,
"home": "/home/test01",
"name": "test01",
"shell": "/bin/bash",
"state": "present",
"stderr": "useradd:警告:此主目录已经存在。\n不从 skel 目录里向其中复制任何文件。\n",
"stderr_lines": [
"useradd:警告:此主目录已经存在。",
"不从 skel 目录里向其中复制任何文件。"
],
"system": true,
"uid": 306
}
[root@localhost opt]# ansible mysql -m command -a 'tail /etc/passwd'
192.168.110.137 | CHANGED | rc=0 >>
saned:x:989:983:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:988:982::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
shang:x:1000:1000:shang:/home/shang:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
test01:x:306:306::/home/test01:/bin/bash
[root@localhost opt]# ansible mysql -a 'id test01' '执行查看一下具体的用户信息'
192.168.110.137 | CHANGED | rc=0 >>
uid=306(test01) gid=306(mysql) 组=306(mysql)
eg1:将本机的/etc/fstab 复制到 目标主机的/opt/fatab 这里源和目标要分清出 也要看一下本机是否有这各文件 不然会报错
[root@localhost opt]# ansible webserver -m copy -a 'src=/etc/fstab dest=/opt/fstab owner=root mode=640'
192.168.110.136 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "190dd05feebecb7c01b96a99760707b48da7c258",
"dest": "/opt/fstab",
"gid": 0,
"group": "root",
"md5sum": "c2681ffb6e152a1151ca413de59a10eb",
"mode": "0640",
"owner": "root",
"secontext": "system_u:object_r:usr_t:s0",
"size": 465,
"src": "/root/.ansible/tmp/ansible-tmp-1602921712.59-16048-113078267691827/source",
"state": "file",
"uid": 0
}
[root@localhost opt]# ansible webserver -m command -a 'ls /opt' '验证是否创建成功'
192.168.110.136 | CHANGED | rc=0 >>
fstab
info.txt
rh
temp
eg2:终端输入某些字段写入到对目标主机的目标文件中 这里是覆盖到对方文件中慎用
[root@localhost opt]# ansible mysql -m copy -a 'content="hello heihei!" dest=/opt/test1'
192.168.110.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "b783c5c2da963523d21deff007f6e6b97fc625dc",
"dest": "/opt/test1",
"gid": 0,
"group": "root",
"md5sum": "0e7a9bdc00d20b6e3e1b03d836095644",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:usr_t:s0",
"size": 13,
"src": "/root/.ansible/tmp/ansible-tmp-1602921947.65-16174-217801884734684/source",
"state": "file",
"uid": 0
}
[root@localhost opt]# ansible mysql -m command -a 'cat /opt/test1'
192.168.110.137 | CHANGED | rc=0 >>
hello heihei!
eg1:创建一个文件到目标主机上 并设置属主属组 首先要确认目标主机上是否有你想设置的用户和组
[root@localhost opt]# ansible mysql -m file -a 'owner=mysql group=mysql mode=644 path=/opt/fstab.back'
192.168.110.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 306,
"group": "mysql",
"mode": "0644",
"owner": "mysql",
"path": "/opt/fstab.back",
"secontext": "system_u:object_r:usr_t:s0",
"size": 465,
"state": "file",
"uid": 305
}
[root@localhost opt]# ansible mysql -m command -a 'ls /opt'
192.168.110.137 | CHANGED | rc=0 >>
fstab.back '刚刚创建的文件'
inittab.back
rh
script.txt
test1
eg2:设置链接文件
[root@localhost opt]# ansible mysql -m file -a 'path=/opt/fstab.link src=/opt/fstab.back state=link'
192.168.110.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/opt/fstab.link",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"secontext": "unconfined_u:object_r:usr_t:s0",
"size": 15,
"src": "/opt/fstab.back",
"state": "link",
"uid": 0
}
[root@localhost opt]# ansible mysql -m command -a 'ls -l /opt'
192.168.110.137 | CHANGED | rc=0 >>
总用量 16
-rw-r--r--. 1 mysql mysql 465 10月 17 10:26 fstab.back
lrwxrwxrwx. 1 root root 15 10月 17 16:14 fstab.link -> /opt/fstab.back
-rw-r--r--. 1 root root 240 10月 17 13:45 inittab.back
drwxr-xr-x. 2 root root 6 10月 31 2018 rh
-rw-r--r--. 1 root root 26 10月 17 10:50 script.txt
-rw-r--r--. 1 root root 13 10月 17 16:05 test1
这里要注意 在ansible中不能使用命令缩写 不识别 最好加上命令的绝对路径 涉及到环境变量
[root@localhost opt]# ansible mysql -m command -a 'll /opt'
192.168.110.137 | FAILED | rc=2 >>
[Errno 2] 没有那个文件或目录
eg3:删除一个文件
[root@localhost opt]# ansible mysql -m file -a "path=/opt/fstab.back state=absent"
192.168.110.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/opt/fstab.back",
"state": "absent"
}
eg4:创建一个文件或目录 创建目录加上directory目录参数
[root@localhost opt]# ansible mysql -m file -a "path=/opt/test state=touch"
192.168.110.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/opt/test",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:usr_t:s0",
"size": 0,
"state": "file",
"uid": 0
}
[root@localhost opt]# ansible mysql -m file -a 'path=/opt/shangzhen state=directory mode=755'
192.168.110.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/opt/shangzhen",
"secontext": "unconfined_u:object_r:usr_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}
[root@localhost opt]#
[root@localhost opt]# ansible all -m ping
192.168.110.137 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
192.168.110.136 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
eg1:给目标主机下载软件
[root@localhost opt]# ansible mysql -m yum -a 'name=zsh'
192.168.110.137 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"installed": [
"zsh"
]
},
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\n * base: mirrors.163.com\n * extras: mirrors.huaweicloud.com\n * updates: mirrors.bfsu.edu.cn\nResolving Dependencies\n--> Running transaction check\n---> Package zsh.x86_64 0:5.0.2-34.el7_8.2 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n zsh x86_64 5.0.2-34.el7_8.2 updates 2.4 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 2.4 M\nInstalled size: 5.6 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : zsh-5.0.2-34.el7_8.2.x86_64 1/1 \n Verifying : zsh-5.0.2-34.el7_8.2.x86_64 1/1 \n\nInstalled:\n zsh.x86_64 0:5.0.2-34.el7_8.2 \n\nComplete!\n"
]
}
[root@localhost opt]# ansible mysql -m command -a 'rpm -q zsh'
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'.
If you need to use command because yum, dnf or zypper is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg
to get rid of this message.
192.168.110.137 | CHANGED | rc=0 >>
zsh-5.0.2-34.el7_8.2.x86_64
//创建用户使用无交互模式给用户设置密码
[root@localhost opt]# ansible mysql -m shell -a 'echo abc123|passwd --stdin mysql'
192.168.110.137 | CHANGED | rc=0 >>
更改用户 mysql 的密码 。
passwd:所有的身份验证令牌已经成功更新。
编写脚本
vim script.sh '编写脚本'
#!/bin/bash
echo "abcd" >> /opt/test2.txt
chmod +x test.sh
ansible mysql -m script -a 'test.sh' '执行脚本文件'
[root@localhost opt]# ls
fstab.link inittab.back rh script.txt shangzhen test test1 test2.txt
[root@localhost opt]# cat test2.txt
abcd
[root@localhost opt]#
inventory 存货 库存 详细目录 清单
ansible默认的主机清单是/etc/ansible/hosts文件 主机清单可以手动设置,也可以通过Dynamic Inventory动态生成 一般主机名使用FQDN
vi /etc/ansible/hosts [webserver] #方括号设置组名 www1.example.org #定义被监控主机,这边可以是主机名也可以是IP地址,主机名需要修改/etc/hosts文件 www2.example.org:2222 #冒号后定义远程连接端口,默认是ssh的22端口
如果是名称类似的主机,可以使用列表的方式标识各个主机 [webserver] www[01:50].example.org ansible_ssh_user=root ansible_ssh_pass=123456
[dbbservers] db-[a:f].example.org //支持匹配a b c … f
(1)主机变量 [webserver] www1.magedu.com http_port=80 maxRequestsChild=808 www2.magedu.com http_port=8080 maxRequestsChild=909 (2)组变量 [servers:vars] ntp_server=ntp.example.org nfs_server=nfs.example.org (3)组嵌套 [apache] http1.example.org http2.example.org
[nginx] ngx1.example.org ngx2.example.org
[webservers:children] apache nginx
参数 说明
ansible_ssh_host 将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.
ansible_ssh_port ssh端口号.如果不是默认的端口号,通过此变量设置.
ansible_ssh_user 默认的 ssh 用户名
ansible_ssh_pass ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)
ansible_ssh_private_key_file ssh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况.
ansible_ssh_common_args 此设置附加到sftp,scp和ssh的缺省命令行
ansible_sftp_extra_args 此设置附加到默认sftp命令行。
ansible_scp_extra_args 此设置附加到默认scp命令行。
ansible_ssh_extra_args 此设置附加到默认ssh命令行。
ansible_ssh_pipelining 确定是否使用SSH管道。 这可以覆盖ansible.cfg中得设置。
ansible_shell_type 目标系统的shell类型.默认情况下,命令的执行使用 'sh' 语法,可设置为 'csh' 或 'fish'.
ansible_python_interpreter 目标主机的 python 路径.适用于的情况: 系统中有多个 Python, 或者命令路径不是"/usr/bin/python",比如 *BSD, 或者 /usr/bin/python
ansible_*_interpreter 这里的"*"可以是ruby 或perl 或其他语言的解释器,作用和ansible_python_interpreter 类似
ansible_shell_executable 这将设置ansible控制器将在目标机器上使用的shell,覆盖ansible.cfg中的配置,默认为/bin/sh。
Ansible的脚本—playbook剧本 通过task调用ansible的模板将多个play组织在一个playbook中运行。 playbooks本身由以下各部分组成 (1)Tasks:任务,即调用模块完成的某操作; (2)Variables:变量 (3)Templates:模板 (4)Handlers:处理器,当某条件满足时,触发执行的操作; (5)Roles:角色。
执行一个playbook ansible-playbook [yaml文件名] 例如:ansible-playbook ping.yml 参数:-k(–ask-pass) 用来交互输入ssh密码 -K(-ask-become-pass) 用来交互输入sudo密码 -u 指定用户
补充命令:
ansible-playbook nginx.yaml --syntax-check #检查yaml文件的语法是否正确
ansible-playbook nginx.yaml --list-task #检查tasks任务
ansible-playbook nginx.yaml --list-hosts #检查生效的主机
ansible-playbook nginx.yaml --start-at-task='Copy Nginx.conf' #指定从某个task开始运行
- hosts: webserver //定义的主机组,即应用的主机
vars: //定义变量
http_port: 80
max_clients: 200
user: root
tasks: //执行的任务
- name: ensure apache is at the latest version //显示输出的内容 自定义
yum: pkg=httpd state=latest //参数 检查http是否为最新版本
- name: write the apache config file //name模块必须要写的 不然你不知道哪个模块执行到哪里有问题
template: src=/srv/httpd.j2 dest=/etc/httpd.conf //定义apache的模板 要实现定义好 放在管理端 定义到node src指向源 dest 指向目标
notify: //调用handlers的操作 要重启
- restart apache
- name: ensure apache is running
service: name=httpd state=started
handlers: //处理器 被调用到上面的notify
- name: restart apache
service: name=httpd state=restarted //定义handlers的具体的动作 重启apache
运行yaml文件
[root@localhost opt]# vim webserver.yaml
- hosts: webserver
vars:
http_port: 80
max_clients: 200
user: root
tasks:
- name: ensure apache is at the latest version
yum: pkg=httpd state=latest
- name: write the apache config file
template: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart apache
- name: ensure apache is running
service: name=httpd state=started
handlers:
- name: restart apache
service: name=httpd state=restarted
[root@localhost opt]# ansible webserver -a 'cat /etc/httpd/conf/httpd.conf'
在一个playbook中,我们一般会定义很多个task,如果我们只想执行其中的某一个task或多个task时就可以使用tags标签功能了,格式如下:
vi hosts.yml
- hosts: webserver
remote_user: root
tasks:
- name: task1
file:
path: /opt/t1
state: touch
tags: t1
- name: task2
file: path=/opt/t2
state=touch
tags: t2
- name: task3
file: path=/opt/t3
state=touch
tags: t3
执行命令:ansible-playbook hosts.yml --tags="t1"
可以使用–tags选项指定某个标签,当指定标签后,只有标签对应的任务会被执行,其他任务都不会被执行,执行上述命令后,只有task2会执行,因为task2的标签值为t2,task1和task3都不会执行,这样就达到了只执行playbook中部分任务的目的。
可以使用 --skip-tags选项指定"不执行的任务",执行上述命令后,task1和task3会执行,task2不会执行,因为我们已经在命令中指定了’跳过’标签t2所对应的任务,相当于使用了’排除法’,t2对应的任务被排除了,其他任务都会执行。
[root@localhost opt]# ansible-playbook --skip-tags=‘t2’ host.yaml
scp root@192.168.175.130:/etc/httpd/conf/httpd.conf ./
vi templates/httpd.conf //放在管理端
Listen {{http_port}}
ServerName {{server_name}}
MaxClients {{access_num}}
mv httpd.conf httpd.conf.j2
vi /etc/ansible/hosts
[webserver]
192.168.175.130 http_port=192.168.175.130:80 access_num=100 server_name="www.yun.com:80"
vi apache.yml
---
- hosts: webserver
remote_user: root
vars:
- package: httpd
- service: httpd
tasks:
- name: install httpd package
yum: name={{package}} state=latest
- name: install configure file
template: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: start httpd server
service: name={{service}} enabled=true state=started
handlers:
- name: restart httpd
service: name={{service}} state=restarted
//注意格式对齐
ansible-playbook apache.yml #执行脚本
roles能够根据层次型结构自动装载变量文件、task以及handlers等。简单来讲,roles就是通过分别将变量、文件、任务、模块及处理器放置于单独的目录中,并可以便捷地include它们, roles一般用于基于主机构建服务的场景中,但也可以用于构建守护进程等场景中。
roles内各目录含义解释 files:用来存放由copy模块或script模块调用的文件。 templates:用来存放jinjia2模板,template模块会自动在此目录中寻找jinjia2模板文件。 tasks:此目录应当包含一个main.yml文件,用于定义此角色的任务列表,此文件可以使用include包含其它的位于此目录的task文件。 handlers:此目录应当包含一个main.yml文件,用于定义此角色中触发条件时执行的动作。 vars:此目录应当包含一个main.yml文件,用于定义此角色用到的变量。 defaults:此目录应当包含一个main.yml文件,用于为当前角色设定默认变量。 meta:此目录应当包含一个main.yml文件,用于定义此角色的特殊设定及其依赖关系。
1)创建以roles命令的目录。
mkdir /etc/ansible/roles/ -p #yum装完默认就有
2)创建全局变量目录。
mkdir /etc/ansible/group_vars/ -p
touch /etc/ansible/group_vars/all #文件名自己定义,引用的时候注意
3)在roles目录中分别创建以各角色名称命令的目录,如httpd。
mkdir /etc/ansible/roles/common -p
4)在每个角色命令的目录中分别创建files、handlers、tasks、templates、meta、defaults和vars目录,用不到的目录可以创建为空目录,但不可以不创建。
mkdir /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta} -p
5)在每个角色的handlers、tasks、meta、defaults、vars目录下创建main.yml文件,千万不能自定义。
touch /etc/ansible/roles/httpd/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/mysql/{defaults,vars,tasks,meta,handlers}/main.yml
6)在playbook文件中,调用各角色。
vi /etc/ansible/site.yml
---
- hosts: webserver
remote_user: root
roles:
- httpd
- mysql
创建必须的文件夹
mkdir /etc/ansible/roles/httpd/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/mysql/{files,templates,tasks,handlers,vars,defaults,meta} -p
mkdir /etc/ansible/roles/php/{files,templates,tasks,handlers,vars,defaults,meta} -p
touch /etc/ansible/roles/httpd/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/mysql/{defaults,vars,tasks,meta,handlers}/main.yml
touch /etc/ansible/roles/php/{defaults,vars,tasks,meta,handlers}/main.yml
编写httpd模块 写一个简单的tasks/main.yml
vi /etc/ansible/roles/httpd/tasks/main.yml
- name: ensure apache is at the latest version
yum: pkg={{ pkg }} state=latest
定义变量:可以定义在全局变量中,也可以定义在roles角色变量中,一般定义在角色变量中
vi /etc/ansible/roles/httpd/vars/main.yml
pkg: httpd
编写mysql模块
vi /etc/ansible/roles/mysql/tasks/main.yml
- name: ensure mysql is at the latest version
yum: pkg={{ pkg }} state=latest
vi /etc/ansible/roles/mysql/vars/main.yml
pkg: mariadb*
编写php模块
vi /etc/ansible/roles/php/tasks/main.yml
- name: ensure php is at the latest version
yum: pkg={{ pkg }} state=latest
vi /etc/ansible/roles/php/vars/main.yml
pkg: php
编写roles示例
vi /etc/ansible/site.yml
---
- hosts: webserver
remote_user: root
roles:
- httpd
- mysql
- php