ansible入門
Ansible是一個配置管理和配置工具,使用SSH連接到服務器並運行配置好的任務,服務器上不需要安裝任何其他軟件,只需要開啓SSH,客戶端的ansible會完成所有其他的工作。
首先安裝Ansible:
apt-get安裝的版本很低,建議使用pip安裝:
sudo pip install ansible
可能會提示什麼:from pip import main ImportError: cannot import name main
那就使用--user
:pip install --user ansible
在虛擬環境virtualenv中安裝:
sudo pip install -U virtualenv # 首先安裝virtualenv
# 然後進入一個ansible專門的文件夾,創建虛擬環境:
virtualenv .venv
source .venv/bin/activate
# 進入了虛擬環境
pip install ansible
pip install -U ansible # 任何時候都可以這樣更新ansible
配置ansible:
在ansible1裏面,我們需要用到/etc/ansible裏面的默認配置,但是像上面那樣在虛擬環境裏面安裝ansible,那麼我們完全不需要這些默認文件,我們可以在非本地路徑下創建這些配置文件。
管理服務器:Inventory
我們需要創建一個inventory file用來定義管理哪個server,命名隨意,一般爲hosts:
[web]
192.168.22.10
192.168.22.11
在web標籤下我們需要管理兩個server
當我們在server上運行ansible against本地機,我們不需要關心Inventory file裏面的內容
當我們本地運行ansible against遠程服務器時,Inventory file需要這樣編寫:
[local]
127.0.0.1
[remote]
192.168.1.2
接下來是建立本地與遠程服務器之間連接的命令。
Running Commands
接下來我們針對服務器運行任務tasks,ansible是通過SSH去管理服務器的:
# Run against localhost
ansible -i ./hosts --connection=local local -m ping
# Run against remote server針對遠程服務器運行ansible
ansible -i ./hosts remote -m ping
參數說明
--connection=local
的意思是不需要通過ssh連接,因爲我們針對的就是本地,還可以是--connection=ssh
意思是需要ssh連接。-i ./hosts
指定Inventory file,告訴我們連接到哪裏remote,local,all
指明使用哪個標籤下面的服務器清單,all表示針對每個服務器運行-m ping
表示使用ping模塊,會返回ping結果
然後針對阿里雲服務器進行了嘗試,運行ansible -i ./hosts remote -m ping
發現報錯:
39.107.74.200 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).",
"unreachable": true
}
感覺問題應該是這樣的命令沒有輸入用戶名和密碼,嘗試
ansible -i ./hosts remote -m ping -u root --ask-pass
前提是使用sudo apt-get
安裝好了sshpass,然後返回結果:
39.107.74.200 | SUCCESS => {
"changed": false,
"ping": "pong"
}
但是每次敲這麼多命令就很複雜,更簡單的方法是將用戶名密碼寫入hosts文件裏面:
[remotr]
29.107.74.200 ansible_ssh_user=root ansible_ssh_pass=123456
然後就不需要額外的命令了
Modules
ansible使用模塊來完成任務,比如安裝軟件,複製文件以及使用模板。
模塊aret the way to use absible, 因爲模塊可以使用上下文來確定完成任務需要做些什麼。
如果沒有模塊,我們只能運行shell命令,其實是使用的shell模塊:
ansible -i ./hosts remote -b --become-user=root all
-m shell -a 'apt-get install nginx’
-b
成爲另一個用戶--become-user=root
以用戶root運行命令-m
定義模塊,這裏使用的是shell模塊-a
給模塊傳遞參數
這樣做並不好,因爲這樣只能完成bash腳本能夠完成的任務
我們還可以使用apt模塊,這樣能確保冪等(一個冪等操作的特點是其任意多次執行所產生的影響均與一次執行的影響相同)
ansible -i ./hosts remote -b --become-user=root
-m apt -a 'name=nginx state=installed update_cache=true'
如果執行後顯示的是change:False
,就表明已經安裝,沒有對環境造成任何影響
name=nginx
表示需要安裝的軟件包名state=installed
表示需要的結束狀態update_cache=true
表示是否更新軟件包儲存庫緩存
通過模塊我們可以做我們想做的事,但是爲了更好管理,我們把任務都放到playbook裏面,就可以運行多個tasks
Playbooks
playbook可以運行多任務還有一些高級功能,playbook和roles(劇本和角色)都使用YAML文件定義:
nginx.yml
- hosts: remote
become: yes
become_user: root
tasks:
- name: install nginx
apt:
name: nginx
state: present
update_cache: true
YAML文件格式:用縮排方式呈現,結構通過縮進來表示,連續的項目使用-
表示,鍵值對用:
表示,不可以使用TAB.
如何使用playbook呢?直接運行:
ansible-playbook -i ./hosts nginx.yml
Handlers
handlers=tasks,處理程序可以做任務可以完成的任何事,但是隻有當另一個task調用它的時候纔會執行。
我們添加notify指令到上面的playbook中:
- hosts: remote
become: yes
become_user: root
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
update_cache: true
notify:
- Start Nginx
handlers:
- name: Start Nginx
service:
name: nginx
state: started
任務task運行後會通知名爲start Nginx的處理程序:該handler處理使用服務service模塊,可以啓動停止重啓等操作。
所以handler和task的唯一區別就是task會自動調用,handler得等着task來調用它
這裏還有一個地方需要注意:如果這裏nginx已經安裝,那麼nginx安裝任務不會運行,handler也不會被調用。
使用變量:
首先定義變量:
- hosts: local
connection: local
become: yes
become_user: root
vars:
- docroot: /var/www/serversforhackers.com/public
使用變量方法:
file:
path: '{{ docroot }}'
使用Jinja模板,必須是單引號或者雙引號
roles(最終使用方法,比playbook更好配置)
roles角色用於組織多個task並封裝成完成這些任務需要的數據,但是真實的配置場景裏面,我們需要很多變量,文件,模板之類的東西,雖然可以配合playbook使用,但是roles更好,因爲它有一個目錄結構來規範
roles
rolename
- files
- handlers
- meta
- templates
- tasks
- vars
我們下面創建一個新的role例子,role的任務有:
1. Add Nginx Repository- 使用apt_repository模塊添加Nginx穩定PPA以獲取最新的穩定版本的Nginx 。
2. Install Nginx - 使用Apt模塊安裝Nginx。
3. Create Web Root - 最後創建一個Web根目錄。
在每一個子目錄中,ansible都會自動找到並讀取那個叫main.yml的文件。
創建角色
進入本地ansible-test文件夾,激活虛擬環境:
mkdir nginx # 這裏最好就使用roles這個名字
cd nginx
ansible-galaxy init nginx
然後進入nginx,會發現整個role目錄已經建立好了。
1. files
裏面放置我們需要複製到服務器中的文件
2. handlers
記得放在main.yml文件中:
---
- name: Start Nginx
service:
name: nginx
state: started
- name: Reload Nginx
service:
name: nginx
state: reloaded
前面提到了handler需要被task調用才起作用,我們可以在其他的YAML文件中調用它們
3. meta
這個目錄裏面的main.yml用於描述角色之間的依賴關係,比如nginx role依賴於ssl role,那麼:
---
dependencies:
- {role: ssl}
這樣調用nginx角色時,自動會先調用ssl角色
如果無任何依賴:
dependencies: []
4. Template
這裏面不需要有main.yml文件,這裏的文件使用的是Jinja2模板引擎的.j2
文件
比如我們新建一個文件爲serversforhackers.com.conf.j2:
server {
# Enforce the use of HTTPS
listen 80 default_server;
server_name {{ domain }};
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl default_server;
root /var/www/{{ domain }}/public;
index index.html index.htm index.php;
access_log /var/log/nginx/{{ domain }}.log;
error_log /var/log/nginx/{{ domain }}-error.log error;
server_name {{ domain }};
charset utf-8;
include h5bp/basic.conf;
ssl_certificate {{ ssl_crt }};
ssl_certificate_key {{ ssl_key }};
include h5bp/directive-only/ssl.conf;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; }
location ~ \.php$ {
include snippets/fastcgi.conf;
fastcgi_pass unix:/var/run/php7.1-fpm.sock;
}
}
這是一個標準的php應用的Nginx配置文件,裏面有一些變量
5. vars
該目錄需要有main.yml文件:
---
domain: serversforhackers.com
ssl_key: /etc/ssl/sfh/sfh.key
ssl_crt: /etc/ssl/sfh/sfh.crt
6. tasks
這個是最重要的文件夾,使用role的時候,運行的文件就是該文件夾下面的main.yml文件:
---
- name: Add Nginx Repository
apt_repository:
repo: ppa:nginx/stable
state: present
- name: Install Nginx
apt:
pkg: nginx
state: installed
update_cache: true
notify:
- Start Nginx
- name: Add H5BP Config
copy:
src: h5bp
dest: /etc/nginx
owner: root
group: root
運行角色 Run Role
我們要對服務器運行一個或者多個role的時候,我們還是需要用到playbook,playbook需要和roles在同一個目錄下面,然後創建一個主yml文件,在這個文件裏面定義需要使用的角色以及是對哪個服務器執行的操作:
新建 server.yml(這就是那個playbook):
- hosts: local
connection: local
roles:
- nginx
然後老樣子運行playbook:
ansible-playbook -i ./hosts server.yml
前面幾大板塊的關係:
playbook >> roles >> tasks+handler+… >>modules
總的playbook裏面可能就幾行,所有各部分的功能交給不同的role來完成。
每個role裏面是各種task並且調用handler支撐功能的。
每個task和handler裏面的功能是由各種模塊modules實現的。
我們使用ssh與託管節點(服務器)通信,默認使用sftp(使用SSH協議進行FTP傳輸的協議叫做SFTP,),如果不可用,需要在ansible.cfg
文件中配置成scp方式
這裏不建議使用virtualenv虛擬環境來安裝ansible
Inventory文件
對於每一個host可以選擇連接類型和連接用戶名:
[targets]
localhost ansible_connection=local
39.107.74.200 ansible_connection=ssh ansible_ssh_user=root
給整個組添加變量
上面是給服務器單獨配置變量,但是也可以給整個組配置變量:
[remote:vars]
ansible_connection=ssh
[remote]
host1
host2
只要需要配置的組的名字對上了,就可以獲取配置
將一個組變成另一個組的成員:
[groupA]
host1
[groupB:children]
groupA
各種參數說明:
參數 | 說明 |
---|---|
ansible_ssh_host | 遠程主機名 |
ansible_ssh_port | ssh端口 |
ansible_ssh_user | ssh用戶名 |
ansible_ssh_pass | ssh密碼,建議使用–ask-pass |
ansible_sudo_pass | sudo 密碼,建議使用–ask-sudo-pass |
ansible_connection | 與遠程主機連接類型local ssh |
ansible_python_interpreter | 目標主機python路徑,適用於主機有多個python |
Ad-Hoc
ansible 有兩種方法完成任務,一個是as-hoc,一個是playbook,前者解決簡單任務,後者複雜任務
Ansible配置文件
ansible的一些設置可以通過配置文件完成,大多數情況下,默認配置就足夠了,配置被讀取的順序:
ANSIBLE_CONFIG(環境變量)
ansible.cfg(當前目錄)
.ansible.cfg(當前家目錄)
/etc/ansible/ansible.cfg
下面是配置文件不同段的詳解:
多數爲默認段,[defaults]中
代碼 | 說明 |
---|---|
ask_pass | 控制playbook是否彈出詢問密碼,默認值爲No |
ask_sudo_pass | 是否彈出詢問sudo密碼 |
command_warnings | 當shell和命令行模塊被默認模塊簡化的時,是否發出警告 |
deprecation_warnings | 設置爲True,允許在輸出結果中禁用‘不建議使用’警告 |
forks | 設置與主機通信時默認並行進程數,如果有很多主機,高數值會讓跨主機行爲更快,設置爲100 |
gathering | 控制默認facts收集(遠程系統變量),smart值代表沒有facts的新host不會被掃描,在節省facts收集時比較有用 |
inventory | 默認庫文件(hosts文件)位置 |
library | 默認模塊的位置,library = ./library/,但是ansible知道如何搜尋多個用冒號隔開的路徑,也會搜索playbook中的./library |
log_path | ansible將會在選定位置記錄執行信息 |
module_lang | 默認模塊和計算機通信語言,默認爲C語言 |
module_name | 是默認模塊名,默認爲command,但是不支持shell變量等,所以可能希望改爲shell |
nocolor=0 | 爲輸出結果加上顏色,便於區分,如果想關閉設置爲1 |
remote_port | 默認的遠程端口,默認爲22 |
remote_tmp | 使用默認路徑希望像更換補丁一樣使用模塊 |
remote_user | 默認遠程主機用戶名 |
roles_path | 指的是’roles/’下的額外目錄,用於playbook搜索Ansible roles. |
timeout | ssh嘗試超時時間 |
host_key_checking | 檢測主機祕鑰,禁用設爲False |
[ssh_connection] 調整ssh通信連接
代碼 | 說明 |
---|---|
scp_if_ssh | 設置爲True,scp將代替用來爲遠程主機傳輸文件,沒必要修改 |
pipelining | 開啓將顯著提高性能,在不通過實際文件傳輸的情況下執行ansible模塊來使用管道特性,從而減少執行遠程模塊SSH操作次數 |
Ansible Playbook
1. 主機和用戶:
---
- hosts: webservers
remote_user: yourname
sudo: yes
還可以sudo到不同的用戶身份:
sudo_user: postgres
2. Tasks列表:
modules具有冪等性,如果再一次執行module,module只會執行必ansible_ssh_connection=ssh要的改動,所以重複多次執行playbook也沒有問題。
基本task定義,使用key=value格式,大多數都是這樣的:
tasks:
- name: make sure apache is running
service: name=httpd state=running
特別的模塊是command和shell,不使用key=value:
tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand
ignore_errors: True
action中可以使用變量,假設前面vars裏面設置了變量’vhost‘
tasks:
- name: create a virtual host file for {{ vhost }}
template: src=somefile.j2 dest=/etc/httpd/conf.d/{{ vhost }}
3. Handlers 在發生改變的時候執行操作
因爲module具有冪等性,如果遠端系統被人改動,可以重放playbook達到恢復的目的,playbook本身有識別這種改動的功能,有一個基本的event system時間系統可以響應改動
- name: template configuration file
template: src=template.j2 dest=/etc/foo.conf
notify:
- restart memcached
- restart apache
handlers:
- name: restart memcached
service: name=memcached state=restarted
- name: restart apache
service: name=apache state=restarted
notify會在playbook每一個task結束的時候觸發,即使有多個不同的task通知改動發生,notify也只會觸發一次
handlers一般用於重啓服務,此外很少用到
4. 執行一個playbook:
ansible-playbook playbook.yml -f 10
並行的級別爲10
5. include file
在一個playbook中,include指令可以跟普通的task混合在一起使用:
tasks:
- include: tasks/a.yml
- include: tasks/b.yml
如果希望定義一個重啓服務的handler,只需要定義一次,可以創建一個handlers.yml文件
---
# this might be in a file like handlers/handlers.yml
- name: restart apache
service: name=apache state=restarted
然後在主playbook中:
handlers:
- include: handlers/handlers.yml
我們可以定義一個頂層playbook:
- name: this is a play at the top level of a file
hosts: all
remote_user: root
tasks:
- name: say hi
tags: foo
shell: echo "hi..."
- include: load_balancers.yml
- include: webservers.yml
- include: dbservers.yml
Variables:
1. 在playbook中直接定義變量:
- hosts: webservers
vars:
http_port: 80
2. YAML陷阱,開頭使用變量
- hosts: app_servers
vars:
app_path: {{ base_path }}/22
這樣是不行的,以變量開頭需要將整行用雙引號包起來。
app_path: "{{ base_path }}/22"
3. 變量單獨放到一個文件:
---
- hosts: all
remote_user: root
vars:
favcolor: blue
vars_files:
- /vars/external_vars.yml
變量文件:
---
somevar: somevalue
password: magic
條件選擇:
when語句:
tasks:
- shell: echo "only on Red Hat 6, derivatives, and later"
when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int >= 6
在特定情況下運行task
標準循環
- name: add several users
user: name={{ item }} state=present groups=wheel
with_items:
- testuser1
- testuser2
Roles
roles會自動加載tasks,handler
創建roles:
mkdir nginx # 這裏最好就使用roles這個名字
cd nginx
ansible-galaxy init nginx
調用roles的方式:
---
- hosts: webservers
roles:
- common
- webservers
如果 roles/x/tasks/main.yml 存在, 其中列出的 tasks 將被添加到 play 中
如果 roles/x/handlers/main.yml 存在, 其中列出的 handlers 將被添加到 play 中
如果 roles/x/vars/main.yml 存在, 其中列出的 variables 將被添加到 play 中
如果 roles/x/meta/main.yml 存在, 其中列出的 “角色依賴” 將被添加到 roles 列表中 (1.3 and later)
所有 copy tasks 可以引用 roles/x/files/ 中的文件,不需要指明文件的路徑。
所有 script tasks 可以引用 roles/x/files/ 中的腳本,不需要指明文件的路徑。
所有 template tasks 可以引用 roles/x/templates/ 中的文件,不需要指明文件的路徑。
所有 include tasks 可以引用 roles/x/tasks/ 中的文件,不需要指明文件的路徑。
最後的項目結構應該是:
production # inventory file for production servers 關於生產環境服務器的清單文件
stage # inventory file for stage environment 關於 stage 環境的清單文件
group_vars/
group1 # here we assign variables to particular groups 這裏我們給特定的組賦值
group2 # ""
host_vars/
hostname1 # if systems need specific variables, put them here 如果系統需要特定的變量,把它們放置在這裏.
hostname2 # ""
library/ # if any custom modules, put them here (optional) 如果有自定義的模塊,放在這裏(可選)
filter_plugins/ # if any custom filter plugins, put them here (optional) 如果有自定義的過濾插件,放在這裏(可選)
site.yml # master playbook 主 playbook
webservers.yml # playbook for webserver tier Web 服務器的 playbook
dbservers.yml # playbook for dbserver tier 數據庫服務器的 playbook
roles/
common/ # this hierarchy represents a "role" 這裏的結構代表了一個 "role"
tasks/ #
main.yml # <-- tasks file can include smaller files if warranted
handlers/ #
main.yml # <-- handlers file
templates/ # <-- files for use with the template resource
ntp.conf.j2 # <------- templates end in .j2
files/ #
bar.txt # <-- files for use with the copy resource
foo.sh # <-- script files for use with the script resource
vars/ #
main.yml # <-- variables associated with this role
defaults/ #
main.yml # <-- default lower priority variables for this role
meta/ #
main.yml # <-- role dependencies
webtier/ # same kind of structure as "common" was above, done for the webtier role
monitoring/ # ""
fooapp/ # ""
Ansible常用模塊
1. ping
檢查指定節點機器是否能聯通
2. yum
這個模塊是RedHat和CentOS作爲遠端節點的OS的時候,用得最多的包管理工具
3. apt
這個模塊是ubuntu和Debian作爲遠端節點的時候用的最多的包管理工具
- deb: 軟件包名字,可選
- install_recommends: 默認爲true,設置爲False代表只下載不安裝
- update_cache: yes相當於apt-get update
- name: apt要下載的軟件名,指定版本可用name=git=1.6
- state: (present, adsent, latest) present表示爲安裝,然後是刪除,在是安裝位最新版本
4. pip
安裝python庫依賴項,必須參數爲name或者requirements
- chdir: 執行pip前需要進入的目錄
- name: 庫名字
- requirements: requirements.txt文件路徑,應該是遠程系統的本地文件,就可以使用chdir將文件指定爲相對定位
- version: 庫版本
- extra_args: pip額外參數
- executable: 顯式可執行文件或可執行文件的路徑名
- virtualenv: 要安裝到的virtualenv路徑名
- virtualenv_command: 創建虛擬環境
- virtualenv_python: 用於創建虛擬環境的命令或路徑名
- state:(present, lastest, absent)
5. synchronize
使用rsync同步文件,將主控方目錄推送到指定節點的目錄下
- delete: 刪除不存在的文件
- src: 要同步到目的地的源主機的路徑
- dest: 目的地上同步地址
- dest_port: 目的地機上的端口
- mode: push/pull,默認push,本機向遠程傳文件
- rsync_opts:
6. copy
在遠程主機上面複製文件
- src: 複製到遠程的文件在本地的地址,如果路徑以/結尾,只複製目錄裏面的內容,如果沒有,則包含目錄在內的整個內容全部複製
- content: 代替src,可以直接設定指定文件的值
- dest: 複製到遠程文件的路徑
- directory_mode: 目錄權限
- force:默認爲yes,強制覆蓋
- others: 所有file模塊裏面的選項
- mode: 0644
7. user
- home:指定用戶家目錄,需要配合createhome使用
- groups: 指定用戶組
- uid:
- password: 用戶密碼,不能使用明文密碼
- name: 指定用戶名
- createhome: 是否創建家目錄,yes、no
- system: 是否爲系統用戶
- remove: 當state爲absent的時候,remove=yes表示聯同家目錄一起刪除
- state: present, absent
- shell: 指定用戶的shell環境
- generate_ssh_key: 生成ssh祕鑰
- ssh_key_bits: 指定創建ssh祕鑰中的位數
- ssh_key_passphrase: ssh祕鑰密碼
- ssh_key_file: ssh祕鑰文件
- ssh_key_type: ssh祕鑰類型
8. group
- gid: 指定的gid
- name: 指定用戶名
- state: present, absent
- system: true,表示創建的是系統組
9. service
- arguments: 命令選項
- enabled: 是否開機啓動 yes
- name: 必選,服務名稱
- runlevel: 運行級別
- sleep: restarted之間的間隔
- state: started/stopped/restarted/reloaded
10. get_url
用於從http,ftp,https服務器上面上下載文件,類似wget
- sha256sum: 下載後sha256驗證
- timeout: 下載超時時間,默認爲10s
- url: 下載的url
- url_password, url_username: 如果下載需要提供驗證
- dest: 下載到哪裏
- headers: 以key:value的格式自定義http標頭
11. file
用於遠程主機上面的文件操作
- force: 強制創建軟連接
- group: 文件目錄的屬組
- mode: 文件目錄的權限
- owner: 文件目錄的屬主
- path: 必選,文件目錄的路徑
- recurse: 遞歸的設置文件的屬性
- src: 被連接的源文件路徑,當state=link的時候
- dest: 被連接到的路徑,當state=link的時候
- state: directory 如果目錄不存在則創建目錄
file 如果文件不存在則創建
link 創建軟連接
hard 創建硬鏈接
touch 如果文件不存在則創建,如果存在則修改最後修改時間屬性
absent 刪除文件目錄或者取消連接文件
# 創建一個目錄,如果它不存在
- file:
path: /etc/some_directory
state: directory
mode: 0755
12. unarchive
解壓文件
- copy: 解壓文件之前,是否現將文件複製到遠程主機
- creates: 指定一個文件名,當該文件存在,解壓不執行
- dest: 遠程主機上解壓文件的絕對路徑
- list_files: yes會列出壓縮包裏面的文件
- mode: 解壓後文件的權限
- src: 如果copy爲yes,需要指定壓縮文件的原路徑
- group: 解壓後文件屬組
- owner: 解壓後文件的屬主
13. shell
- chdir: 運行命令前切換到此目錄
將阿里雲API封裝成ansible module例子:
#!/usr/bin/env python
#coding=utf-8
from ansible.module_utils.basic import AnsibleModule
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
def run_ecs_instance(accessKey, accessSecret, region, ImageId, InstanceType, SecurityGroupId, VSwitchId,
InternetMaxBandwidthOut, HostName, Password, InternetChargeType, SystemDisk_Size, SystemDisk_Category, Amount, InstanceChargeType, ZoneId):
client = AcsClient(accessKey, accessSecret, region)
request = CommonRequest()
request.set_accept_format('json')
request.set_domain('ecs.aliyuncs.com')
request.set_method('POST')
request.set_protocol_type('https') # https | http
request.set_version('2014-05-26')
request.set_action_name('RunInstances')
request.add_query_param('RegionId', region)
request.add_query_param('ImageId', ImageId)
request.add_query_param('InstanceType', InstanceType)
request.add_query_param('SecurityGroupId', SecurityGroupId)
request.add_query_param('VSwitchId', VSwitchId)
request.add_query_param('InternetMaxBandwidthOut', InternetMaxBandwidthOut)
request.add_query_param('HostName', HostName)
request.add_query_param('Password', Password)
request.add_query_param('InternetChargeType', InternetChargeType)
request.add_query_param('SystemDisk.Size', SystemDisk_Size)
request.add_query_param('SystemDisk.Category', SystemDisk_Category)
request.add_query_param('Amount', Amount)
request.add_query_param('InstanceChargeType', InstanceChargeType)
request.add_query_param('ZoneId', ZoneId)
response = client.do_action(request)
# python2: print(response)
print(str(response, encoding = 'utf-8'))
return response
def ansible_module_builder():
#
module_args = dict(
action = dict(type='str', required=True),
accessKey = dict(type='str', required=True),
accessSecret = dict(type='str', required=True),
region = dict(type='str', required=True),
ImageId = dict(type='str', required=True),
InstanceType = dict(type='str', required=True),
SecurityGroupId = dict(type='str', required=True),
VSwitchId = dict(type='str', required=True),
InternetMaxBandwidthOut = dict(type='int', required=True),
HostName = dict(type='str', required=True),
Password = dict(type='str', required=True),
InternetChargeType = dict(type='str', required=True),
SystemDisk_Size = dict(type='int', required=True),
SystemDisk_Category = dict(type='str', required=True),
Amount = dict(type='int', required=True),
InstanceChargeType = dict(type='str', required=True),
ZoneId = dict(type='str', required=True)
)
module = AnsibleModule(
argument_spec = module_args,
supports_check_mode = True,
)
if module.params['action'] == 'createEcsInstance':
result = run_ecs_instance(
module.params['accessKey'],
module.params['accessSecret'],
module.params['region'],
module.params['ImageId'],
module.params['InstanceType'],
module.params['SecurityGroupId'],
module.params['VSwitchId'],
module.params['InternetMaxBandwidthOut'],
module.params['HostName'],
module.params['Password'],
module.params['InternetChargeType'],
module.params['SystemDisk_Size'],
module.params['SystemDisk_Category'],
module.params['Amount'],
module.params['InstanceChargeType'],
module.params['ZoneId']
)
module.exit_json(**result)
def main():
ansible_module_builder()
if __name__ == '__main__':
main()