Ansible權威指南筆記(粗略)

#

在托管節(jié)點上安裝

python

解釋器

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

#

各個平臺上安裝

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

#python

多環(huán)境管理:

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

定期執(zhí)行任務

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

線程數(shù)

--private-key

-m

-M

模塊路徑

-k

認證密碼

-K sudo

密碼

-o

輸出成一行

-s? sudo

-T

時間

-B

后臺執(zhí)行命令的時間

-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

執(zhí)行

sleep 20

,設置最大連接超時時長為

#2s

反砌,且設置為后臺運行模式贱纠,執(zhí)行過程每

2s

輸出一次進度,如

5s

還未執(zhí)行完則終止該任務蝌衔。

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? #

動態(tài)創(chuàng)建主機

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末皮胡,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子赏迟,更是在濱河造成了極大的恐慌屡贺,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異甩栈,居然都是意外死亡泻仙,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進店門量没,熙熙樓的掌柜王于貴愁眉苦臉地迎上來玉转,“玉大人,你說我怎么就攤上這事殴蹄【孔ィ” “怎么了?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵袭灯,是天一觀的道長刺下。 經常有香客問我,道長稽荧,這世上最難降的妖魔是什么橘茉? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮姨丈,結果婚禮上畅卓,老公的妹妹穿的比我還像新娘。我一直安慰自己蟋恬,他們只是感情好翁潘,可當我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著筋现,像睡著了一般。 火紅的嫁衣襯著肌膚如雪箱歧。 梳的紋絲不亂的頭發(fā)上矾飞,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天,我揣著相機與錄音呀邢,去河邊找鬼洒沦。 笑死,一個胖子當著我的面吹牛价淌,可吹牛的內容都是我干的申眼。 我是一名探鬼主播,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼蝉衣,長吁一口氣:“原來是場噩夢啊……” “哼括尸!你這毒婦竟也來了?” 一聲冷哼從身側響起病毡,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤濒翻,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體有送,經...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡淌喻,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了雀摘。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片裸删。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖阵赠,靈堂內的尸體忽然破棺而出涯塔,到底是詐尸還是另有隱情,我是刑警寧澤豌注,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布伤塌,位于F島的核電站,受9級特大地震影響轧铁,放射性物質發(fā)生泄漏每聪。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一齿风、第九天 我趴在偏房一處隱蔽的房頂上張望药薯。 院中可真熱鬧,春花似錦救斑、人聲如沸童本。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽穷娱。三九已至,卻和暖如春运沦,著一層夾襖步出監(jiān)牢的瞬間泵额,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工携添, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留嫁盲,地道東北人。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓烈掠,卻偏偏與公主長得像羞秤,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子左敌,可洞房花燭夜當晚...
    茶點故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內容

  • 作為背鍋俠運維工作的基本流程 運維工具的分類 : ansible的模塊化: ansible密鑰登陸 ansible...
    二郎5閱讀 4,159評論 0 10
  • ###### Ansible總結 ##### 運維工作: 系統(tǒng)安裝(物理機瘾蛋、虛擬機)-->程序包安裝、配置矫限、服務啟...
    二郎5閱讀 2,031評論 0 4
  • 1)安裝2)常用模塊3)inventory4)playbook(role\tag\template)5) yaml...
    秦記閱讀 4,170評論 2 5
  • 一.ansible (1) ansible: ansible是一款新出現(xiàn)的自動化運維系統(tǒng)瘦黑,基于python開發(fā)并集...
    楠人幫閱讀 1,943評論 0 8
  • 1. 什么是Ansible京革,它有什么用? Ansible它是個集配置管理和應用部署于一體的自動化運維工具幸斥。 應用情...
    午覺不眠Orz閱讀 1,518評論 0 0