ansible部署
应用环境:批量部署软件
备注:ansible不需要在管理设备安装agent,通过ssh进行管理。
准备服务器:
服务器端:192.168.25.129
客户端:192.168.25.130
客户端:192.168.25.131
1、/etc/hosts文件添加客户端信息。(地址解析)
验证
2、通过yum使用epel源安装。
yum -y install epel-release
3、安装ansible
yum -y install ansible
4、服务器连接客户端同步状态(配置公钥非必须)。
用户名加密码方式测试:
ansible web1 -m ping -u root -k
举例:
不支持key公钥可以定义主机清单/etc/ansible/hosts 配置用户密码指定端口
[webserver]
host1 ansible_ssh_user=‘root‘ ansible_ssh_pass=‘777777‘ ansible_ssh_port=‘2222‘
host[2:4] ansible_ssh_user=‘root‘ ansible_ssh_pass=‘666666‘
子分组定义:
[apache]
host[1:2]
[nginx]
host[3:4]
[webserver:children]
apache
nginx
[webserver:vars]
ansible_ssh_user=‘root‘
ansible_ssh_pass=‘666666‘
清单定义变量:
Ansible使用
1、Ansible基础
rpm -ql ansible列出所有文件
rpm -qc ansible查看配置文件
ansible --help查看ansible帮助
ansible-doc -l看所有模块(A10,华为,docker,EC2,aws等等广大厂商设备)
ansible-doc -s yum
yum list Package name enablerepo
2测试连通性(此处ping为探测22端口)
ansible host1 -m ping
处理警告:修改/etc/ansible/hosts文件
或者添加用户组:修改选项webservers 添加客户机web1 host1
测试成功
如果有询问,可去掉(yes/no)的询问
修改/etc/ssh/ssh_config 为StrictHostKeyChecking no
3、执行远程命令
ansible host1 -m shell -a ‘date‘
ansible host1 -m yum -a ‘httpd‘ state=latest‘
使用ansible支持得yum模块安装软件包state状态latest最新
4、ansible组件Ad-Hoc-点对点模式(执行简单命令一条命令)
备注:ansible-doc 模块名称查看使用规则 ansible-doc -l查看所有模块
执行命令 -m shell (-f 2 指定线程数)
ansible webserver -m shell -a ‘hostname‘ -o -f 2
复制文件 -m copy(src源dest目的owner所有者group所属组mode权限)
ansible webserver -m copy -a ‘src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777‘
软件包管理 -m yum
ansible host2 -m yum -a ‘name="httpd" state=latest‘(安装apache)
ansible host1 -m yum -a ‘name="*" state=latest‘(升级所有包)
服务管理 -m service
[root@ansible ~]# ansible webservers -m service -a ‘name=httpd state=started enabled=yes‘ -f 3 -o
用户管理 -m user
创建用户
ansible webservers -m user -a ‘name=qianfeng state=present‘
删除用户
ansible webservers -m user -a ‘name=qianfeng state=absent‘
5、Ansible组件 - Facts
facts组件是Ansible用于采集被管理主机信息的一个功能,可以使用 setup模块查看主机的有的facts信息。
ansible webservers -m setup
6、Ansible组件 - playbook
YAML-YAML Ain’t Markup Language-非标记语言
http://docs.ansible.com/ansible/YAMLSyntax.html
示例准备:远程安装apache,并推送本机配置的apache配置文件(先本机安装apache备份并修改配置文件)
yum install httpd 安装
cp -rf /etc/httpd/conf/httpd.conf /home/shao/yaml/apache/ 备份
配置文件监听端口80修改为Listen 8080
示例编写:安装apache 并上传修改的配置文件
触发复制文件时,重启apache服务
[root@localhost apache]# cat apache.yaml
- hosts: all
tasks:- name: install apache package
yum: name=httpd state=present - name: copy conf
copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart apache service - name: apache run
service: name=httpd state=started enabled=yes
handlers: - name: restart apache service
service: name=httpd state=restarted
- name: install apache package
示例测试执行:
ansible-playbook apache.yaml --syntax-check 检验语法
ansible-playbook apache.yaml --list-tasks 列出任务
ansible-playbook apache.yaml --list-hosts 列出主机
ansible-playbook apache.yaml 执行
[root@localhost apache]# ansible-playbook apache.yaml --syntax-check
playbook: apache.yaml
[root@localhost apache]# ansible-playbook apache.yaml --list-tasks
playbook: apache.yaml
play #1 (all): all TAGS: []
tasks:
install apache package TAGS: []
copy conf TAGS: []
apache run TAGS: []
[root@localhost apache]# ansible-playbook apache.yaml --list-hosts
playbook: apache.yaml
play #1 (all): all TAGS: []
pattern: [u‘all‘]
hosts (2):
web1
host1
[root@localhost apache]# ansible-playbook apache.yaml
PLAY [all] *****
TASK [Gathering Facts] *****
ok: [host1]
ok: [web1]
TASK [install apache package] **
changed: [web1]
changed: [host1]
TASK [copy conf] *****
changed: [host1]
changed: [web1]
TASK [apache run] **
changed: [host1]
changed: [web1]
RUNNING HANDLER [restart apache service] ***
changed: [web1]
changed: [host1]
PLAY RECAP *****
host1 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web1 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
7、Ansible-roles-角色(构建roles的playbooks目录架构)
示例:通过roles远程部署nginx并配置(roles名称可随意命名)
构建示例目录:
mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p
touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml
echo 1234 > roles/nginx/files/index.html
本机安装nginx 并备份配置文件
yum install -y nginx && cp /etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.j2
site:意思为地址
编写任务
编写site.yaml
- hosts: host1
roles:- nginx
编写tasks:备注没有epel packge源会找不到nginx包
- name: install epel packge
yum: name=epel-release state=present - name: install nginx packge
yum: name=nginx state=latest - name: copy index.html
copy: src=index.html dest=/usr/share/nginx/html/index.html - name: copy nginx.conf template
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart nginx - name: make sure nginx service running
service: name=nginx state=started enabled=yes
编写handlers:
- name: restart nginx
service: name=nginx state=restarted
编写vars:
worker_connections: 1024
编写推送的模板文件template:nginx配置文件为nginx.conf.j2
user nginx;worker_processes {{ ansible_processor_cores }};
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {worker_connections {{ worker_connections }};
}
执行playbook:
ansible-playbook site.yaml --syntax-check 检验语法
ansible-playbook site.yaml --list-tasks 列出任务
ansible-playbook site.yaml --list-hosts 列出主机
ansible-playbook site.yaml 执行
报错信息1:ngnix已经运行重启服务器后报错消失。
PLAY [web1] ***
TASK [Gathering Facts] ****
ok: [web1]
TASK [nginx : install epel packge] ****
ok: [web1]
TASK [install nginx packge] ***
ok: [web1]
TASK [nginx : copy index.html] ****
ok: [web1]
TASK [copy nginx.conf template] ***
ok: [web1]
TASK [make sure nginx service running] ****
fatal: [web1]: FAILED! => {"changed": false, "msg": "Unable to start service nginx: Job for nginx.service failed because the control process exited with error code. See \"systemctl status nginx.service\" and \"journalctl -xe\" for details.\n"}
PLAY RECAP ****
web1 : ok=5 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
如果对您有所帮助请《点赞》、《收藏》、《转发》,您的支持是我持续更新的动力,有疑问请留言