Ansible : Playbooks - First Steps
This article presents some examples of basic Ansible Playbooks, to give you a feel for how Ansible Playbooks work.
oracle miscconfigurationintermediate
by OracleDba
14 views
This article presents some examples of basic Ansible Playbooks, to give you a feel for how Ansible Playbooks work.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
---
- name: Configure NGINX servers
hosts: appservers
become: true
tasks:
- name: Install NGINX package
dnf:
name: nginx
state: present
update_cache: yes
- name: Enable and start NGINX service
service:
name: nginx
enabled: yes
state: started
$ ansible-playbook configure_nginx.yml
PLAY [Configure NGINX servers] *******************************************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************************************************
ok: [appserver2.localdomain]
ok: [appserver1.localdomain]
TASK [Install NGINX package] *********************************************************************************************************************************************************************
changed: [appserver1.localdomain]
changed: [appserver2.localdomain]
TASK [Enable and start NGINX service] ************************************************************************************************************************************************************
changed: [appserver1.localdomain]
changed: [appserver2.localdomain]
PLAY RECAP ***************************************************************************************************************************************************************************************
appserver1.localdomain : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
appserver2.localdomain : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$
---
- name: Configure NGINX servers
hosts: appservers
become: true
tasks:
- name: Install NGINX package
dnf:
name: nginx
state: present
update_cache: yes
- name: Enable and start NGINX service
service:
name: nginx
enabled: yes
state: started
- name: Allow SSH traffic through the firewall
firewalld:
service: ssh
permanent: yes
state: enabled
- name: Allow HTTPS traffic through the firewall
firewalld:
service: https
permanent: yes
state: enabled
- name: Enable the firewall
service:
name: firewalld
enabled: yes
state: started
$ ansible-playbook configure_nginx.yml
PLAY [Configure NGINX servers] *****************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************
ok: [appserver2.localdomain]
ok: [appserver1.localdomain]
TASK [Install NGINX package] *******************************************************************************************************************
ok: [appserver1.localdomain]
ok: [appserver2.localdomain]
TASK [Enable and start NGINX service] **********************************************************************************************************
ok: [appserver2.localdomain]
ok: [appserver1.localdomain]
TASK [Allow SSH traffic through the firewall] **************************************************************************************************
ok: [appserver1.localdomain]
ok: [appserver2.localdomain]
TASK [Allow HTTPS traffic through the firewall] ************************************************************************************************
changed: [appserver1.localdomain]
changed: [appserver2.localdomain]
TASK [Enable the firewall] *********************************************************************************************************************
changed: [appserver2.localdomain]
changed: [appserver1.localdomain]
PLAY RECAP *************************************************************************************************************************************
appserver1.localdomain : ok=6 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
appserver2.localdomain : ok=6 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$
---
- name: Configure NGINX servers
hosts: appservers
become: true
tasks:
- name: Install NGINX package (DNF)
dnf:
name: nginx
state: present
update_cache: yes
when: ansible_distribution in ["OracleLinux", "Red Hat Enterprise Linux", "CentOS"]
- name: Install NGINX package (APT)
apt:
name: nginx
state: present
update_cache: yes
when: ansible_distribution in ["Ubuntu", "Debian"]
- name: Enable and start NGINX service
service:
name: nginx
enabled: yes
state: started
- name: Allow SSH traffic through the firewall
firewalld:
service: ssh
permanent: yes
state: enabled
- name: Allow HTTPS traffic through the firewall
firewalld:
service: https
permanent: yes
state: enabled
- name: Enable the firewall
service:
name: firewalld
enabled: yes
state: started
$ ansible-playbook configure_nginx.yml
PLAY [Configure NGINX servers] *****************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************
ok: [appserver2.localdomain]
ok: [appserver1.localdomain]
TASK [Install NGINX package (DNF)] *************************************************************************************************************
ok: [appserver1.localdomain]
ok: [appserver2.localdomain]
TASK [Install NGINX package (APT)] *************************************************************************************************************
skipping: [appserver1.localdomain]
skipping: [appserver2.localdomain]
TASK [Enable and start NGINX service] **********************************************************************************************************
ok: [appserver1.localdomain]
ok: [appserver2.localdomain]
TASK [Allow SSH traffic through the firewall] **************************************************************************************************
ok: [appserver2.localdomain]
ok: [appserver1.localdomain]
TASK [Allow HTTPS traffic through the firewall] ************************************************************************************************
ok: [appserver2.localdomain]
ok: [appserver1.localdomain]
TASK [Enable the firewall] *********************************************************************************************************************
ok: [appserver1.localdomain]
ok: [appserver2.localdomain]
PLAY RECAP *************************************************************************************************************************************
appserver1.localdomain : ok=6 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
appserver2.localdomain : ok=6 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
$
$ ansible database1.localdomain -m setup
$ ansible database1.localdomain -m gather_facts123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
---
- name: Patch servers
hosts: databases
become: true
tasks:
- name: Update all packages
dnf:
name: "*"
update_cache: yes
state: latest
$ ansible-playbook update_database_packages.yml
PLAY [Patch servers] ***************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************
ok: [database1.localdomain]
TASK [Update all packages] *********************************************************************************************************************
ok: [database1.localdomain]
PLAY RECAP *************************************************************************************************************************************
database1.localdomain : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$
---
- name: Patch servers
hosts: databases
become: true
tasks:
- name: Update all packages
dnf:
name: "*"
update_cache: yes
state: latest
- name: Reboot server
reboot:
$ ansible-playbook update_database_packages.yml
PLAY [Patch servers] ***************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************
ok: [database1.localdomain]
TASK [Update all packages] *********************************************************************************************************************
ok: [database1.localdomain]
TASK [Reboot server] ***************************************************************************************************************************
changed: [database1.localdomain]
PLAY RECAP *************************************************************************************************************************************
database1.localdomain : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$
---
- name: Patch servers
hosts: databases
become: true
tasks:
- name: Update all packages (DNF)
dnf:
name: "*"
update_cache: yes
state: latest
when: ansible_distribution in ["OracleLinux", "Red Hat Enterprise Linux", "CentOS"]
- name: Update all packages (APT)
apt:
name: "*"
update_cache: yes
state: latest
when: ansible_distribution in ["Ubuntu", "Debian"]
- name: Reboot server
reboot:
$ ansible-playbook update_database_packages.yml
PLAY [Patch servers] ***************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************
ok: [database1.localdomain]
TASK [Update all packages (DNF)] ***************************************************************************************************************
ok: [database1.localdomain]
TASK [Update all packages (APT)] ***************************************************************************************************************
skipping: [database1.localdomain]
TASK [Reboot server] ***************************************************************************************************************************
changed: [database1.localdomain]
PLAY RECAP *************************************************************************************************************************************
database1.localdomain : ok=3 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
$
---
- name: Patch servers
hosts: databases
become: true
tasks:
- name: Update all packages (DNF)
dnf:
name: "*"
update_cache: yes
state: latest
when: ansible_distribution in ["OracleLinux", "Red Hat Enterprise Linux", "CentOS"]
register: dnf_update
- name: Update all packages (APT)
apt:
name: "*"
update_cache: yes
state: latest
when: ansible_distribution in ["Ubuntu", "Debian"]
register: apt_update
- name: Reboot server
reboot:
when: dnf_update.changed or apt_update.changed
$ ansible-playbook update_database_packages.yml
PLAY [Patch servers] ***************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************
ok: [database1.localdomain]
TASK [Update all packages (DNF)] ***************************************************************************************************************
ok: [database1.localdomain]
TASK [Update all packages (APT)] ***************************************************************************************************************
skipping: [database1.localdomain]
TASK [Reboot server] ***************************************************************************************************************************
skipping: [database1.localdomain]
PLAY RECAP *************************************************************************************************************************************
database1.localdomain : ok=2 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
$1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
---
- name: Using tags
hosts: appservers
become: true
tasks:
- name: Install NGINX package
tags: nginx
dnf:
name: nginx
state: present
update_cache: yes
- name: Enable and start NGINX service
tags: nginx
service:
name: nginx
enabled: yes
state: started
- name: Allow SSH traffic through the firewall
tags: firewall,fwrule
firewalld:
service: ssh
permanent: yes
state: enabled
- name: Allow HTTPS traffic through the firewall
tags: firewall,fwrule
firewalld:
service: https
permanent: yes
state: enabled
- name: Enable the firewall
tags: firewall,fwservice
service:
name: firewalld
enabled: yes
state: started
$ ansible-playbook --list-tags tags.yml
playbook: tags.yml
play #1 (appservers): Using tags TAGS: []
TASK TAGS: [firewall, nginx, rule, service]
$
$ ansible-playbook --tags "firewall" tags.yml
$ ansible-playbook --tags "nginx" tags.yml
$ ansible-playbook --tags "nginx,fwrule" tags.yml12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
<html>
<title>Default Page</title>
<body>
<p>This is the default page!</p>
</body>
</html>
---
- name: Configure NGINX servers
hosts: appservers
become: true
tasks:
- name: Install NGINX package
dnf:
name: nginx
state: present
update_cache: yes
- name: Enable and start NGINX service
service:
name: nginx
enabled: yes
state: started
- name: Allow SSH traffic through the firewall
firewalld:
service: ssh
permanent: yes
state: enabled
- name: Allow HTTPS traffic through the firewall
firewalld:
service: https
permanent: yes
state: enabled
- name: Enable the firewall
service:
name: firewalld
enabled: yes
state: started
- name: Copy default web page
copy:
src: default_page.html
dest: /usr/share/nginx/html/index.html
owner: root
group: root
mode: 0644
$ ansible-playbook configure_nginx_2.yml
PLAY [Configure NGINX servers] *****************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************
ok: [appserver1.localdomain]
ok: [appserver2.localdomain]
TASK [Install NGINX package] *******************************************************************************************************************
ok: [appserver2.localdomain]
ok: [appserver1.localdomain]
TASK [Enable and start NGINX service] **********************************************************************************************************
ok: [appserver2.localdomain]
ok: [appserver1.localdomain]
TASK [Allow SSH traffic through the firewall] **************************************************************************************************
ok: [appserver2.localdomain]
ok: [appserver1.localdomain]
TASK [Allow HTTPS traffic through the firewall] ************************************************************************************************
ok: [appserver1.localdomain]
ok: [appserver2.localdomain]
TASK [Enable the firewall] *********************************************************************************************************************
ok: [appserver1.localdomain]
ok: [appserver2.localdomain]
TASK [Copy default web page] *******************************************************************************************************************
changed: [appserver1.localdomain]
changed: [appserver2.localdomain]
PLAY RECAP *************************************************************************************************************************************
appserver1.localdomain : ok=7 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
appserver2.localdomain : ok=7 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
---
- name: Add basic packages
hosts: all
become: true
tasks:
- name: Install packages
dnf:
name:
- zip
- unzip
- wget
state: present
update_cache: yes
---
- name: Configure firewall
hosts: appservers
become: true
tasks:
- name: Allow web through the firewall
firewalld:
port: "{{ item }}"
permanent: yes
state: enabled
with_items:
- 80/tcp
- 443/tcp
- 8080/tcp
- 8443/tcp
---
- name: Configure firewall
hosts: appservers
become: true
tasks:
- name: Allow web through the firewall
firewalld:
port: "{{ item.port }}"
permanent: yes
state: "{{ item.state}}"
with_items:
- { port: 80/tcp, state: disabled }
- { port: 443/tcp, state: enabled }
- { port: 8080/tcp, state: disabled }
- { port: 8443/tcp, state: disabled }
---
- name: Configure firewall
hosts: appservers
become: true
tasks:
- name: Allow web through the firewall
firewalld:
port: "{{ item.port }}"
permanent: yes
state: "{{ item.state}}"
loop:
- { port: 80/tcp, state: disabled }
- { port: 443/tcp, state: enabled }
- { port: 8080/tcp, state: disabled }
- { port: 8443/tcp, state: disabled }
---
- name: Configure firewall
hosts: appservers
become: true
vars:
fwrules:
- { port: 80/tcp, state: disabled }
- { port: 443/tcp, state: enabled }
- { port: 8080/tcp, state: disabled }
- { port: 8443/tcp, state: disabled }
tasks:
- name: Allow web through the firewall
firewalld:
port: "{{ item.port }}"
permanent: yes
state: "{{ item.state}}"
loop: "{{ fwrules }}"
---
- name: Configure firewall
hosts: appservers
become: true
vars:
fwrules:
- { port: 80/tcp, state: disabled }
- { port: 443/tcp, state: enabled }
- { port: 8080/tcp, state: disabled }
- { port: 8443/tcp, state: disabled }
tasks:
- name: Allow web through the firewall
firewalld:
port: "{{ item.port }}"
permanent: yes
state: "{{ item.state}}"
with_items: "{{ fwrules }}"123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
---
- name: Create groups and users
hosts: databases
become: true
tasks:
- name: Create groups
group:
gid: "{{ item.group_id}}"
name: "{{ item.group_name}}"
state: present
with_items:
- { group_name: oinstall, group_id: 54321}
- { group_name: dba, group_id: 54322}
- { group_name: oper, group_id: 54323 }
$ ansible-playbook groups_and_users.yml
PLAY [Create groups and users] *****************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************
ok: [database1.localdomain]
TASK [Create groups] ***************************************************************************************************************************
changed: [database1.localdomain] => (item={'group_name': 'oinstall', 'group_id': 54321})
changed: [database1.localdomain] => (item={'group_name': 'dba', 'group_id': 54322})
changed: [database1.localdomain] => (item={'group_name': 'oper', 'group_id': 54323})
PLAY RECAP *************************************************************************************************************************************
database1.localdomain : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$
---
- name: Create groups and users
hosts: databases
become: true
tasks:
- name: Create groups
group:
gid: "{{ item.group_id}}"
name: "{{ item.group_name}}"
state: present
with_items:
- { group_name: oinstall, group_id: 54321}
- { group_name: dba, group_id: 54322}
- { group_name: oper, group_id: 54323 }
- name: Create oracle user
user:
uid: 54321
name: oracle
password: "{{ 'DummyPassword123' | password_hash('sha512', 'mysecretsalt') }}"
groups: oinstall,dba,oper
append: yes
state: present
update_password: on_create
$ ansible-playbook groups_and_users.yml
PLAY [Create groups and users] *****************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************
ok: [database1.localdomain]
TASK [Create groups] ***************************************************************************************************************************
ok: [database1.localdomain] => (item={'group_name': 'oinstall', 'group_id': 54321})
ok: [database1.localdomain] => (item={'group_name': 'dba', 'group_id': 54322})
ok: [database1.localdomain] => (item={'group_name': 'oper', 'group_id': 54323})
TASK [Create oracle user] **********************************************************************************************************************
changed: [database1.localdomain]
PLAY RECAP *************************************************************************************************************************************
database1.localdomain : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$
$ ansible-playbook groups_and_users.yml
PLAY [Create groups and users] *****************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************
ok: [database1.localdomain]
TASK [Create groups] ***************************************************************************************************************************
ok: [database1.localdomain] => (item={'group_name': 'oinstall', 'group_id': 54321})
ok: [database1.localdomain] => (item={'group_name': 'dba', 'group_id': 54322})
ok: [database1.localdomain] => (item={'group_name': 'oper', 'group_id': 54323})
TASK [Create oracle user] **********************************************************************************************************************
ok: [database1.localdomain]
PLAY RECAP *************************************************************************************************************************************
database1.localdomain : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$
$ ansible all -i localhost, -m debug -a "msg={{ 'DummyPassword123' | password_hash('sha512', 'mysecretsalt') }}"
localhost | SUCCESS => {
"msg": "$6$mysecretsalt$RP/rxvw0AG/pfo/SLr9LEkQuxGBsFlrfU01cWAcWMxOAA4leVi7j1Y2UzIIU1YyrqyWbpuiE/Ic7efvLJYzaE/"
}
$
---
- name: Create groups and users
hosts: databases
become: true
tasks:
- name: Create groups
group:
gid: "{{ item.group_id}}"
name: "{{ item.group_name}}"
state: present
with_items:
- { group_name: oinstall, group_id: 54321}
- { group_name: dba, group_id: 54322}
- { group_name: oper, group_id: 54323 }
- name: Create oracle user
user:
uid: 54321
name: oracle
password: "$6$mysecretsalt$RP/rxvw0AG/pfo/SLr9LEkQuxGBsFlrfU01cWAcWMxOAA4leVi7j1Y2UzIIU1YyrqyWbpuiE/Ic7efvLJYzaE/"
groups: oinstall,dba,oper
append: yes
state: present
update_password: on_create1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
mkdir -p host_vars
hostname: database1.localdomain
short_hostname: database1
ip_address: 192.168.56.103
packages:
- zip
- unzip
- tar
- cpio
- tmux
- oracle-database-preinstall-19c
fwrules:
- 22/tcp
- 1521/tcp
---
- name: Use host variables
hosts: databases
become: true
tasks:
- name: Add hosts entry
lineinfile:
state: present
dest: /etc/hosts
line: "{{ ip_address }} {{ hostname }} {{ short_hostname }}"
- name: Install packages
dnf:
name: "{{ packages }}"
update_cache: yes
state: latest
- name: Configure firewall
firewalld:
port: "{{ item }}"
permanent: yes
state: enabled
with_items: "{{ fwrules }}"
$ ansible-playbook host_variables.yml
PLAY [Use host variables] **********************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************
ok: [database1.localdomain]
TASK [Add hosts entry] *************************************************************************************************************************
changed: [database1.localdomain]
TASK [Install packages] ************************************************************************************************************************
ok: [database1.localdomain]
TASK [Configure firewall] **********************************************************************************************************************
ok: [database1.localdomain] => (item=22/tcp)
ok: [database1.localdomain] => (item=1521/tcp)
PLAY RECAP *************************************************************************************************************************************
database1.localdomain : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$12345678910111213141516171819202122232425262728293031323334
mkdir -p group_vars
ssh_users: "oracle tim"
---
- name: Use group variables
hosts: appservers
tasks:
- name: Show variable value
debug:
var: ssh_users
$ ansible-playbook group_variables.yml
PLAY [Use group variables] *********************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************
ok: [appserver2.localdomain]
ok: [appserver1.localdomain]
TASK [Show variable value] *********************************************************************************************************************
ok: [appserver1.localdomain] => {
"ssh_users": "oracle tim"
}
ok: [appserver2.localdomain] => {
"ssh_users": "oracle tim"
}
PLAY RECAP *************************************************************************************************************************************
appserver1.localdomain : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
appserver2.localdomain : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
---
- name: Configure appservers
hosts: appservers
become: true
tasks:
- name: Amend nginx.conf
lineinfile:
state: present
dest: /etc/nginx/nginx.conf
line: "# One silly a line added."
register: nginx1
- name: Restart nginx
service:
name: nginx
state: restarted
when: nginx1.changed
---
- name: Configure appservers
hosts: appservers
become: true
tasks:
- name: Amend nginx.conf
lineinfile:
state: present
dest: /etc/nginx/nginx.conf
line: "# One silly a line added."
register: nginx1
- name: Amend nginx.conf
lineinfile:
state: present
dest: /etc/nginx/nginx.conf
line: "# Another silly a line added."
register: nginx2
- name: Restart nginx
service:
name: nginx
state: restarted
when: nginx1.changed or nginx2.changed
---
- name: Configure appservers
hosts: appservers
become: true
tasks:
- name: Amend nginx.conf
lineinfile:
state: present
dest: /etc/nginx/nginx.conf
line: "# One silly a line added."
notify: Restart nginx
- name: Amend nginx.conf
lineinfile:
state: present
dest: /etc/nginx/nginx.conf
line: "# Another silly a line added."
notify: Restart nginx
handlers:
- name: Restart nginx
service:
name: nginx
state: restarted
---
- name: Configure appservers
hosts: appservers
become: true
tasks:
- name: Amend nginx.conf
lineinfile:
state: present
dest: /etc/nginx/nginx.conf
line: "# One silly a line added."
notify:
- Restart nginx
- Restart firewall
handlers:
- name: Restart nginx
service:
name: nginx
state: restarted
- name: Restart firewall
service:
name: firewalld
state: restarted
---
- name: Configure appservers
hosts: appservers
become: true
tasks:
- name: Amend nginx.conf
lineinfile:
state: present
dest: /etc/nginx/nginx.conf
line: "# One silly a line added."
notify: "restart stuff"
handlers:
- name: Restart nginx
service:
name: nginx
state: restarted
listen: "restart stuff"
- name: Restart firewall
service:
name: firewalld
state: restarted
listen: "restart stuff"
- name: Restart nginx
service:
name: nginx
state: restarted
listen: "restart stuff"
- name: Restart firewall
service:
name: firewalld
state: restarted
listen: "restart stuff"
---
- name: Configure appservers
hosts: appservers
become: true
tasks:
- name: Amend nginx.conf
lineinfile:
state: present
dest: /etc/nginx/nginx.conf
line: "# One silly a line added."
notify: "restart stuff"
handlers:
- import_tasks: handlers/main.yml123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
mkdir templates
AllowUsers {{ ssh_users }}
ssh_users: "oracle tim"
---
- name: Configure databases
hosts: databases
become: true
tasks:
- name: Generate 99-my-sshd-users.conf file
template:
src: 99-my-sshd-users.conf.j2
dest: /etc/ssh/ssh_config.d/99-my-sshd-users.conf
group: root
owner: root
mode: 0644
notify: Restart sshd
handlers:
- name: Restart sshd
service:
name: sshd
state: restarted
$ ansible-playbook templates.yml
PLAY [Configure databases] *********************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************
ok: [database1.localdomain]
TASK [Generate 99-my-sshd-users.conf file] *****************************************************************************************************
changed: [database1.localdomain]
RUNNING HANDLER [Restart sshd] *****************************************************************************************************************
changed: [database1.localdomain]
PLAY RECAP *************************************************************************************************************************************
database1.localdomain : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$
[root@database1 ssh_config.d]# cat 99-my-sshd-users.conf
AllowUsers oracle tim
[root@database1 ssh_config.d]#Please to add comments
No comments yet. Be the first to comment!