Wednesday, July 13, 2022

Variables in Ansible

in case we need to install bulk of packages , we will need a variable with the packages names


root@dcex1510ctrl001:~# cat ansible/playbooks/variables.yaml
- name: Ansible playbook to study variables
  hosts: localhost
  vars:
    courses:
      - ansible
      - terraform
      - docker
  tasks:
  - name: Display Value
    debug:
      msg: "{{ courses[0] }}"


root@dcex1510ctrl001:~# ansible-playbook ansible/playbooks/variables.yaml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [Ansible playbook to study variables] ***************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [localhost]

TASK [Display Value] *************************************************************************************************************
ok: [localhost] => {
    "msg": "ansible"
}

PLAY RECAP ***********************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0



We will create a variable with kind array and a loop to install some packages

root@dcex1510ctrl001:~/ansible/playbooks/variablesPlaybooks# cat installPackagesLoop.yaml             
- name: Ansible playbook to install 2 packages and remove them through variables and loops
  hosts: client01
  vars:
    packages:
      - apache2
      - mysql-client
  tasks:
  - name: Install the packages
    apt:
      name: "{{ item }}"
      state: present
    loop: "{{ packages }}"
  - name: Remove the packages
    apt:
      name: "{{ item }}"
      state: absent
    loop: "{{ packages }}"


root@dcex1510ctrl001:~/ansible/playbooks/variablesPlaybooks# ansible-playbook -i /root/ansible/hosts/hosts.ini installPackagesLoop.yaml

PLAY [Ansible playbook to install 2 packages and remove them through variables and loops] ****************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [10.11.25.67]

TASK [Install the packages] ******************************************************************************************************
ok: [10.11.25.67] => (item=apache2)
changed: [10.11.25.67] => (item=mysql-client)

TASK [Remove the packages] *******************************************************************************************************
changed: [10.11.25.67] => (item=apache2)
changed: [10.11.25.67] => (item=mysql-client)

PLAY RECAP ***********************************************************************************************************************
10.11.25.67                : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0





We can create variable file for ansible like terraform, let's do it here 

root@dcex1510ctrl001:~/ansible/playbooks/variablesPlaybooks# cat vars.yaml
server_name: Test_VM1
server_port: 443


root@dcex1510ctrl001:~/ansible/playbooks/variablesPlaybooks# cat variables.yaml
- name: Ansible playbook to study variables
  hosts: localhost
  tasks:
  - name: Display Server Name
    debug:
      msg: "{{ server_name }}"
  - name: Display Server Name
    debug:
      msg: "{{ server_port }}"



root@dcex1510ctrl001:~/ansible/playbooks/variablesPlaybooks# ansible-playbook -e "@vars.yaml" variables.yaml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [Ansible playbook to study variables] ***************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [localhost]

TASK [Display Server Name] *******************************************************************************************************
ok: [localhost] => {
    "msg": "Test_VM1"
}

TASK [Display Server Name] *******************************************************************************************************
ok: [localhost] => {
    "msg": 443
}

PLAY RECAP ***********************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0



We can add the variable file in our playbook and the playbook will read the variables from it

root@dcex1510ctrl001:~/ansible/playbooks/variablesPlaybooks# cat variables.yaml
- name: Ansible playbook to study variables
  hosts: localhost
  vars_files:
    vars.yaml
  tasks:
  - name: Display Server Name
    debug:
      msg: "{{ server_name }}"
  - name: Display Server Name
    debug:
      msg: "{{ server_port }}"



root@dcex1510ctrl001:~/ansible/playbooks/variablesPlaybooks# ansible-playbook variables.yaml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [Ansible playbook to study variables] ***************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
ok: [localhost]

TASK [Display Server Name] *******************************************************************************************************
ok: [localhost] => {
    "msg": "Test_VM1"
}

TASK [Display Server Name] *******************************************************************************************************
ok: [localhost] => {
    "msg": 443
}

PLAY RECAP ***********************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


No comments:

Post a Comment

Install Vault on ubuntu 20

 Today I will install vault on ubuntu 20 to use it as a part of my Ansible learning journey  Add GPG key curl -fsSL https://apt.releases.has...