Ansible权威指南笔记(粗略)

简介: #在托管节点上安装python解释器ansible myhost --sudo -m raw -a "yum install -y python2 python-simplejson"#各个平台上安装ansible http://www.

#

在托管节点上安装

python

解释器

ansible myhost --sudo -m raw -a "yum install -y python2 python-simplejson"

#

各个平台上安装

ansible http://www.ansible.com.cn/docs/intro_installation.html

#python

多环境管理:

pyenv

virutalenv

etc/ansible: ansible.cfg hosts

#ansible.cfg

inventory

library

module_name

########################################

ansible all -m ping -u root --sudo

ansible all -m copy -a "src=/etc/fstab dest=/tmp/fstab owner=root group=root

mode=644 backup=yes"

ansible-galaxy init,install,list,remove

ansible-pull

定期执行任务

ansible-doc -l

ansible-doc ping

ansible-playbook playbook.yml

ansible-vault encrypt/decrypt xx.yml 

加密解密

########hosts

192.168.1.1

ntp.mmm.com:222

[xxx]

xx[10:20].xx.com

[xxx:vars]

xx=aa

cc=bb

[webservers:children]

xx

cc

ansible_ssh_host

ansible_ssh_port

ansible_ssh_user

ansible_ssh_pass

ansible_ssh_private_key_file=

############################

ansible webs -m service -a "name=httpd state=restarted"

ansible "webs1:webs2"

ansible "webs1:!xxx"

ansible "webs1:&xx:!xxxx"

ansible ~web1*    #

正则匹配主机

###################################

ansible -i inventory

-f

线程数

--private-key

-m

-M

模块路径

-k

认证密码

-K sudo

密码

-o

输出成一行

-s  sudo

-T

时间

-B

后台执行命令的时间

-u

用户

-l

指定运行的主机

###################

ansible proxy -f 5 -m ping

ansible proyx -s -m command -a "hostname"

ansible proxy --list

time ansible 192.168.1.1 -B 5 -P 2 -T 2 -m command -a 'sleep 20' -u root ##

192.168.1.1

以用户

root

执行

sleep 20

,设置最大连接超时时长为

#2s

,且设置为后台运行模式,执行过程每

2s

输出一次进度,如

5s

还未执行完则终止该任务。

ansible apps -a "df -lh"

ansible apps -m shell -a "free -m"

ansible apps -m yum -a "name=redhat-lsb state=present"

ansible apps -m yum -a "name=ntp state=present"

ansible apps -m service -a "name=ntpd state=started enabled=yes"

ansible apps -m command -a "service ntpd status" --limit "192.168.1.22"

###################################

##

新增用户

ansible db -m user -a 'name=dba shell=/bin/bash groups=admins,dbagroup

append=yes home=/home/dba/ state=present'

ansible db -m user -a 'name=dba groups=ddd append=no'  #

变更用户属性

ansible db -m user -a 'name=dba state=absent remove=yes'  #

删除用户

ansible db -m user -a "name=tom shell=/bin/bash password=to32cxooiudsof

update_password=always" ##

变更密码,密码必须自己

sha-512

生成

ansible xx -m win_user -a "name=stanly password=xxxxxxx group=Administrators" 

##win

用户

ansible xx -m mysql_user -a "login_host=localhost login_password=123456

login_user=root name=stanly password=123456 priv=zabbix.*:ALL state=present"

###

新增

mysql

用户

###########################################################

##yaml

语法

不要使用

tab

相同阶层的元素左对齐

##########

########playbook

语法

---  #

开头

- hosts: webservers

vars:

http_pot: 80

max_clients: 222

remote_user: root

tasks:

- name: ensdlfjsd

yum: pkg=httpd state=latest

- name: xx

template: src=/srv/httpd.j2 dest=/etc/httpd.conf

notify: restart apache

- name: xx

service: name=httpd state=started

handlers:

- name: restart apache

service: name=httpd state=restarted

#################################

############shellplaybook

---

- hosts: all

tasks:

- name: "setup apache"

command: yum install --quiet -y httpd httpd-devel

- name: "copy configure"

command: cp /tmp/httpd.conf /etc/httpd/conf/httpd.conf

command: cp /tmp/httpd-vhosts.conf /etc/httpd/conf/httpd-vhosts.conf

command: service httpd start

command: chkconfig httpd on

####################################################ansible-playbook

./playbook.yml

ansible-playbook --limit xx.com

ansible-playbook --list-hosts  #

列出哪些主机受影响

--remote-user=tom

#########node.js

不全

---

- hosts: all

tasks:

- name: gpg

rpm_key: "key={{ item }} state=present"

with_items:

- "http://rpms.famillecollet.com/RPM-GPG-KEY-remi"

- name: install remi repo

command: "rpm -Uvh --force {{ item.href }} creates={{ item.creates }}"

with_items:

- href: "http://rpms.famillecollet.com/enterprise/remi-release-6.rpm"

- creates: "/etc/yum.repos.d/remi.repo"

yum: name=epel-release state=present

service: name=iptables state=stopped

service: name=firewalld state=stopped

yum: name=npm state=present enablerepo=epel

command: npm config set registry https://registry.npm.taobao.org

command: npm config set strict-ssl false

npm: name=forever global=yes state=latest

file: "path={{ node_apps_location }} state=directory"

copy: "src=app dest={{ node_apps_location }}"

npm: "path={{ node_apps_location }}/app"

########################################################ansible-playbook

--extra-vars="node_apps_location=/usr/local/opt/node"

###ansible

安装部署

drupal https://www.howtoing.com/how-to-create-an-ansible-

playbook-to-automate-drupal-installation-on-ubuntu-14-04/

###ansible

安装部署

tomcat https://www.cnblogs.com/Carr/p/7447309.html 

http://blog.51cto.com/miaocbin/1893759

###ansible

安装部署

JDK  https://www.cnblogs.com/guxiong/p/7241885.html

###handler

模块

handlers:

- name: restart apache

service: name=apache2 state=restarted

notify: restart memcached

- name: restart memcached

service: name=memcached state=restarted

notify:

- restart apache

- restart memcached

###################

ansible-playbook xx.yml --extra-vars "foo=bar"

ansible-playbook xx.yml --extra-vars "@xx.json"

#

主机变量

host1 xx=bar  {{ hostvars['host1']['xx'] }}

#gather_facts: no

##

在远程主机上

/etc/ansible/xx.fact

,则

setu

模块将返回

fact

的内容

[users]

admin=xx

###

##

条件判断

- name:

[task xxx]

when: software_version.split('.')[0] == '4'

##

##register

- command: my-app --status

register: myapp_result

-command: do-something-to-my-app

when: "'ready' in myapp_result.stdout"

#myapp_result.stdout.find('xxx') == -1

####################

##

如果

PHP

7

,则降级

- shell: php --version

register: php_version

- shell: yum -y downgrade php*

when: "'7.0' in php_version.stdout"

#####

####

如果主机的

hosts

不存在,就传一个文件过去

- stat: path=/etc/hosts

register: hosts_file

- copy: src=path/local/file dest=/path/remote/file

when: hosts_file.stat.exists == false

################################

#

交互

hosts: all

vars_prompt:

- name: user

prompt: "what is your name"

#######confirm

为用户输入两次

#########wait_for

模块 

delegate_to

模块

tags

标签 

block

块功能 

roles

功能 

jinja2

语法

#

#######################

######include

功能

tasks:

- include: xx.yml

---

- include: xx.yml

- include: yy.yml

when: extra_file.stat.exists

################################

ansible-galaxy install username.rolename

################################################################################

####

ansible all -i inventory.py -m ping  #

动态创建主机

目录
相关文章
|
网络安全 数据安全/隐私保护
Ansible之 Tower使用User和Team管理访问权限的笔记
写在前面 这部分内容没有太大的差别,所以用旧版本的 Ansible Tower 博文内容涉及: 创建 Tower 用户即角色添加 创建 Tower 团队 即角色添加 食用方式: 需要了解 Ansible 理解不足小伙伴帮忙指正
156 0
Ansible之 Tower使用User和Team管理访问权限的笔记
|
存储 JSON 安全
Ansible之 AWX 管理清单和凭据的一些笔记
写在前面 分享一些 AWX 创建清单和凭据的笔记 博文内容涉及: 创建静态清单,清单角色,变量的配置 创建凭据,凭据类型,角色等配置 使用创建清单和凭据运行 ad-hoc 的 Demo 食用方式: 需要了解 Ansible 理解不足小伙伴帮忙指正
230 0
Ansible之 AWX 管理清单和凭据的一些笔记
|
存储 Kubernetes 关系型数据库
关于K8s中Ansible AWX(awx-operator)平台Helm部署的一些笔记
整理一些K8s中通过Helm的方式部署AWX的笔记分享给小伙伴 博文内容为部署过程和遇到问题的解决过程 食用方式: 需要了解K8s 需要预置的K8s+Helm环境 需要科学上网 理解不足小伙伴帮忙指正 嗯,疫情快点结束吧 ^_^
857 0
关于K8s中Ansible AWX(awx-operator)平台Helm部署的一些笔记
《Ansible权威指南 》一1.9 本章小结
Ansible是运维自动化工具的后起之秀。本章前半部分我们学习了Ansible是什么,底层通信发展史,Ansible发展历程等概念性知识。后半部分我们详细介绍了Ansible安装部署方式,同时考虑本地复杂环境可能导致的部署问题,本章后半部分我们也引申介绍了Python多环境扩展管理,以方便大家应对部署过程中可能出现的各类问题。
1436 0
|
Web App开发 运维 Python
《Ansible权威指南 》一 导读
对比主流的自动化工具SaltStack、Puppet等,Ansible给人最直观的感觉就是比较简单,而这也是笔者选择使用Ansible最重要的理由之一。因为笔者一直认为每个人精力有限,如腾讯早期的Ops技能培训希望个人同时兼备Ops和Dev的战略,但直到现在身边真正同时具备Dev和Ops能力于一身的人凤毛麟角。
3895 0
《Ansible权威指南 》一 3.5 本章小结
Ansible Ad-Hoc在运维日常工作中的作用举足轻重,日常工作中的临时并发性操作均通过Ad-Hoc协助完成,因此我们花了很多篇幅为大家介绍其使用及企业实践。
811 0
|
JavaScript 算法 Linux
《Ansible权威指南 》一3.4 Ad-Hoc用户与组管理
用户权限管理是运维日常最重要的管理工作之一,如生产环境禁用开发和测试人员登录变更,但测试环境的用户权限仍需耗费精力维护,这项工作大公司也存在(将测试环境交给测试或开发管理并不是每个公司都能做到的,但未来是趋势)。
2713 0
|
Web App开发 JavaScript 关系型数据库
《Ansible权威指南 》一 3.3 Ad-Hoc组管理和特定主机变更
Ansible也提供了简洁但强大的组管理功能。同时,我们也可能遇到只针对这组主机中一台或某些主机做变更的场景,针对这些复杂多变的企业场景,本节我们将深入了解Ad-Hoc组管理和特定主机变更,进一步了解Ansible如何应对复杂多变的企业环境。
2383 0
|
JavaScript 前端开发 Linux
《Ansible权威指南 》一3.2 Ad-Hoc命令集介绍
本节介绍通过Ad-Hoc命令集查看系统设置,通过Ad-Hoc研究Ansible的并发特性,通过Ad-Hoc研究Ansible的模块使用。俗话说,磨刀不误砍柴工。开始之前做一些简单的初始化检查,如系统时间正确与否、磁盘容量是否充足等,是很有必要的。
2272 0