That's not just a marketing slogan. We really mean it and believe that. We strive to reduce complexity in how we've designed Ansible tools and encourage you to do the same.
Strive for simplification in what you automate.
If done properly, it can be the documentation of your workflow automation.
Ansible is a desired state engine by design. If you're trying to "write code" in your plays and roles, you're setting yourself up for failure. Our YAML-based playbooks were never meant to be for programming.
basic-project
├── inventory
│ ├── group_vars
│ │ └── web.yml
│ ├── host_vars
│ │ └── db1.yml
│ └── hosts
└── site.yml
myapp
├── roles
│ ├── myapp
│ │ ├── tasks
│ │ │ └── main.yml
│ │ └── ...
│ ├── nginx
│ │ └── ...
│ └── proxy
│ └── ...
└── site.yml
myapp/
├── config.yml
├── provision.yml
├── roles
│ └── requirements.yml
└── site.yml
10.1.2.75
10.1.5.45
10.1.4.5
10.1.0.40
w14301.acme.com
w17802.acme.com
w19203.acme.com
w19304.acme.com
[db]
db[1:4]
[web]
web[1:4]
db1 = db, east, dev
[east]
db1
web1
db3
web3
[west]
db2
web2
db4
web4
[dev]
db1
web1
[test]
db3
web3
[prod]
db2
web2
db4
web4
apache_max_keepalive: 25
apache_port: 80
tomcat_port: 8080
- name: Clone student lesson app for a user
host: nodes
tasks:
- name: Create ssh dir
file:
state: directory
path: /home/{{ username }}/.ssh
- name: Set Deployment Key
copy:
src: files/deploy_key
dest: /home/{{ username }}/.ssh/id_rsa
- name: Clone repo
git:
accept_hostkey: yes
clone: yes
dest: /home/{{ username }}/exampleapp
key_file: /home/{{ username }}/.ssh/id_rsa
repo: git@github.com:example/apprepo.git
- name: Clone student lesson app for a user
host: nodes
vars:
user_home_dir: "/home/{{ username }}"
user_ssh_dir: "{{ user_home_dir }}/.ssh"
deploy_key: "{{ user_ssh_dir }}/id_rsa"
app_dir: "{{ user_home_dir }}/exampleapp"
tasks:
- name: Create ssh dir
file:
state: directory
path: "{{ user_ssh_dir }}"
- name: Set Deployment Key
copy:
src: files/deploy_key
dest: "{{ deploy_key }}"
- name: Clone repo
git:
dest: "{{ app_dir }}"
key_file: "{{ deploy_key }}"
repo: git@github.com:example/exampleapp.git
accept_hostkey: yes
clone: yes
- name: install telegraf
yum: name=telegraf-{{ telegraf_version }} state=present update_cache=yes disable_gpg_check=yes enablerepo=telegraf
notify: restart telegraf
- name: configure telegraf
template: src=telegraf.conf.j2 dest=/etc/telegraf/telegraf.conf
- name: start telegraf
service: name=telegraf state=started enabled=yes
- name: install telegraf
yum: >
name=telegraf-{{ telegraf_version }}
state=present
update_cache=yes
disable_gpg_check=yes
enablerepo=telegraf
notify: restart telegraf
- name: configure telegraf
template: src=telegraf.conf.j2 dest=/etc/telegraf/telegraf.conf
- name: start telegraf
service: name=telegraf state=started enabled=yes
- name: install telegraf
yum:
name: telegraf-{{ telegraf_version }}
state: present
update_cache: yes
disable_gpg_check: yes
enablerepo: telegraf
notify: restart telegraf
- name: configure telegraf
template:
src: telegraf.conf.j2
dest: /etc/telegraf/telegraf.conf
notify: restart telegraf
- name: start telegraf
service:
name: telegraf
state: started
enabled: yes
- hosts: web
tasks:
- yum:
name: httpd
state: latest
- service:
name: httpd
state: started
enabled: yes
PLAY [web]
********************************
TASK [setup]
********************************
ok: [web1]
TASK [yum]
********************************
ok: [web1]
TASK [service]
********************************
ok: [web1]
- hosts: web
name: installs and starts apache
tasks:
- name: install apache packages
yum:
name: httpd
state: latest
- name: starts apache service
service:
name: httpd
state: started
enabled: yes
PLAY [install and starts apache]
********************************
TASK [setup]
********************************
ok: [web1]
TASK [install apache packages]
********************************
ok: [web1]
TASK [starts apache service]
********************************
ok: [web1]
- debug:
msg: "This always displays"
- debug:
msg: "This only displays with ansible-playbook -vv+"
verbosity: 2
- name: check for proper response
uri:
url: http://localhost/myapp
return_content: yes
register: result
until: '"Hello World" in result.content'
retries: 10
delay: 1
- name: add user
command: useradd appuser
- name: install apache
command: yum install httpd
- name: start apache
shell: |
service httpd start && chkconfig httpd on
- name: add user
user:
name: appuser
state: present
- name: install apache
yum:
name: httpd
state: latest
- name: start apache
service:
name: httpd
state: started
enabled: yes
- hosts: all
vars:
cert_store: /etc/mycerts
cert_name: my cert
tasks:
- name: check cert
shell: certify --list --name={{ cert_name }} --cert_store={{ cert_store }} | grep "{{ cert_name }}"
register: output
- name: create cert
command: certify --create --user=chris --name={{ cert_name }} --cert_store={{ cert_store }}
when: output.stdout.find(cert_name)" != -1
register: output
- name: sign cert
command: certify --sign --name={{ cert_name }} --cert_store={{ cert_store }}
when: output.stdout.find("created")" != -1
- hosts: all
vars:
cert_store: /etc/mycerts
cert_name: my cert
tasks:
- name: create and sign cert
certify:
state: present
sign: yes
user: chris
name: "{{ cert_name }}"
cert_store: "{{ cert_store }}"
See ansible.com/devel/dev_guide
acme_corp/
├── configure.yml
├── provision.yml
└── site.yml
$ cat site.yml
---
- include: provision.yml
- include: configure.yml
{{ ansible_managed | comment }}