特性node
Ansible 主要組層部分python
相關文件nginx
Ansible 文件配置web
ansible 經過 ssh 實現配置管理,應用部署,任務執行等功能,所以,須要實現配置 ansible 端能基於祕鑰認證的方式聯繫各被管理節點shell
ansible 命令用法編程
ansible <host-pattern> [-m module_name] [-a args] --version #顯示版本 -m module # 指定模塊,默認爲 command -v # 詳細過程 -vv -vvv 更詳細的 --list-hosts # 顯示主機列表,可簡寫 --list -k, --ask-pass # 顯示鏈接密碼,默認key驗證 -C,--check # 檢查,並不執行 -T, --timeout=TIMEOUT # 執行命令的超時時間,默認10s -u, --user = REMOTE_USER # 執行遠程執行的用戶 -b, --become # 代替舊版的 sudo 切換
主配置文件ansible.cfg 配置緩存
[default] inventory = /etc/ansible/host # 主機列表配置文件 library = /usr/share/my_modules # 哭文件存放目錄 remote_tmp = $HOME/.ansible/tmp # 臨時py命令文件存放在運城主機目錄 local_tmp = $HOME/.ansible/tmp # 本機的臨時命令執行目錄 forks = 5 # 默認併發數 sudu_user = root # 默認sudo用戶 ask_sudo_pass = True # 每次執行 ansible 命令是否詢問ssh密碼 ask_pass = True remote_port = 22 host_key_checking = False # 檢查對應服務器的host_key,建議取消註釋
log_path = /var/log/ansible.log # 默認日誌 建議開啓
主機清單 hosts 配置安全
# 分組配置,一臺主機能夠再多個組內, 默認 分組 all 所有主機 [web] 172.16.0.40 [db] 172.16.0.40 172.16.0.41 # www[001:006].example.com
ansible 命令執行過程bash
1. 加載本身的配置文件 默認/etc/ansible/ansible.cfg 2. 加載本身對應的模塊文件 如: command 3. 經過 ansible 將模塊或命令生成對應的臨時 py 文件,並將該文件傳輸至遠程服務器的對應執行用戶 $HOME/.ansible/tmp/ansible-tmp-數字/xxx.py 文件 4. 給文件 +x 執行權限 5. 執行並返回結果 6. 刪除臨時 py 文件, sleep 0 退出 # 執行狀態 綠色: 執行成功而且不須要作改變的操做 黃色: 執行成功而且對目標主機作變動 紅色: 執行失敗
模塊服務器
# command #在遠程主機執行命令,默認模塊,可忽略 -m 選項,變量 重定向 管道符等有問題,須要使用 shell模塊 - chdir # 改變工做目錄 ~]# ansible all -m command -a "chdir=/var/log/ ls" - creates # 若是文件存在 則不執行後續命令 ~]# ansible all -m command -a "creates=/etc/fstab ls /" 172.16.0.40 | SUCCESS | rc=0 >> skipped, since /etc/fstab exists 172.16.0.41 | SUCCESS | rc=0 >> skipped, since /etc/fstab exists - removes # 若是文件存在 則執行後續命令 ~]# ansible all -m command -a "removes=/etc/fstab ls /" # shell # 和 command 類似,用shell 執行命令 命令要用單引號 ~]# ansible all -m shell -a 'echo $HOSTNAME' # Script : 運行腳本, 本機腳本在遠程主機上運行 ~]# ansible all -m script -a '/data/test.sh' # copy 從服務器複製文件到客戶端 ~]# ansible all -m copy -a 'src=/data/test.sh dest=/data/ backup=yes mode=600 owner=test'
~]# ansible all -m copy -a 'content="df -h\nhostname\nls\n" dest=/data/f1.sh' # src 源文件 # dest 目標文件 # backup=yes 若是存在同名文件是否備份 # mode 文件權限 # owner 全部者
# content 把內容寫到某個文件
和 copy 模塊相反,不能抓取目錄,目錄可先tar ~]# ansible all -m fetch -a 'src=/etc/passwd dest=/data' ~]# tree /data/ /data/ ├── 172.16.0.40 │ └── etc │ └── passwd ├── 172.16.0.41 │ └── etc │ └── passwd
# 建立一個空文件 ~]# ansible all -m file -a 'path=/data/testfile state=touch mode=600 owner=test group=test' # 建立鏈接文件 ~]# ansible all -m file -a 'src=/data/testfile path=/tmp/testfile-link state=link' # 建立空文件夾 ~]# ansible all -m file -a 'path=/data/test1 state=directory' # 刪除文件夾 ~]# ansible all -m file -a 'path=/data/test1 state=absent'
~]# ansible 172.16.0.41 -m hostname -a 'name=test-node1'
# 支持時間 minute, hour, day, month, weekday # 週六週日 每隔五分鐘 打印一條消息 ~]# ansible all -m cron -a 'minute=*/5 weekday=0,6 job="/usr/bin/wall cron job" name="test cronjob"' # 禁用 定時任務 ~]# ansible all -m cron -a 'disabled=true job="/usr/bin/wall cron job" name="test cronjob"' # 取消禁用 ~]# ansible all -m cron -a 'disabled=no job="/usr/bin/wall cron job" name="test cronjob"' # 刪除計劃任務 ~]# ansible all -m cron -a 'state=absent job="/usr/bin/wall cron job" name="test cronjob"'
# 安裝軟件包 ~]# ansible all -m yum -a 'name=httpd state=present' # 刪除軟件包 ~]# ansible all -m yum -a 'name=httpd state=absent' # 安裝最新版本 ~]# ansible all -m yum -a 'name=httpd state=latest' # 臨時禁用 gpg 檢查 ~]# ansible all -m yum -a 'name=httpd state=present disable_gpg_check=yes' # 更新緩存 ~]# ansible all -m yum -a 'name=httpd,vsftpd state=present disable_gpg_check=yes update_cache=yes'
~]# ansible all -m service -a 'name=httpd state=stopped enabled=yes' ~]# ansible all -m service -a 'name=httpd state=started' ~]# ansible all -m service -a 'name=httpd state=restarted'
# 添加用戶,指定: 備註,uid,家目錄,主組,附屬組 ~]# ansible all -m user -a 'name=test2 comment="test user2" uid=2000 home=/data/test2 group=test groups=root,bin' ~]# ansible all -a 'getent passwd test2' 172.16.0.40 | CHANGED | rc=0 >> test2:x:2000:1001:test user2:/data/test2:/bin/bash # 添加系統用戶 ,shell 類型 ~]# ansible all -m user -a 'name=sysuser system=yes shell=/sbin/nologin' # 刪除用戶 ~]# ansible all -m user -a 'name=sysuser state=absent' # 刪除用戶同時刪除家目錄 ~]# ansible all -m user -a 'name=test2 state=absent remove=yes'
# 添加組 ~]# ansible all -m group -a 'name=group1' # 查看組 ~]# ansible all -a 'getent group group1' # 刪除組 ~]# ansible all -m group -a 'name=group1 state=absent'
Playbook
ansible-galaxy
ansible-galaxy remove geerlingguy.nginx
ansible-pull
ansible-playbook
ansible-vault
]# ansible-vault encrypt hello.yml #加密
]# ansible-vault view hello.yml # 查看加密文件
]# ansible-vault edit hello.yml # 編輯加密文件
]# ansible-vault create hello.yml # 建立新文件