ansible-playbook 01_replace_playbook.yml
docker run -d -p 2222:22 yumainaura/python-ssh
ansible-playbook 01_replace_playbook.yml -i docker_inventory.example.txt | tee ansible_playbook_result.txt
- ansible 2.6.1
ansible-playbook 01_replace_playbook.yml
docker run -d -p 2222:22 yumainaura/python-ssh
ansible-playbook 01_replace_playbook.yml -i docker_inventory.example.txt | tee ansible_playbook_result.txt
- hosts: | |
- localhost | |
tasks: | |
- name: copy | |
copy: | |
src: people_source.txt | |
dest: people_replaced.txt | |
- name: Replace "Alice" by "Alice is a lady" in each lines | |
replace: | |
path: people_replaced.txt | |
regexp: '^Alice$' | |
replace: Alice is a lady | |
before: 'REPLACE ALICE BEFORE THIS LINE' | |
- name: Replace "Bob (lastname)" by "Bob (lastname) is a gentleman" in each lines | |
replace: | |
path: people_replaced.txt | |
# () is capture of regular expression | |
regexp: '^Bob ([a-zA-Z]+)$' | |
# \1 is replaced by first captured strings | |
replace: 'Bob \1 is a gentleman' | |
- name: Replace "Carol" | |
replace: | |
path: people_replaced.txt | |
regexp: '^Carol$' | |
replace: 'Carol is a loving child' | |
after: 'REPLACE CAROL AFTER THIS LINE' | |
#before: REPLACE CAROL BEFORE THIS LINE # Umm can not be used in combination with "after" ... why? | |
- name: check replaced file | |
slurp: | |
src: people_replaced.txt | |
register: replaced_file | |
- name: check replaced file body | |
debug: | |
msg: "{{ replaced_file.content | b64decode }}" |
PLAY [localhost] ************************************************************************************************************************** | |
TASK [Gathering Facts] ******************************************************************************************************************** | |
ok: [localhost] | |
TASK [copy] ******************************************************************************************************************************* | |
changed: [localhost] | |
TASK [Replace "Alice" by "Alice is a lady" in each lines] ********************************************************************************* | |
changed: [localhost] | |
TASK [Replace "Bob (lastname)" by "Bob (lastname) is a gentleman" in each lines] ********************************************************** | |
changed: [localhost] | |
TASK [Replace "Carol"] ******************************************************************************************************************** | |
changed: [localhost] | |
TASK [check replaced file] **************************************************************************************************************** | |
ok: [localhost] | |
TASK [check replaced file body] *********************************************************************************************************** | |
ok: [localhost] => { | |
"msg": "Alice is a lady\nBob Marley is a gentleman\nCarol\nAlice is a lady\nREPLACE ALICE BEFORE THIS LINE\nBob Wills is a gentleman\nREPLACE CAROL AFTER THIS LINE\nCarol is a loving child\nAlice\nBob Horper is a gentleman\nCarol is a loving child\n" | |
} | |
PLAY RECAP ******************************************************************************************************************************** | |
localhost : ok=7 changed=4 unreachable=0 failed=0 | |
[example] | |
localhost | |
[example:vars] | |
ansible_user=root | |
ansible_port=2222 | |
ansible_ssh_pass=screencast | |
https://gist.github.com/YumaInaura/592121dd8dc2a71b796591ccc4564f31 |
https://medium.com/supersonic-generation/ansible2-6-replace-module-example-a3e3c6a0b992 |
Alice is a lady | |
Bob Marley is a gentleman | |
Carol | |
Alice is a lady | |
REPLACE ALICE BEFORE THIS LINE | |
Bob Wills is a gentleman | |
REPLACE CAROL AFTER THIS LINE | |
Carol is a loving child | |
Alice | |
Bob Horper is a gentleman | |
Carol is a loving child |
Alice | |
Bob Marley | |
Carol | |
Alice | |
REPLACE ALICE BEFORE THIS LINE | |
Bob Wills | |
REPLACE CAROL AFTER THIS LINE | |
Carol | |
Alice | |
Bob Horper | |
Carol |
https://qiita.com/YumaInaura/items/0cf2976b09d0658fc38e |