Guest User

Untitled

a guest
Apr 23rd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. ## Ansible
  2. Ansible is used for automation.
  3.  
  4. In this post we will learn some basic stuff and workarounds in ansible.
  5.  
  6. Here is a sample ansible playbook:
  7.  
  8. ```
  9. hosts: localhost
  10. vars_files:
  11. - ./vars/main.yml
  12. tasks:
  13. - name: This a task
  14. vars:
  15. a: "apple"
  16. b: ["book", "cat", "dog"]
  17. command: echo "{{ a }}"
  18. register: sample_task
  19. - debug: var=sample_task # or msg="{{ sample_task }}"
  20. ```
  21.  
  22. #### Basic Commands:
  23.  
  24. ##### hosts
  25. ---
  26. This command contains a list or a environment where all the tasks will run. Hosts can be defined in multiple ways. Like using a custom inventory file, using default inventory file and directly writing after hosts module.
  27. - Directly Writing after hosts module
  28. `hosts: localhost`
  29. - using inventory file
  30. Create an inventory file with name `inventory` with following containts:
  31. ```
  32. [host1]
  33. IP_1
  34. [host2]
  35. IP_2
  36. [host3]
  37. IP_3
  38. [host_list:children]
  39. host1
  40. host2
  41. host3
  42. ```
  43. Write hosts module as:
  44. `hosts: host1:host2:host3` or `hosts: host1` or `hosts: hostlist`
  45.  
  46. To exucute playbook use command: `ansible-playbook -i inventory my_playbook.yml --private-key="KEY"`
  47. - using default inventory
  48. Default inventory file is at `/etc/ansible/hosts`. If inventory file is not provided, this is used as inventory.
  49.  
  50. ##### tasks
  51. ---
  52. This command is used to write tasks to be executed on the environmet, all tasks will be inside this only, for that environment. A simple task is written as:
  53. ```
  54. - name: This a task
  55. vars:
  56. a: "apple"
  57. b: ["book", "cat", "dog"]
  58. command: echo "{{ a }}"
  59. register: sample_task
  60. ```
  61.  
  62. ##### script
  63. ---
  64. Script module is used to upload script at the environment and execute the script. script will contain a complete playbook.
  65. A sample task with script module:
  66. ```
  67. - script: './files/my_script.yml -i inventory --private-key=keys/KEY.pem'
  68. args:
  69. chdir: '/home/ubuntu/scripts' # the inventory file and keys folder will be located here
  70. register: output
  71. ```
  72.  
  73. ##### vars_files
  74. ---
  75. List of files that contains the varibales needed in the tasks. A typical vars file:
  76. ```
  77. INT_VAR: 12313
  78. STR_VAR: "Apple"
  79. ```
  80. use:
  81. ```
  82. vars_files:
  83. - ./vars/main.yml
  84. ```
  85.  
  86. ##### vars
  87. ---
  88. This module is used to inialize new variable.
  89.  
  90. ##### set_fact
  91. ---
  92. This module is used to inialize new variable.
  93.  
  94. ##### lineinfile
  95. ---
  96.  
  97. ##### with_items, with_list, with_dict
  98. ---
  99. For Itrating over list etc.
  100. #### Data Structure in Ansible:
  101. ##### List:
  102. ---
  103. ```
  104. vars:
  105. my_list:
  106. - 1
  107. - 2
  108. - 3
  109. ```
  110. To append more:
  111. ```
  112. vars:
  113. my_list: []
  114. set_fact:
  115. my_list: "{{ my_list }} + [ {{ item }} ]"
  116. ```
  117. ##### Dictonary:
  118. ---
  119. ```
  120. vars:
  121. dict:
  122. a: "apple"
  123. b: "book"
  124. c: "cat"
  125. ```
  126. To append more:
  127. ```
  128. - name: Create and add items to dict
  129. vars:
  130. def_val: "asdf"
  131. dict: {}
  132. set_fact:
  133. dict: '{{ dict | combine( { item: def_val } ) }}'
  134. with_items:
  135. - 1
  136. - 2
  137. - 3
  138. ```
  139. To get values:
  140. ```
  141. - name: Get values
  142. debug:
  143. msg: "{{item.key}}: {{ item.value }}"
  144. with_dict: "{{ dict }}"
  145.  
  146. ```
Add Comment
Please, Sign In to add comment