From d8a7729358a2fd3b911022e45d0197fda3e5da94 Mon Sep 17 00:00:00 2001 From: boom2 Date: Fri, 8 Dec 2023 14:12:33 -0500 Subject: - add exeriments for: - roles - variables - inventory changes - config changes --- ansible-practice/12-playbook-copy-directory.yml~ | 14 ------- .../references/ansible-role-hello-world | 1 + .../system/03-playbook-print-gateway.yml | 16 ++++++++ .../system/04-playbook-bash-configs.yml | 12 ++++++ .../system/05-playbook-bash-configs.yml | 31 +++++++++++++++ .../system/06-playbook-print-group-vars.yml | 44 ++++++++++++++++++++++ .../system/07-playbook-bash-configs.yml | 11 ++++++ ansible-practice/system/vars/localhost.yml | 3 ++ ansible-practice/system/vars/otherhosts.yml | 3 ++ 9 files changed, 121 insertions(+), 14 deletions(-) delete mode 100644 ansible-practice/12-playbook-copy-directory.yml~ create mode 160000 ansible-practice/references/ansible-role-hello-world create mode 100644 ansible-practice/system/03-playbook-print-gateway.yml create mode 100644 ansible-practice/system/04-playbook-bash-configs.yml create mode 100644 ansible-practice/system/05-playbook-bash-configs.yml create mode 100644 ansible-practice/system/06-playbook-print-group-vars.yml create mode 100644 ansible-practice/system/07-playbook-bash-configs.yml create mode 100644 ansible-practice/system/vars/localhost.yml create mode 100644 ansible-practice/system/vars/otherhosts.yml (limited to 'ansible-practice') diff --git a/ansible-practice/12-playbook-copy-directory.yml~ b/ansible-practice/12-playbook-copy-directory.yml~ deleted file mode 100644 index 1d68918..0000000 --- a/ansible-practice/12-playbook-copy-directory.yml~ +++ /dev/null @@ -1,14 +0,0 @@ -# copy a directory from control node to managed node -# https://www.freekb.net/Article?id=759 -# https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html -# - ---- -- name: "12 - Playing with Ansible - copy a directory from contol to managed node" - hosts: all - tasks: - - name: copy a directory from contol to managed node - ansible.builtin.copy: - src: /home/dpierre/ansible-practice/ - dest: /home/dpierre/tmp/temp_files/ansible-practice -... \ No newline at end of file diff --git a/ansible-practice/references/ansible-role-hello-world b/ansible-practice/references/ansible-role-hello-world new file mode 160000 index 0000000..887e566 --- /dev/null +++ b/ansible-practice/references/ansible-role-hello-world @@ -0,0 +1 @@ +Subproject commit 887e5661819a186060190fe98c508fc6826a41a7 diff --git a/ansible-practice/system/03-playbook-print-gateway.yml b/ansible-practice/system/03-playbook-print-gateway.yml new file mode 100644 index 0000000..7fd2277 --- /dev/null +++ b/ansible-practice/system/03-playbook-print-gateway.yml @@ -0,0 +1,16 @@ +# display some per host info using 'magic variables' +# https://docs.ansible.com/ansible/latest/collections/ansible/builtin/debug_module.html +# https://docs.ansible.com/ansible/latest/inventory/implicit_localhost.html +# https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_delegation.html +# + +--- +- name: "03 - display some per host info" + hosts: all +# hosts: localhost +# connection: local + tasks: + - name: Print the gateway for each host when defined + ansible.builtin.debug: + msg: System {{ inventory_hostname }} has gateway {{ ansible_default_ipv4.gateway }} + when: ansible_default_ipv4.gateway is defined \ No newline at end of file diff --git a/ansible-practice/system/04-playbook-bash-configs.yml b/ansible-practice/system/04-playbook-bash-configs.yml new file mode 100644 index 0000000..99da687 --- /dev/null +++ b/ansible-practice/system/04-playbook-bash-configs.yml @@ -0,0 +1,12 @@ +# - makes use of 'roles' +# - experimenting w/ including variable files +# - this only works when we create a '/vars' directory w/ our variables in it + +--- +- hosts: all +#- hosts: localhost +# connection: local + vars_files: + - [ "vars/{{ inventory_hostname }}.yml", "vars/otherhosts.yml" ] + roles: + - bash_config_old diff --git a/ansible-practice/system/05-playbook-bash-configs.yml b/ansible-practice/system/05-playbook-bash-configs.yml new file mode 100644 index 0000000..3f56745 --- /dev/null +++ b/ansible-practice/system/05-playbook-bash-configs.yml @@ -0,0 +1,31 @@ +# - similar to '02-playbook-copy-bash-configs.yml' but uses 'vars_files' +# to store the variable 'prefix_dir' inside files representing +# the environment + +--- +- name: "05 - custom ansible - copy bash config files w/ content" + hosts: localhost + connection: local + vars_files: + - [ "vars/{{ inventory_hostname }}.yml", "vars/otherhosts.yml" ] + tasks: + - name: copy '.bash_aliases' + ansible.builtin.copy: + src: ~/repos/ansible_repo/ansible-practice/system/.bash_aliases + dest: "{{ prefix_dir }}.bash_aliases" + mode: '0644' + tags: ['bash_aliases'] + + - name: Creating '.bashrc' file with content + ansible.builtin.copy: + src: ~/repos/ansible_repo/ansible-practice/system/.bashrc + dest: "{{ prefix_dir }}.bashrc" + mode: '0644' + tags: ['bashrc'] + + - name: Creating '.bash_profile' file with content + ansible.builtin.copy: + src: ~/repos/ansible_repo/ansible-practice/system/.bash_profile + dest: "{{ prefix_dir }}.bash_profile" + mode: '0644' + tags: ['bash_profile'] diff --git a/ansible-practice/system/06-playbook-print-group-vars.yml b/ansible-practice/system/06-playbook-print-group-vars.yml new file mode 100644 index 0000000..850bea2 --- /dev/null +++ b/ansible-practice/system/06-playbook-print-group-vars.yml @@ -0,0 +1,44 @@ + +# https://stackoverflow.com/questions/53253879/ansible-vars-files-vs-include-vars +# https://docs.ansible.com/ansible/2.7/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable + +# https://www.middlewareinventory.com/blog/run-ansible-playbook-locally/ +# https://nixzie.com/run-ansible-playbook-locally/#Run_Ansible_Playbook_Locally_Using_Local_Action +# https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_delegation.html#delegating-facts +# https://docs.ansible.com/ansible/latest/collections/ansible/builtin/debug_module.html +# https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html + +--- +- name: "06" + hosts: localhost +# hosts: "*" + tasks: + - name: Print the gateway for each host when defined + ansible.builtin.debug: + msg: System {{ inventory_hostname }} has gateway {{ ansible_default_ipv4.gateway }} + when: ansible_default_ipv4.gateway is defined + + - name: Print the ansible python interpreter + ansible.builtin.debug: + msg: + - "ansible_playbook_python = {{ ansible_playbook_python }}" +# - "ansible_python_interpreter = {{ ansible_python_interpreter }}" + + - name: Print the ansible python interpreter - for the localhost + ansible.builtin.debug: + msg: + - "ansible_playbook_python = {{ ansible_playbook_python }}" + - "ansible_host = {{ ansible_host }}" +# - "ansible_python_interpreter = {{ ansible_python_interpreter }}" + delegate_to: localhost + run_once: true + + - name: Print the ansible python interpreter - using 'local_action' + local_action: + module: ansible.builtin.debug + var: ansible_playbook_python + run_once: true + + - name: Print the group variable + ansible.builtin.debug: + msg: prefix variable = {{ prefix_dir }} diff --git a/ansible-practice/system/07-playbook-bash-configs.yml b/ansible-practice/system/07-playbook-bash-configs.yml new file mode 100644 index 0000000..be77bc7 --- /dev/null +++ b/ansible-practice/system/07-playbook-bash-configs.yml @@ -0,0 +1,11 @@ +# - similar to '04-playbook-copy-bash-configs.yml' +# - uses new changes in '~/.ansible/hosts' +# - uses new additions inside the following files to store 'prefix_dir': +# - '/home/dpierre/.ansible/group_vars/all' +# - '/home/dpierre/.ansible/host_vars/localhost' + +--- +- name: "07 - custom ansible - copy bash config files w/ content" + hosts: localhost + roles: + - bash_config \ No newline at end of file diff --git a/ansible-practice/system/vars/localhost.yml b/ansible-practice/system/vars/localhost.yml new file mode 100644 index 0000000..c5727b6 --- /dev/null +++ b/ansible-practice/system/vars/localhost.yml @@ -0,0 +1,3 @@ +--- +# vars for localhost +prefix_dir: ~/tmp/temp_files/ diff --git a/ansible-practice/system/vars/otherhosts.yml b/ansible-practice/system/vars/otherhosts.yml new file mode 100644 index 0000000..1fe8201 --- /dev/null +++ b/ansible-practice/system/vars/otherhosts.yml @@ -0,0 +1,3 @@ +--- +# vars for other hosts +prefix_dir: ~/ -- cgit v1.2.3-54-g00ecf