标签:install war mysql 简单 groups tree 日志功能 迭代 processor
ansibleansible不是以服务的方式运行,用户需要时执行
首先必须设置主机清单? vim?/etc/ansible/hosts? ,添加如下内容:
[web]
172.16.41.51
172.16.41.61
[beifen]
172.16.41.88
172.16.41.152
ansible 172.16.41.61 -m ping? 如果主机清单中未设置172.16.41.61,则此命令执行报错
[root@centos76 11:13 .ssh]# ansible 172.16.41.61 -m ping
[WARNING]: Could not match supplied host pattern, ignoring: 172.16.41.61
[WARNING]: No hosts matched, nothing to do
主机清单添加对应的地址后执行命令:第一次执行会出现yes/no的提示
ansible 172.16.41.51 -m ping
The authenticity of host ‘172.16.41.51 (172.16.41.51)‘ can‘t be established.
ECDSA key fingerprint is SHA256:ipiclPOhfzuGppT1kscx7sIW5oadl2G+VKKgmwi4NX0.
ECDSA key fingerprint is MD5:f1:40:3e:af:56:a8:95:df:f6:bc:5a:f3:13:a6:fe:e1.
Are you sure you want to continue connecting (yes/no)? yes
取消此项设置:在配置文件中vim /etc/ansible/ansible.cfg? 取消下面的注释
#host_key_checking = False修改为host_key_checking = False
测试命令,提示下列错误,即验证失败,未设置是基于口令或基于publickey验证
[root@centos76 11:15 .ssh]# ansible 172.16.41.61 -m ping
172.16.41.61 | UNREACHABLE! => {
????"changed": false,
????"msg": "Failed to connect to the host via ssh: Warning: Permanently added ‘172.16.41.61‘ (RSA) to the list of known hosts.\r\nPermission denied (publickey,password).",
????"unreachable": true
}
需要添加选项 -k? ?ansible 172.16.41.61 -m ping -k?然后输入主机对应默认的root密码即可成功,而且只可以输入一次,如果多个主机口令不一致会导致执行失败
ansible <host-pattern> [-m module_name] [-a args] 选项定义:
-m?module? ?指定模块,默认为command? ??
-k? ?提示输入ssh连接密码,默认key验证? ? ??
-u? ?指定远程执行的用户? ? ?
-b? ?sudo切换? ??
-K? ?提示输入sudo时的口令
-C? ?检查,并不执行
--list-hosts? ?显示主机列表,可简写 --list
-v? ?详细过程 –vv -vvv更详细
基于key验证,需要将在安装ansible的机器上生成ssh公钥并拷贝值主机清单的机器上
ssh-keygen;ssh-copy-id 172.16.41.51;;ssh-copy-id 172.16.41.61;ssh-copy-id 172.16.41.152;ssh-copy-id 172.16.41.88??ssh-copy-id 172.16.41.52
测试分组的连接情况:ansible web/beifen/all -m ping
查看组的主机列表:ansible all --list-host? ?ansible all --list? ?ansible?web?--list-host? ? ??ansible?beifen--list-host
配置文件修改内容:
log_path = /var/log/ansible.log? ? 取消此行的注释,开启日志功能
host_key_checking = False? ? ? ? ? ? ?检查对应服务器的host_key,建议取消注释
#module_name = command? ? ? ? ? 将此行修改为下面对应的内容,将默认的模块名修改为shell?
module_name = shell? ? ? ? ? ? ? ? ? ? ?
host-pattern:
[root@centos76 12:40 .ssh]# ansible ‘web:!beifen‘ --list? ?
[root@centos76 12:40 .ssh]# ansible "web:!beifen" --list
注意上述示例中单引号和双引号的标记,如果使用非 !? 的话需要使用单引号,使用双引号会报错
1、command模块:执行命令? ? ? ? ?ansible-doc -s command
ansible web -m command -a ‘cat /etc/redhat-release‘
ansible web -m command -a ‘chdir=/etc cat redhat-release‘
ansible web -m command -a ‘ls /data‘
ansible web -m command -a ‘rm -rf??/data‘
ansible web -m command -a ‘ls /data‘
ansible web -m command -a ‘hostname‘
ansible web -m command -a ‘echo $HOSTNAME‘? ? ? ? ? ?不可以显示出预想的结果,结果是
ansible web -m command -a "echo $HOSTNAME"? ? ? ? ??不可以显示出预想的结果,结果是
ansible web -m command -a "echo 123456|passwd --stdin root"? ? ? ? ? ? ??不可以显示出预想的结果,结果是
2、shell模块 :执行命令??注意单引号双引号 ? ? ? ? ? ? ? ? ? ansible-doc -s shell
ansible web -m??shell -a ‘echo $HOSTNAME‘? ? ?注意使用的是单引号 ? ? ? ?使用双引号ansible web -m??shell -a "echo $HOSTNAME"?返回的是ansible的主机名
ansible web -m shell -a ‘chdir=/data touch a.txt‘
ansible web -m shell -a ‘ls /data‘
ansible web -m shell -a ‘sed -i "s/SELINUX=disabled/SELINUX=enforcing/" /etc/selinux/config‘
ansible web -m shell -a ‘cat??/etc/selinux/config‘
ansible web -m shell -a ‘sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config‘
ansible web -m shell -a ‘yum remove -y httpd‘
3、script模块:执行脚本? ? ? ? ? ?ansible-doc -s script
ansible web -m script -a ‘/data/test.sh‘
4、copy模块 :拷贝文件? ? ? ? ? ? ?ansible-doc -s copy
ansible web -m copy -a ‘src=/etc/fstab dest=/data/151 mode=777 owner=wang group=wang backup=yes‘? ? 指定备份backup
复制目录和文件夹:ansible web -m copy -a ‘src=/etc/sysconfig dest=/data/‘
指定内容生成文件:ansible web -m copy -a ‘content=wangbin\nceshi\n123\n456 dest=/data/copy.txt‘
ansible web -m copy -a ‘content=[test]\nname=test\nurl=test dest=/data/test.txt‘
5、fetch模块 :抓取文件,当前不支持抓取文件夹和目录? ?ansible-doc -s fetch
ansible web -m fetch -a ‘src=/data/test.txt dest=/data‘?? ? ? ??
ansible web -m shell -a ‘tar jcvf /root/data.tar.bz2 /data‘? ?打包命令
ansible web -m fetch -a ‘src=/root/data.tar.bz2 dest=/data‘? ?抓取打包文件
6、file模块:设置文件属性? ?ansible-doc -s file
ansible web -m file -a ‘path=/data/test.log owner=wang mode=777‘
ansible web -m file -a ‘src=/data/test.log name=/data/test.link state=link‘? ?创建软连接? ?state=hard? 创建硬链接
ansible web -m file -a ‘path=/data/dir1??state=directory‘? ? ? ? ? 创建目录
ansible web -m file -a ‘path=/data/f1.txt??state=touch‘? ? ? ? ? ? ?创建文件
ansible web -m file -a ‘path=/data/f1.txt??state=absent‘? ? ? ? ? ? 删除文件
ansible web -m file -a ‘path=/data/dir1 state=absent‘? ? ? ? ? ? ? ?删除目录? ? ? ? ? ? ?absent 关键字
ansible web -m file -a ‘path=/data/ state=absent‘? ? ? ? ? ? ? ? ? ? 删除所有文件,此种用法不可用,不会删除所有的文件
ansible web -m shell -a ‘rm -rf /data/"‘? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 这样删除目录下的所有文件
7、hostname模块:修改主机名
ansible 172.16.41.61 -m hostname -a ‘name=6.10-mini‘? ? ? ? ? ? ? ? ? ? ? ? ?centos6? 修改了/etc/sysconfig/network文件? 未修改/etc/hosts
ansible 172.16.41.51 -m hostname -a ‘name=7.6-mini-DNS‘? ? ? ? ? ? ? ? ? centos7? 修改了/etc/hostname? 未修改/etc/hosts
8、cron模块:计划任务
ansible web -m cron -a ‘name=synctime minute=/5 job="/usr/sbin/ntpdate 172.16.41.151 &> /dev/null"‘
ansible web -m cron -a ‘name=synctime minute=/5 job="/usr/sbin/ntpdate 172.16.41.151 &> /dev/null" disabled=true‘? ? ? ? ? 禁用定义的计划任务,实际上就是注释掉计划任务
ansible web -m cron -a ‘name=synctime minute=/5 job="/usr/sbin/ntpdate 172.16.41.151 &> /dev/null" disabled=false‘? ? ? ? ?启用禁用的计划任务
ansible web -m cron -a ‘name=synctime minute=/5 job="/usr/sbin/ntpdate 172.16.41.151 &> /dev/null" state=absent‘? ? ? ? ? ?删除计划任务
ansible web -m shell -a ‘crontab -l ‘
9、yum模块:管理包
ansible web -m yum -a ‘name=ntpdate‘
ansible 172.16.41.88 -m yum -a ‘name=ntpdate state=absent‘? ?卸载包
10、service模块:管理服务
ansible web -m service -a ‘name=named state=started enabled=yes‘? ? ? ? ? ?enabled?设置为true和yes都可以
ansible web -m shell -a ‘ss -ntlu|grep 53‘
ansible web -a ‘service named status ‘
11、user模块:用户管理
ansible web -m user -a ‘name=wangbin?system=yes?shell=/sbin/nologin‘?
ansible web -m user -a ‘name=mysql state=absent remove=yes‘
remove=yes? 删除用户家目录 ? state=absent? 移除用户
12、group组:管理组
ansible-galaxy? ?角色
连接 https://galaxy.ansible.com 下载相应的roles
列出所有已安装的galaxy
?? ?ansible-galaxy list
安装galaxy
?? ?ansible-galaxy install geerlingguy.redis
删除galaxy
ansible-galaxy remove geerlingguy.redis
查看相关文件:tree /root/.ansible/roles/
ansible-pull
ansible-vault
ansible-vault encrypt hello.yml? ? ? ? ?加密
功能:管理加密解密yml文件
ansible-vault [create|decrypt|edit|encrypt|rekey|view]
ansible-vault encrypt hello.yml 加密
ansible-vault decrypt hello.yml 解密
ansible-vault view hello.yml 查看
ansible-vault edit hello.yml 编辑加密文件
ansible-vault rekey hello.yml 修改口令
ansible-vault create new.yml 创建新文件
ansible-console:交互式
? ? -C:测试但不执行,干跑模式
ansible-playbook -C hello.yml
vim?hello.yml? 注意语法结果,严格要求空格个数和对齐
#hello world yml file
yaml语法:
列表? 其所有元素都使用 -? 打头
字典? 通常由多个key和value构成? ?kay: value? 注意冒号? :? 后面一定要有空格 多个之间使用? ,? 分隔
#install httpd
??tasks:
????- name: install httpd
??????yum: name=httpd
????- name: copy conf
??????copy: src=/data/httpd.conf dest=/etc/httpd/conf/ backup=yes
????- name: service
??????service: name=httpd state=started enabled=yes
#user add
#install httpd
??tasks:
????- name: install httpd
??????yum: name=httpd
????- name: copy conf
??????copy: src=files/httpd.conf dest=/etc/httpd/conf/ backup=yes
??????notify: restart service
????- name: service
??????service: name=httpd state=started enabled=yes
??????
??handlers:
????- name: restart service
??????service: name=httpd state=restarted
tags? ?执行时使用-t?参数,多个参数之间使用逗号分隔? 多个不同的动作可以使用相同的标签
ansible-playbook -t config,service httpd.yml
??- hosts: web
????remote_user: root
????vars:
??????- port: 4848
????vars_files:
??????- vars.yml? ? ? ? ? ? ? ? ? ? ? ? ??vars.yml是独立的变量YAML文件
????tasks:
??????- name: file
????????file: name=/data/{{ansible_nodename}}{{mark}}{{port}}.log state=touch
变量优先级:命令行变量(-e)? >>独立的变量YAML文件>>playbook变量>主机清单普通变量>主机清单公共(组)变量
模版template? ??根据模块文件动态生成对应的配置文件
文本文件,嵌套有脚本(使用模板编程语言编写)
Jinja2语言,使用字面量,有下面形式
字符串:使用单引号或双引号
数字:整数,浮点数
列表:[item1, item2, ...]? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?可以修改
元组:(item1, item2, ...)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?不可以修改
字典:{key1:value1, key2:value2, ...}
布尔型:true/false
算术运算:+, -, *, /, //, %, **? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//?整除
比较操作:==, !=, >, >=, <, <=
逻辑运算:and,or,not
流表达式:For,If,When
template功能:根据模块文件动态生成对应的配置文件
template文件必须存放于templates目录下,且命名为 .j2 结尾
在放置yaml文件的目录下?创建?templates目录
yml 文件需和templates目录平级,目录结构如下:
./
├── temnginx.yml
└── templates
? ? ? ? ? ? ? └── nginx.conf.j2
#nginx
??- hosts: web7
????remote_user: root
????tasks:
??????- name: package
????????yum: name=nginx
??????- name: conf
????????template: src=nginx.j2 dest=/etc/nginx/nginx.conf
????????notify: restart service
??????- name: service
????????service: name=nginx state=started enabled=yes
????handlers:
??????- name: restart service????????????????????????????????????????????????????????????????????????????????????????
????????service: name=nginx state=restarted
nginx配置进程个数:
worker_processes {{ansible_processor_vcpus*2+2}};
引用模版:
#install httpd
??tasks:
????- name: install httpd
??????yum: name=httpd
????- name: copy conf 7
??????template: src=templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
??????when: ansible_distribution_major_version == "7"
??????notify: restart service
????- name: copy conf 6??????????????????????????????????????????????????????????????????????????????????????????????
??????template: src=templates/httpd6.conf.j2 dest=/etc/httpd/conf/httpd.conf
??????when: ansible_distribution_major_version == "6"
??????notify: restart service
????- name: service
??????service: name=httpd state=started enabled=yes
??handlers:
????- name: restart service
??????service: name=httpd state=restarted
迭代:
Playbook中template for if
vim test.for.j2
{%for i in test_ports%}
server {
? ? listen {{i}}
? ? servername www.a.com
? ? root /app/websitea
}
{%endfor%}
vim test.for.jml?
vim test.for.jml?
vim test2.for.jml?
标签:install war mysql 简单 groups tree 日志功能 迭代 processor
原文地址:https://blog.51cto.com/12589011/2386260