--check
- name: create group
group:
name: hoge
gid: 999
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/group_module.html
- name: create user
user:
name: hoge
uid: 999
groups: hoge
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/user_module.htmle
- name: disable selinux
selinux:
state: disabled
policy: targeted
https://docs.ansible.com/ansible/latest/collections/ansible/posix/selinux_module.html
- name: set timezone
timezone:
name: Asia/Tokyo
- name: set locale to Japanese
shell: localectl set-locale LANG=ja_JP.utf8
- name: is exists tar command
stat:
path: /usr/bin/tar
register: check_tar
- name: install tar
dnf:
name: tar
state: installed
when: not check_tar.stat.exists
- name: copy hoge.txt
copy:
src: /home/hoge/hoge.txt
dest: /tmp/
(参考) https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html
- name: copy *.txt
copy:
src: "{{ item }}"
dest: /tmp/
owner: hoge
group: hoge
mode: 0644
with_fileglob:
- "/home/hoge/*.txt"
- name: copy hoge.txt
copy:
src: /home/hoge/hoge.txt
dest: /tmp/
remote_src: true
- name: create symbolic link
file:
src: /hoge/hoge/hoge.txt
dest: /hoge/hoge/fuga.txt
state: link
- name: hoge template
template:
src: hoge.conf.j2
dest: /etc/hoge.conf
mode: 0644
vars:
hoge: fuga
- name: read file(Base64 encoded).
slurp:
src: /hoge/hoge/hoge.txt
register: hoge
- name: show hoge.txt
debug:
msg: "{{ hoge.content | b64decode }}"
※読み込んだ内容は Base64 化される。
- name: unarchive xxx.tar.gz
unarchive:
src: /home/foo/hoge.tar.gz
dest: /usr/local
- name: create directory
file:
path: /tmp/hoge
state: directory
owner: hoge
group: hoge
mode: 0755
- name: delete tar.gz + extracted directory
file:
path: /tmp/{{ item }}
state: absent
with_items:
- hoge.tar.gz
- hoge
- name: find rpm files
find:
paths: /tmp/rpms
patterns: "*.rpm"
register: rpm_files
- name: create rpm file list
set_fact:
rpm_file_list: "{{ rpm_files.files | map(attribute='path') | list }}"
- name: install rpm files
dnf:
disablerepo: "\\*"
disable_gpg_check: true
name: "{{ rpm_file_list }}"
state: present
- name: check hoge is installed?
dnf:
name: hoge
state: installed
check_mode: true
ignore_errors: true
register: check_hoge_installed
- name: install rpm files
dnf:
disablerepo: "\\*"
disable_gpg_check: true
name: hoge.rpm
state: present
when: check_hoge_installed.failed
対象の RPM がインストールされていない場合 dnf が 1 を返し、そのままではエラーで ansible が中断してしまうので、ignore_errors: true を設定。
ansible 2.9 で、上の方法では RPM インストールチェックがうまくいかないので、ファイルの存在チェックで判断した方が良さそう
- name: check httpd is installed?
stat:
path: /usr/sbin/httpd
register: check_httpd_command
- name: install rpm files
dnf:
name: httpd
state: present
when: not check_httpd_command.stat.exists
- name: enable service hoge
systemd:
name: hoge.service
daemon_reload: yes
enabled: yes
state: started
(参考)
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/systemd_module.html
[Ansible] service モジュールの基本的な使い方(サービスの起動・停止・自動起動の有効化など)
- name: wait port 80 is listening
wait_for:
port: 80
host: 127.0.0.1
※host は、ansible 実行対象から見たホストになる模様。
(例) ansible のターゲットホストが 192.168.1.1 で host に 127.0.0.1 を指定した場合は、192.168.1.1 自身の 80 ポートを待機。
(参考)
ansible.builtin.wait_for module – Waits for a condition before continuing
- name: permit https to firewalld
firewalld:
service: https
state: enabled
permanent: true
immediate: true
https://docs.ansible.com/ansible/latest/collections/ansible/posix/firewalld_module.html
- name: permit port 8080 to firewalld
firewalld:
port: 8080/tcp
state: enabled
permanent: true
immediate: true
- name: write string to text file
copy:
dest: /home/hoge/hoge.txt
content: "hoge is {{ hoge }}\n"
- name: write string to text file
copy:
dest: /home/hoge/hoge.txt
content: |
hoge is {{ hoge }}
fuga is {{ fuga }}
※”|” で複数行に分ける場合は、変数を使っていても文字列はダブルクォートで囲まなくてよい。
- name: replace string in text file
replace:
path: /home/hoge/hoge.txt
regexp: '正規表現'
replace: '置換文字列'
変換対象が複数の場合
- name: replace string in text file
replace:
path: /home/hoge/hoge.txt
regexp: "{{ item.regexp }}"
replace: "{{ item.replace }}"
with_items:
- regexp: hoge
replace: fuga
- regexp: foo
replace: bar
- name: replace by line in text file
lineinfile:
path: /home/hoge/hoge.txt
regexp: '正規表現'
line: '置換文字列'
変換対象が複数の場合
- name: replace by line in text file
lineinfile:
path: /home/hoge/hoge.txt
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
with_items:
- regexp: '^hoge'
line: fuga
- regexp: '^foo'
line: bar
- name: replace by line in text file
lineinfile:
path: /home/hoge/hoge.txt
backrefs: true
regexp: 'xxx1=(\d+);'
line: 'xxx2=\1;'
- name: exec hoge.sh
shell:
cmd: /home/hoge/hoge.sh
chdir: /home/hoge
become: true
become_user: hoge
- name: exec hoge.sh
shell:
cmd: /home/hoge/hoge.sh | grep hoge | awk '{print $3;}' | sed -e 's/hoge/fuga/'
chdir: /home/hoge
register: exec_hoge
become: true
become_user: hoge
- name: exec if hoge is fuga
shell:
# hoge.sh の標準出力を grep/awk/sed した内容をパラメーターで渡す
cmd: /home/hoge/fuga.sh {{ exec_hoge.stdout }}
# hoge.sh の実行ステータスが 0 で、grep/awk/sed した後の標準出力が "fuga" の場合に実行
when: exec_hoge.rc is defind && exec_hoge.rc == 0 && exec_hoge.stdout == "fuga"
- name: grep
shell:
cmd: grep -L hoge *.txt
register: grep_hoge
- name: set_fact
set_fact:
exists_hoge: '{{ grep_hoge.stdout_lines | select("regex", "^[0-9]+hoge\.txt$") | list | count > 0 }}'
exists_fuga: '{{ grep_hoge.stdout_lines | select("regex", "^[0-9]+fuga\.txt$") | list | count > 0 }}'
- name: message hoge
debug:
msg: hoge exits
when: exists_hoge
- name: message fuga
debug:
msg: fuga exits
when: exists_fuga
hoge を含む拡張子 txt のファイルのうち、ファイル名が数字で始まり、hoge.txt で終わるファイルと、fuga.txt で終わるファイルが存在するかどうかをチェック。
(他にも良い方法がありそうですがフィルタを使ったケースとして)
- name: exec hoge.sh
shell:
cmd: /home/hoge/hoge.sh
chdir: /home/hoge
changed_when: false
- hosts: hoge_host
roles:
- hoge_role
tags:
- never
- hoge
ansible-playbook -t hoge とパラメーター指定した場合だけ実行される。
(参考) [Ansible] 通常時は実行せず、タグが指定されたときのみタスクを実行する
- name: copy ash-data.yml template
shell:
cmd: /home/hoge/hoge.sh {{ param1 }} {{ param2 }}
vars:
param1: >-
{%- if user == 'hoge' -%} 1
{%- elif user == 'hogehoge' -%} 2
{%- else -%} 3
{%- endif -%}
param2: >-
{%- if target == 'fuga' -%} 1
{%- elif target == 'fugafuga' -%} 2
{%- else -%} 3
{%- endif -%}
「{\%-」で始まる行は、変数名の定義からインデントさせないと「Syntax Error」が発生。
- hosts: hoge
vars_prompt:
- name: hoge_value
prompt: "input value of hoge"
private: no
roles:
- hoge
入力された文字列が name に指定した hoge_value にセットされる。
- name: input about hoge
pause:
echo: true
prompt: "input about hoge"
register: about_hoge
- name: debug
debug:
msg: "input is {{ about_hoge.user_input }}"
入力した内容は .user_input で参照できる。
- name: exec hoge.sh
shell:
cmd: /home/hoge/hoge.sh
chdir: /home/hoge
register: exec_hoge
failed_when: exec_hoge.stderr is defined and exec_hoge.stderr != ''
シェルの実行結果が 0 であっても、エラー出力が空でない場合はエラーにする。
=> register に指定した変数の内容が画面に出力される。
- name: hoge
set_fact:
url: https://example.com/
when: ansible_distribution == "xxx"
- name: include var file
include_vars: "{{ ansible_distribution }}.yml"
ansible <ホスト名> -m setup
※ホスト名: Ansible の hosts ファイルに指定したターゲットホスト名
{
"ansible_facts": {
"ansible_architecture": "x86_64",
"ansible_default_ipv4": {
"address": "172.21.xx.xx",
"alias": "eth1",
"broadcast": "172.xx.xx.255",
"gateway": "172.21.xx.xx",
"interface": "eth1",
"macaddress": "00:16:3e:xx:xx:xx",
"mtu": 1500,
"netmask": "255.xx.xx.0",
"network": "172.xx.xx.0",
"type": "ether"
},
"ansible_distribution": "OracleLinux",
"ansible_distribution_file_search_string": "Oracle Linux",
"ansible_distribution_file_variety": "OracleLinux",
"ansible_distribution_major_version": "8",
"ansible_distribution_release": "NA",
"ansible_distribution_version": "8.4",
"ansible_dns": {
"nameservers": [
"172.21.xx.xx"
]
},
"ansible_domain": "",
"ansible_env": {
"LANG": "C",
"LC_CTYPE": "C.UTF-8",
"USER": "root",
},
"ansible_eth0": {
"active": true,
"device": "eth0",
"ipv4": {
"address": "10.xx.xx.xx",
"broadcast": "10.xx.xx.xx",
"netmask": "255.xx.xx.0",
"network": "10.xx.xx.0"
},
"macaddress": "11:22:33:44:55:66",
"type": "ether"
},
"ansible_eth1": {
"active": true,
"device": "eth1",
"ipv4": {
"address": "172.xx.xx.xx",
"broadcast": "172.xx.xx.255",
"netmask": "255.xx.xx.0",
"network": "172.xx.xx.0"
},
"macaddress": "11:22:33:44:55:67",
"mtu": 1500,
"promisc": false,
"type": "ether"
},
"ansible_hostname": "myserver",
"ansible_interfaces": [
"eth0",
"eth1",
"lo"
],
"ansible_machine": "x86_64",
"ansible_memfree_mb": 6516,
"ansible_memory_mb": {
"nocache": {
"free": 6614,
"used": 751
},
"real": {
"free": 6516,
"total": 7365,
"used": 849
},
"swap": {
"cached": 0,
"free": 0,
"total": 0,
"used": 0
}
},
"ansible_memtotal_mb": 7365,
"ansible_nodename": "myserver",
"ansible_os_family": "RedHat",
"ansible_pkg_mgr": "dnf",
"ansible_processor": [
"0",
"GenuineIntel",
"Intel(R) Core(TM) ix-xxxxxx CPU @ 2.xxGHz",
"1",
"GenuineIntel",
"Intel(R) Core(TM) ix-xxxxxx CPU @ 2.xxGHz",
"2",
"GenuineIntel",
"Intel(R) Core(TM) ix-xxxxxx CPU @ 2.xxGHz",
"3",
"GenuineIntel",
"Intel(R) Core(TM) ix-xxxxxx CPU @ 2.xxGHz"
],
"ansible_processor_cores": 4,
"ansible_processor_count": 1,
"ansible_processor_threads_per_core": 1,
"ansible_processor_vcpus": 4,
"ansible_product_name": "VirtualBox",
"ansible_selinux": {
"status": "disabled"
},
"ansible_service_mgr": "systemd",
"ansible_system": "Linux",
"ansible_user_dir": "/root",
"ansible_user_gecos": "root",
"ansible_user_gid": 0,
"ansible_user_id": "root",
"ansible_user_shell": "/bin/bash",
"ansible_user_uid": 0,
"ansible_userspace_architecture": "x86_64",
"ansible_userspace_bits": "64",
"ansible_virtualization_role": "guest",
"ansible_virtualization_type": "lxc",
}
}