有关使用Ansible模块的参数的备忘录(随时更新)
环境
-
- ansible 2.0.2.0
- Amazon Linux AMI release 2016.03
Ansible命令
选择各环境(prd,stg,dev)中定义的主机组,并指定模块来执行。
$ ansible -i <inventoryfile> <hostpattern> -m <mojule>
模块
-
- 対象ホストで実行するライブラリ群
-
- ファイル操作・転送・PKG操作等
-
- モジュールの引数はシーケンスではなく、文字列として扱われる
-
- 論理値は使い分ける
モジュールの引数はyes,no
その他はTrue,False
行の折り返しは「>」
改行を空白に置換
美味
-
- state
present(default)
インストール状態
absent
非インストール状態
latest
最新版インストール状態
安装Nginx
---
#インデントはスペース2つ
#!/usr/bin/env ansible-playbook
- name: test-play
hosts: target-al
become: True
tasks:
- name: epel-install
yum: >
name=epel-release
state=present
- name: nginx-install
yum: >
name=nginx
state=present
服务
-
- state
started
起動状態
stopped
停止状態
restarted
再起動実施
reloaded
リロード処理実施
在系统启动时将其设为启动状态。
---
---
#インデントはスペース2つ
#!/usr/bin/env ansible-playbook
- name: test-play
hosts: target-al
become: True
tasks:
- name: epel-install
yum: >
name=epel-release
state=present
- name: nginx-install
yum: >
name=nginx
state=present
- name: nginx-status
service: >
name=nginx
state=started
enabled=yes
复制
-
- src
差し替え元
dest
差し替え先
backup
origin作成
替换索引文件(将原始文件作为备份保存)
---
#インデントはスペース2つ
#!/usr/bin/env ansible-playbook
- name: test-play
hosts: target-al
become: True
tasks:
- name: epel-install
yum: >
name=epel-release
state=present
- name: nginx-install
yum: >
name=nginx
state=present
- name: nginx-status
service: >
name=nginx
state=started
enabled=yes
- name: change index.html
copy: >
src=/etc/ansible/index.html
dest=/usr/share/nginx/html/index.html
backup=yes
备份会附带时间戳。
-rw-r--r-- 1 root root 3696 Feb 19 22:52 404.html
-rw-r--r-- 1 root root 3738 Feb 19 22:52 50x.html
-rw-r--r-- 1 root root 192 Jun 13 05:01 index.html
-rw-r--r-- 1 root root 3770 Feb 19 22:52 index.html.2016-06-13@05:01:48~
-rw-r--r-- 1 root root 368 Feb 19 22:52 nginx-logo.png
lrwxrwxrwx 1 root root 32 Jun 13 04:32 poweredby.png -> /usr/share/pixmaps/poweredby.png
设置
自动收集每个主机的信息并赋值给变量。这些变量被称为Facts,可以被明确调用。
可以看出,事实定义在名为 “ansible_facts” 的映射中。
$ ansible -i hosts <target> -m setup
(snip)
10.0.0.90 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"10.0.0.90"
],
"ansible_all_ipv6_addresses": [
"fe80::41e:73ff:fecb:a23f"
],
"ansible_architecture": "x86_64",
"ansible_bios_date": "05/12/2016",
"ansible_bios_version": "4.2.amazon",
"ansible_cmdline": {
"console": "ttyS0",
"root": "LABEL=/"
},
"ansible_date_time": {
"date": "2016-07-05",
"day": "05",
"epoch": "1467702189",
"hour": "16",
"iso8601": "2016-07-05T07:03:09Z",
"iso8601_basic": "20160705T160309733090",
"iso8601_basic_short": "20160705T160309",
"iso8601_micro": "2016-07-05T07:03:09.733143Z",
"minute": "03",
"month": "07",
"second": "09",
"time": "16:03:09",
"tz": "JST",
"tz_offset": "+0900",
"weekday": "Tuesday",
"weekday_number": "2",
"weeknumber": "27",
"year": "2016"
},
"ansible_default_ipv4": {
"address": "10.0.0.90",
"alias": "eth0",
"broadcast": "10.0.0.255",
"gateway": "10.0.0.1",
"interface": "eth0",
"macaddress": "06:1e:73:cb:a2:3f",
"mtu": 9001,
"netmask": "255.255.255.0",
"network": "10.0.0.0",
"type": "ether"
},
(snip)
提取ansible_default_ipv4
$ ansible -i hosts <target> -m setup -a 'filter=ansible_default_ipv4'
10.0.0.90 | SUCCESS => {
"ansible_facts": {
"ansible_default_ipv4": {
"address": "10.0.0.90",
"alias": "eth0",
"broadcast": "10.0.0.255",
"gateway": "10.0.0.1",
"interface": "eth0",
"macaddress": "06:1e:73:cb:a2:3f",
"mtu": 9001,
"netmask": "255.255.255.0",
"network": "10.0.0.0",
"type": "ether"
}
},
"changed": false
}