モジュールとは、ファイル作成やユーザ作成、パッケージのインストールなどが行える機能
モジュール:file
クライアントサーバのファイル、ディレクトリ、シンボリックリンクの所有者、所有グループ、パーミッションを管理するモジュール
実行するplaybook
[root@localhost ~]# #マネージャー側
[root@localhost ~]# cat /root/test_module
- hosts: client01
name: "file test"
become: yes
tasks:
- name: filemake
file:
path: /root/client_file
owner: testuser
group: testgroup
mode: 0600
state: touch
- name: directorymake
file:
path: /root/client_directory
owner: testuser
group: moduletest_group
mode: 0111
state: directory
パラメータ内容
path:操作対象のファイルパス
owner:所有者
group:所有グループ
mode:パーミッション
state:「touch」でファイル作成、「absent」で削除、「directory」でディレクトリ作成
実行前の状態
[root@localhost ~]# #クライアント側
[root@localhost ~]# ls -ld /root/client_directory/
ls: client_directory/ にアクセスできません: そのようなファイルやディレクトリはありません
[root@localhost ~]# ll client_file
ls: client_file にアクセスできません: そのようなファイルやディレクトリはありません
playookの実行
[root@localhost ~]# ansible-playbook /root/test_module -D -k
SSH password:
PLAY [file test] ****************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [192.168.0.21]
TASK [filemake] *****************************************************************************************************************************************************************************
--- before
+++ after
@@ -1,9 +1,9 @@
{
- "atime": 1704377939.6133192,
- "group": 0,
- "mode": "0644",
- "mtime": 1704377939.6133192,
- "owner": 0,
+ "atime": 1704377939.62447,
+ "group": 1001,
+ "mode": "0600",
+ "mtime": 1704377939.62447,
+ "owner": 1000,
"path": "/root/client_file",
- "state": "absent"
+ "state": "touch"
}
changed: [192.168.0.21]
TASK [directorymake] ************************************************************************************************************************************************************************
--- before
+++ after
@@ -1,7 +1,7 @@
{
- "group": 0,
- "mode": "0755",
- "owner": 0,
+ "group": 2999,
+ "mode": "0111",
+ "owner": 1000,
"path": "/root/client_directory",
- "state": "absent"
+ "state": "directory"
}
changed: [192.168.0.21]
PLAY RECAP **********************************************************************************************************************************************************************************
192.168.0.21 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
実行後の状態
[root@localhost ~]# #クライアント側
[root@localhost ~]# ls -ld /root/client_directory/
d--x--x--x 2 testuser moduletest_group 6 1月 4 23:18 client_directory/
[root@localhost ~]# ll client_file
-rw------- 1 testuser testgroup 0 1月 4 23:18 client_file
参考:[Ansible] file モジュールの基本的な使い方(ファイルやディレクトリの操作)
モジュール:shell
shell上でコマンドを実行する
実行するplaybook
[root@localhost ~]# #マネージャー側
[root@localhost ~]# cat /root/test_module
- hosts: client01
name: "shell test"
become: yes
tasks:
- shell: useradd user20240104
パラメータ内容
shell: クライアント側で実行するコマンド
変更する前の/etc/passwd
[root@localhost ~]# #クライアント側
[root@localhost ~]# tail -3 /etc/passwd
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
testuser:x:1000:1000::/home/testuser:/bin/bash
aaa:x:1001:1002::/home/aaa:/bin/bash
playookの実行
[root@localhost ~]# #マネージャー側
[root@localhost ~]# ansible-playbook /root/test_module -k
SSH password:
PLAY [shell test] ***************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [192.168.0.21]
TASK [shell] ********************************************************************************************************************************************************************************
changed: [192.168.0.21]
PLAY RECAP **********************************************************************************************************************************************************************************
192.168.0.21 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
変更後の/etc/passwd
[root@localhost ~]# #クライアント側
[root@localhost ~]# tail -3 /etc/passwd
testuser:x:1000:1000::/home/testuser:/bin/bash
aaa:x:1001:1002::/home/aaa:/bin/bash
user20240104:x:1002:1003::/home/user20240104:/bin/bash
モジュール:lineinfile
指定したファイルを行単位で書き換える
今回は、削除と挿入をしてみる
実行するplaybook
[root@localhost ~]# #マネージャー側
[root@localhost ~]# cat /root/test_module
- hosts: client01
name: "shell test"
become: yes
tasks:
- lineinfile:
path: /root/20240104.txt
state: absent
regexp: '^aaa'
- lineinfile:
path: /root/20240104.txt
line: '12345'
insertafter: '^ccc'
パラメータ内容
1つ目のlineinfileモジュール
path:削除対象のファイル
state:「absent」を指定した場合、行を削除
regexp:削除したい行の正規表現パターン
2つ目のlineinfileモジュール
line:挿入する行の内容
insertafter:ここで指定した行の後に追加
変更する前のファイル
[root@localhost ~]# #クライアント側
[root@localhost ~]# cat /root/20240104.txt
aaa
bbb
ccc
ddd
eee
playookの実行
[root@localhost ~]# ansible-playbook /root/test_module -k
SSH password:
PLAY [shell test] ***************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [192.168.0.21]
TASK [lineinfile] ***************************************************************************************************************************************************************************
changed: [192.168.0.21]
TASK [lineinfile] ***************************************************************************************************************************************************************************
changed: [192.168.0.21]
PLAY RECAP **********************************************************************************************************************************************************************************
192.168.0.21 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
変更後のファイル
[root@localhost ~]# #クライアント側
[root@localhost ~]# cat /root/20240104.txt
bbb
ccc
12345
ddd
eee
モジュール:user
ユーザーを管理するモジュール
実行するplaybook
[root@localhost ~]# #マネージャ側
[root@localhost ~]# cat /root/test_module
- hosts: client01
name: "user_module_test"
become: yes
tasks:
- user:
name: moduletest_user
comment: "test_comment"
uid: 1999
group: testuser
groups: testgroup
shell: /bin/bash
home: /tmp
createhome: yes
パラメータ内容
name:作成するユーザ名
comment:ユーザーに関するコメント(第5フィールド)
uid:ユーザーID
group:プライマリグループ
groups:セカンダリグループ
shell:ログインシェル
home:ホームディレクトリ
createhome:ホームディレクトリを作成する場合はyesとする
変更する前の/etc/passwd
[root@localhost ~]# #クライアント側
[root@localhost ~]# tail -3 /etc/passwd
aaa:x:1001:1002::/home/aaa:/bin/bash
user20240104:x:1002:1003::/home/user20240104:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologi
playbookの実行
[root@localhost ~]# ansible-playbook /root/test_module -D -k
SSH password:
PLAY [user_module_test] *********************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [192.168.0.21]
TASK [user] *********************************************************************************************************************************************************************************
changed: [192.168.0.21]
PLAY RECAP **********************************************************************************************************************************************************************************
192.168.0.21 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
変更後の/etc/passwd
[root@localhost ~]# #クライアント側
[root@localhost ~]# tail -3 /etc/passwd
user20240104:x:1002:1003::/home/user20240104:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
moduletest_user:x:1999:1000:test_comment:/tmp:/bin/bash
モジュール:group
グループを管理するモジュール
実行するplaybook
[root@localhost ~]# #マネージャ側
[root@localhost ~]# cat /root/test_module
- hosts: client01
name: "user_module_test"
become: yes
tasks:
- group:
name: moduletest_group
gid: 2999
state: present
パラメータ内容
name: グループ名
gid: グループID
state: 「present」なら作成「absent」なら削除
変更する前の/etc/group
[root@localhost ~]# #クライアント側
[root@localhost ~]# tail -3 /etc/group
aaa:x:1002:
user20240104:x:1003:
apache:x:48:
playbookの実行
[root@localhost ~]# #マネージャ側
[root@localhost ~]# ansible-playbook /root/test_module -D -k
SSH password:
PLAY [user_module_test] *********************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [192.168.0.21]
TASK [group] ********************************************************************************************************************************************************************************
changed: [192.168.0.21]
PLAY RECAP **********************************************************************************************************************************************************************************
192.168.0.21 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
変更後の/etc/group
[root@localhost ~]# #クライアント側
[root@localhost ~]# tail -3 /etc/group
user20240104:x:1003:
apache:x:48:
moduletest_group:x:2999:
モジュール:yum
パッケージを管理するモジュール
実行するplaybook
[root@localhost ~]# #クライアント側
[root@localhost ~]# cat /root/test_module
- hosts: client01
name: "yum test"
become: yes
tasks:
- yum:
name: httpd
state: present
パラメータ内容
name: インストールするパッケージ名
state: 「present」でインストール、「absent」でアンインストール
実行前の状態
[root@localhost ~]# #クライアント側
[root@localhost ~]# rpm -qa | grep httpd
httpd-tools-2.4.6-99.el7.centos.1.x86_64
playbookの実行
[root@localhost ~]# ansible-playbook /root/test_module -D -k
SSH password:
PLAY [yum test] *****************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [192.168.0.21]
TASK [yum] **********************************************************************************************************************************************************************************
changed: [192.168.0.21]
PLAY RECAP **********************************************************************************************************************************************************************************
192.168.0.21 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
実行後の状態
[root@localhost ~]# #クライアント側
[root@localhost ~]# rpm -qa | grep httpd
httpd-tools-2.4.6-99.el7.centos.1.x86_64
httpd-2.4.6-99.el7.centos.1.x86_64
モジュール:service
サービスを管理するモジュール
実行するplaybook
[root@localhost ~]# #マネージャ側
[root@localhost ~]# cat /root/test_module
- hosts: client01
name: "service test"
become: yes
tasks:
- service:
name: httpd
enabled: yes
パラメータ内容
name: 管理するサービス名
enabled: 「yes」なら有効化、「no」なら無効化にする
実行前の状態
[root@localhost ~]# #クライアント側
[root@localhost ~]# systemctl status http
Unit http.service could not be found.
[root@localhost ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd(8)
man:apachectl(8)
playbookの実行
[root@localhost ~]# ansible-playbook /root/test_module -D -k
SSH password:
PLAY [service test] *************************************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************************************
ok: [192.168.0.21]
TASK [service] ******************************************************************************************************************************************************************************
changed: [192.168.0.21]
PLAY RECAP **********************************************************************************************************************************************************************************
192.168.0.21 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
実行後の状態
[root@localhost ~]# #クライアント側
[root@localhost ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd(8)
man:apachectl(8)
コメント