应用环境:批量部署软件
备注:ansible不需要在管理设备安装agent,通过ssh进行管理。
准备服务器:
服务器端:192.168.25.129
客户端:192.168.25.130
客户端:192.168.25.131
验证
yum -y install epel-release
yum -y install ansible
用户名加密码方式测试:
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‘
清单定义变量:
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
ansible host1 -m ping
处理警告:修改/etc/ansible/hosts文件
或者添加用户组:修改选项webservers 添加客户机web1 host1
测试成功
如果有询问,可去掉(yes/no)的询问
修改/etc/ssh/ssh_config 为StrictHostKeyChecking no
ansible host1 -m shell -a ‘date‘
ansible host1 -m yum -a ‘httpd‘ state=latest‘
使用ansible支持得yum模块安装软件包state状态latest最新
ansible webserver -m shell -a ‘hostname‘ -o -f 2
ansible webserver -m copy -a ‘src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777‘
ansible host2 -m yum -a ‘name="httpd" state=latest‘(安装apache)
ansible host1 -m yum -a ‘name="*" state=latest‘(升级所有包)
[root@ansible ~]# ansible webservers -m service -a ‘name=httpd state=started enabled=yes‘ -f 3 -o
创建用户
ansible webservers -m user -a ‘name=qianfeng state=present‘
删除用户
ansible webservers -m user -a ‘name=qianfeng state=absent‘
facts组件是Ansible用于采集被管理主机信息的一个功能,可以使用 setup模块查看主机的有的facts信息。
ansible webservers -m setup
http://docs.ansible.com/ansible/YAMLSyntax.html
yum install httpd 安装
cp -rf /etc/httpd/conf/httpd.conf /home/shao/yaml/apache/ 备份
配置文件监听端口80修改为Listen 8080
触发复制文件时,重启apache服务
[root@localhost apache]# cat apache.yaml
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
构建示例目录:
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
编写tasks:备注没有epel packge源会找不到nginx包
编写handlers:
编写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
如果对您有所帮助请《点赞》、《收藏》、《转发》,您的支持是我持续更新的动力,有疑问请留言
ansible部署以及使用(apache、nginx批量部署)
原文:https://blog.51cto.com/u_13241097/2827488